Skip to content

Instantly share code, notes, and snippets.

@marcbowes
Created March 29, 2010 16:21
Show Gist options
  • Save marcbowes/348043 to your computer and use it in GitHub Desktop.
Save marcbowes/348043 to your computer and use it in GitHub Desktop.
diff -ur marcbowes-UsingYAML-590c028/lib/using_yaml/array.rb UsingYAML-590c028/lib/using_yaml/array.rb
--- marcbowes-UsingYAML-590c028/lib/using_yaml/array.rb 2010-03-06 22:14:19.000000000 +0200
+++ UsingYAML-590c028/lib/using_yaml/array.rb 2010-03-29 18:05:01.000000000 +0200
@@ -25,5 +25,6 @@
# Load in the extensions for this instance
array.extend(extensions)
+ array
end
end
diff -ur marcbowes-UsingYAML-590c028/lib/using_yaml/hash.rb UsingYAML-590c028/lib/using_yaml/hash.rb
--- marcbowes-UsingYAML-590c028/lib/using_yaml/hash.rb 2010-03-06 22:14:19.000000000 +0200
+++ UsingYAML-590c028/lib/using_yaml/hash.rb 2010-03-29 18:19:28.000000000 +0200
@@ -13,7 +13,8 @@
name = args.shift.to_s
if args.empty?
- send(:[], name) || UsingYAML.add_extensions(nil)
+ value = send(:[], name)
+ value.nil? ? UsingYAML::NilClass : value
elsif args.size == 1 && name =~ /(.+)=/
# This is an "alias" turning self.key= into self[key]=
# Also extends the incoming value so that it behaves
@@ -53,5 +54,6 @@
# Load in the extensions for this instance
hash.extend(extensions)
+ hash
end
end
diff -ur marcbowes-UsingYAML-590c028/lib/using_yaml/nilclass.rb UsingYAML-590c028/lib/using_yaml/nilclass.rb
--- marcbowes-UsingYAML-590c028/lib/using_yaml/nilclass.rb 2010-03-06 22:14:19.000000000 +0200
+++ UsingYAML-590c028/lib/using_yaml/nilclass.rb 2010-03-29 18:15:13.000000000 +0200
@@ -4,7 +4,7 @@
define_method(:method_missing) do
# Child objects should not have #save
if respond_to? :save
- add_extensions(nil)
+ UsingYAML::NilClass
else
# One nil is the same as the next :)
self
@@ -29,5 +29,6 @@
end
instance.extend(extensions)
+ instance
end
end
diff -ur marcbowes-UsingYAML-590c028/lib/using_yaml.rb UsingYAML-590c028/lib/using_yaml.rb
--- marcbowes-UsingYAML-590c028/lib/using_yaml.rb 2010-03-06 22:14:19.000000000 +0200
+++ UsingYAML-590c028/lib/using_yaml.rb 2010-03-29 18:13:27.000000000 +0200
@@ -21,6 +21,8 @@
#
# See +using_yaml+ for usage information.
module UsingYAML
+ NilClass = add_nilclass_extensions(nil, nil)
+
class << self
# Extends the incoming Array/Hash with magic which makes it
# possible to +save+ and (in the case of Hashes) use methods
@@ -31,8 +33,12 @@
add_array_extensions(object, pathname)
when Hash
add_hash_extensions(object, pathname)
- when NilClass
- add_nilclass_extensions(object, pathname)
+ when ::NilClass
+ if pathname
+ add_nilclass_extensions(object, pathname)
+ else
+ UsingYAML::NilClass
+ end
end
object
@@ -142,7 +148,7 @@
@using_yaml_cache[pathname] = UsingYAML.add_extensions(YAML.load_file(pathname), pathname)
rescue Exception => e
$stderr.puts "(UsingYAML) Could not load #{filename}: #{e.message}" unless UsingYAML.squelched?
- UsingYAML.add_extensions(nil)
+ @using_yaml_cache[pathname] = UsingYAML.add_extensions(nil, pathname)
end
end
diff -ur marcbowes-UsingYAML-590c028/spec/using_yaml/using_yaml_spec.rb UsingYAML-590c028/spec/using_yaml/using_yaml_spec.rb
--- marcbowes-UsingYAML-590c028/spec/using_yaml/using_yaml_spec.rb 2010-03-06 22:14:19.000000000 +0200
+++ UsingYAML-590c028/spec/using_yaml/using_yaml_spec.rb 2010-03-29 18:18:05.000000000 +0200
@@ -11,7 +11,12 @@
end
it "should gracefully handle nil.nil..." do
- @person.children.invalid.call.should be_nil
+ @person.children.invalid.foo.should be_nil
+ end
+
+ it "should return false when expected" do
+ YAML.stubs(:load_file).with(anything).returns({ 'example' => false })
+ @person.children.example.should == false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment