Permalink
Browse files

Immutable classes don't have writer methods so violence is required t…

…o set the value of the instance variable matching the given key
  • Loading branch information...
1 parent e3ba976 commit 6bb6c4042f29ea8e795f5a31f539343bbc12152c James Ottaway committed Aug 1, 2012
Showing with 20 additions and 1 deletion.
  1. +5 −1 lib/machinist/lathe.rb
  2. +15 −0 spec/machinable_spec.rb
View
6 lib/machinist/lathe.rb
@@ -53,7 +53,11 @@ def make_one_value(attribute, args) #:nodoc:
def assign_attribute(key, value) #:nodoc:
@assigned_attributes[key.to_sym] = value
- @object.send("#{key}=", value)
+ begin
+ @object.send("#{key}=", value)
+ rescue
+ @object.instance_variable_set("@#{key}", value)
+ end
end
def attribute_assigned?(key) #:nodoc:
View
15 spec/machinable_spec.rb
@@ -10,6 +10,11 @@ class Comment
extend Machinist::Machinable
attr_accessor :post, :title
end
+
+ class Immutable
+ extend Machinist::Machinable
+ attr_reader :name
+ end
end
describe Machinist::Machinable do
@@ -72,6 +77,16 @@ class Comment
end
end
+ it "supports immutable classes" do
+ MachinableSpecs::Immutable.blueprint do
+ name { "Mr. Immutable" }
+ end
+
+ post = MachinableSpecs::Immutable.make
+ post.should be_a(MachinableSpecs::Immutable)
+ post.name.should == "Mr. Immutable"
+ end
+
it "fails without a blueprint" do
expect do
MachinableSpecs::Post.make

0 comments on commit 6bb6c40

Please sign in to comment.