Created
February 9, 2011 03:46
-
-
Save benhamill/817848 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
module MyModule | |
module ClassMethods | |
def from_yaml(file=nil) | |
# ... | |
end | |
end | |
module InstanceMethods | |
def to_yaml(file=nil) | |
file ||= "tmp/#{name.downcase}.yaml" | |
File.open(file, 'w') do |file| | |
f.puts YAML.dump(to_hash) | |
end | |
end | |
def to_hash | |
# ... | |
end | |
end | |
def self.included(receiver) | |
receiver.extend ClassMethods | |
receiver.send :include, InstanceMethods | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'my_module' | |
class MyModuleHolder | |
include MyModule | |
end | |
describe "MyModule" do | |
# ... | |
context "instance methods" do | |
subject { MyModuleHolder.new } | |
# ... | |
describe "to_yaml" do | |
before(:each) do | |
@directory = "tmp/specs" | |
@file_location = "#{@directory}/test.yaml" | |
Dir.mkdir(@directory) unless Dir.exists?(@directory) | |
File.delete(@file_location) if File.exists?(@file_location) | |
end | |
after(:each) do | |
File.delete(@file_location) if File.exists?(@file_location) | |
end | |
it "should write to a given file" do | |
subject.should_receive(:to_hash) { { 'foo' => 'bar', 'baz' => 'bom' } } | |
subject.to_yaml(@file_location) | |
File.exists?(@file_location).should == true | |
end | |
it "should convert the object's hash to yaml" do | |
subject.should_receive(:to_hash) { { 'foo' => 'bar', 'baz' => 'bom' } } | |
subject.to_yaml(@file_location) | |
expected_yaml = <<-YAML | |
--- | |
foo: bar | |
baz: bom | |
YAML | |
File.read(@file_location).should == expected_yaml | |
end | |
end | |
end | |
end |
Retyping fail. Good eye. Fixed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cut and paste fail? "indlue MyModule"