Skip to content

Commit

Permalink
Don't raise a NoMethod error for attributes with a falsy value.
Browse files Browse the repository at this point in the history
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
Manfred committed Oct 16, 2010
1 parent 29f20f6 commit 651a0bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -31,4 +31,5 @@ The easiest way to install Broach and its dependencies is to install the gem.


Eloy Duran <eloy@fngtps.com> Eloy Duran <eloy@fngtps.com>
Ilya Sabanin <ilya.sabanin@gmail.com> Ilya Sabanin <ilya.sabanin@gmail.com>
Todd Eichel <todd@toddeichel.com> Todd Eichel <todd@toddeichel.com>
Anton Mironov <ant.mironov@gmail.com>
4 changes: 2 additions & 2 deletions lib/broach/attributes.rb
Expand Up @@ -8,8 +8,8 @@ def initialize(attributes)
def id; @attributes['id']; end def id; @attributes['id']; end


def method_missing(method, *arguments, &block) def method_missing(method, *arguments, &block)
if value = @attributes[method.to_s] if key = method.to_s and @attributes.has_key?(key)
value @attributes[key]
else else
super super
end end
Expand Down
23 changes: 23 additions & 0 deletions test/unit/attributes_test.rb
@@ -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.