Skip to content

Commit

Permalink
Added a way to disable object caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
notahat committed Jul 6, 2010
1 parent 28626ae commit d495e00
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/machinist.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'machinist/blueprint'
require 'machinist/configuration'
require 'machinist/exceptions'
require 'machinist/lathe'
require 'machinist/machinable'
Expand Down
32 changes: 32 additions & 0 deletions lib/machinist/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion lib/machinist/machinable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions spec/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.