Skip to content
Browse files

Added a way to disable object caching.

  • Loading branch information...
1 parent 28626ae commit d495e00987f1f8880873bb8c16e65f5eab1d24ba @notahat notahat committed Jul 7, 2010
Showing with 52 additions and 1 deletion.
  1. +1 −0 lib/machinist.rb
  2. +32 −0 lib/machinist/configuration.rb
  3. +5 −1 lib/machinist/machinable.rb
  4. +10 −0 spec/active_record_spec.rb
  5. +4 −0 spec/spec_helper.rb
View
1 lib/machinist.rb
@@ -1,4 +1,5 @@
require 'machinist/blueprint'
+require 'machinist/configuration'
require 'machinist/exceptions'
require 'machinist/lathe'
require 'machinist/machinable'
View
32 lib/machinist/configuration.rb
@@ -0,0 +1,32 @@
+module Machinist
+
+ # Configure Machinist.
+ #
+ # To change Machinist configuration, do something like this in your
+ # config/environments/test.rb or somewhere similar:
+ #
+ # Machinist.configure do |config|
+ # config.cache_objects = false
+ # end
+ class Configuration
+ # Set this to false to disable object caching. Defaults to true.
+ attr_accessor :cache_objects
+
+ def cache_objects? #:nodoc:
+ @cache_objects
+ end
+
+ def initialize #:nodoc:
+ self.cache_objects = true
+ end
+ end
+
+ def self.configuration
+ @configuration ||= Configuration.new
+ end
+
+ def self.configure
+ yield(configuration)
+ end
+
+end
View
6 lib/machinist/machinable.rb
@@ -51,7 +51,11 @@ def make(*args)
# Arguments are the same as for make.
def make!(*args)
decode_args_to_make(*args) do |blueprint, attributes|
- Shop.instance.buy(blueprint, attributes)
+ if Machinist.configuration.cache_objects?
+ Shop.instance.buy(blueprint, attributes)
+ else
+ blueprint.make!(attributes)
+ end
end
end
View
10 spec/active_record_spec.rb
@@ -48,6 +48,16 @@ def fake_a_test
fake_a_test { post_b = Post.make! }
post_a.should == post_b
end
+
+ it "should not buy objects from the shop if caching is disabled" do
+ Machinist.configuration.cache_objects = false
+ Post.blueprint { }
+ post_a, post_b = nil, nil
+ fake_a_test { post_a = Post.make! }
+ fake_a_test { post_b = Post.make! }
+ post_a.should_not == post_b
+ Machinist.configuration.cache_objects = true
+ end
end
context "associations support" do
View
4 spec/spec_helper.rb
@@ -5,3 +5,7 @@
require 'test/unit'
require 'rspec'
require 'machinist'
+
+Machinist.configure do |config|
+ config.cache_objects = true
+end

0 comments on commit d495e00

Please sign in to comment.
Something went wrong with that request. Please try again.