Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed overriding initialize and setting an attribute. Fixes #13.

  • Loading branch information...
commit 9d34ff0036adbf9f3775a3542cbd742f7fa7eecd 1 parent fad9e2f
@jnunemaker jnunemaker authored
Showing with 23 additions and 1 deletion.
  1. +2 −1  lib/toy/attributes.rb
  2. +21 −0 spec/toy/attributes_spec.rb
View
3  lib/toy/attributes.rb
@@ -99,6 +99,7 @@ def []=(key, value)
private
def read_attribute(key)
+ @attributes ||= {}
@attributes[key.to_s]
end
@@ -127,7 +128,7 @@ def attribute?(key)
end
def initialize_attributes_with_defaults
- @attributes = {}
+ @attributes ||= {}
self.class.defaulted_attributes.each do |attribute|
@attributes[attribute.name.to_s] = attribute.default
end
View
21 spec/toy/attributes_spec.rb
@@ -423,4 +423,25 @@
User.new.skills.should == []
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
Please sign in to comment.
Something went wrong with that request. Please try again.