Permalink
Browse files

Don't raise a NoMethod error for attributes with a falsy value.

Fixes a problem with the Attributes mixin where an attribute set with a falsy
value such as false or nil, the model would not respond to the attribute
reader.
  • Loading branch information...
1 parent 29f20f6 commit 651a0bd00bb8c6ce620776022e787aaa6518769f @Manfred committed Oct 16, 2010
Showing with 27 additions and 3 deletions.
  1. +2 −1 README.md
  2. +2 −2 lib/broach/attributes.rb
  3. +23 −0 test/unit/attributes_test.rb
View
@@ -31,4 +31,5 @@ The easiest way to install Broach and its dependencies is to install the gem.
Eloy Duran <eloy@fngtps.com>
Ilya Sabanin <ilya.sabanin@gmail.com>
-Todd Eichel <todd@toddeichel.com>
+Todd Eichel <todd@toddeichel.com>
+Anton Mironov <ant.mironov@gmail.com>
View
@@ -8,8 +8,8 @@ def initialize(attributes)
def id; @attributes['id']; end
def method_missing(method, *arguments, &block)
- if value = @attributes[method.to_s]
- value
+ if key = method.to_s and @attributes.has_key?(key)
+ @attributes[key]
else
super
end
@@ -0,0 +1,23 @@
+require File.expand_path('../../start', __FILE__)
+
+class Mole
+ include Broach::Attributes
+end
+
+describe "A class with Attributes" do
+ before do
+ @mole = Mole.new('name' => "Jerry", 'glasses' => false, 'description' => nil)
+ end
+
+ it "returns it's attribute through accessor methods" do
+ @mole.name.should == "Jerry"
+ @mole.glasses.should.be.false
+ @mole.description.should.be.nil
+ end
+
+ it "does not respond to methods for attributes which haven't been set" do
+ lambda {
+ @mole.unknown
+ }.should.raise(NoMethodError)
+ end
+end

0 comments on commit 651a0bd

Please sign in to comment.