Skip to content

Commit

Permalink
Fixed overriding initialize and setting an attribute. Fixes #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed May 28, 2011
1 parent fad9e2f commit 9d34ff0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/toy/attributes.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def []=(key, value)


private private
def read_attribute(key) def read_attribute(key)
@attributes ||= {}
@attributes[key.to_s] @attributes[key.to_s]
end end


Expand Down Expand Up @@ -127,7 +128,7 @@ def attribute?(key)
end end


def initialize_attributes_with_defaults def initialize_attributes_with_defaults
@attributes = {} @attributes ||= {}
self.class.defaulted_attributes.each do |attribute| self.class.defaulted_attributes.each do |attribute|
@attributes[attribute.name.to_s] = attribute.default @attributes[attribute.name.to_s] = attribute.default
end end
Expand Down
21 changes: 21 additions & 0 deletions spec/toy/attributes_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -423,4 +423,25 @@
User.new.skills.should == [] User.new.skills.should == []
end end
end end

# https://github.com/newtoy/toystore/issues/13
describe "Overriding initialize and setting an attribute before calling super" do
before do
User.attribute(:name, String)
User.class_eval do
def initialize(*)
self.name = 'John'
super
end
end
end

it "does not throw error" do
lambda { User.new }.should_not raise_error
end

it "sets value" do
User.new.name.should == 'John'
end
end
end end

0 comments on commit 9d34ff0

Please sign in to comment.