Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Brutally ripped out all the caching code.

  • Loading branch information...
commit 2839888c92da324a18682970fbde233a1064819d 1 parent 729dcf3
@notahat notahat authored
View
45 Gemfile.lock
@@ -0,0 +1,45 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.0.4)
+ activesupport (= 3.0.4)
+ builder (~> 2.1.2)
+ i18n (~> 0.4)
+ activerecord (3.0.4)
+ activemodel (= 3.0.4)
+ activesupport (= 3.0.4)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activesupport (3.0.4)
+ arel (2.0.8)
+ builder (2.1.2)
+ diff-lcs (1.1.2)
+ git (1.2.5)
+ i18n (0.5.0)
+ jeweler (1.5.2)
+ bundler (~> 1.0.0)
+ git (>= 1.2.5)
+ rake
+ mysql (2.8.1)
+ rake (0.8.7)
+ rcov (0.9.9)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.5.0)
+ tzinfo (0.3.24)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activerecord
+ jeweler
+ mysql
+ rake
+ rcov
+ rspec (>= 2.0.0.beta.12)
View
12 lib/machinist.rb
@@ -1,17 +1,5 @@
require 'machinist/blueprint'
-require 'machinist/configuration'
require 'machinist/exceptions'
require 'machinist/lathe'
require 'machinist/machinable'
-require 'machinist/shop'
-require 'machinist/warehouse'
-
-module Machinist
-
- # Call this before each test to get Machinist ready.
- def self.reset_before_test
- Shop.instance.restock
- end
-
-end
View
32 lib/machinist/configuration.rb
@@ -1,32 +0,0 @@
-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
10 lib/machinist/machinable.rb
@@ -45,17 +45,11 @@ def make(*args)
# :call-seq:
# make!([count], [blueprint_name], [attributes = {}])
#
- # A cached object will be returned from the shop if possible. See
- # Machinist::Shop.
- #
# Arguments are the same as for make.
def make!(*args)
decode_args_to_make(*args) do |blueprint, attributes|
- if Machinist.configuration.cache_objects?
- Shop.instance.buy(blueprint, attributes)
- else
- blueprint.make!(attributes)
- end
+ raise BlueprintCantSaveError.new(blueprint) unless blueprint.respond_to?(:make!)
+ blueprint.make!(attributes)
end
end
View
52 lib/machinist/shop.rb
@@ -1,52 +0,0 @@
-module Machinist
-
- # The shop takes care of caching database objects.
- #
- # Calling make! on a class requests objects from the shop; you don't
- # normally access the shop directly.
- #
- # Read more about object caching on the
- # wiki[http://wiki.github.com/notahat/machinist/object-caching].
- class Shop
-
- # Return the singleton Shop instance.
- def self.instance
- @instance ||= Shop.new
- end
-
- def initialize #:nodoc:
- reset!
- end
-
- # Throw out the entire collection of cached objects.
- def reset!
- @warehouse = Warehouse.new
- restock
- end
-
- # Restock the shop with all the cached objects we've got.
- #
- # This should be called before each test.
- def restock
- @back_room = @warehouse.clone
- end
-
- # Buy a (possibly cached) object from the shop.
- #
- # This is just like constructing an object by calling Blueprint#make!,
- # but it will return a previously cached object if one is available.
- def buy(blueprint, attributes = {})
- raise BlueprintCantSaveError.new(blueprint) unless blueprint.respond_to?(:make!)
-
- shelf = @back_room[blueprint, attributes]
- if shelf.empty?
- object = blueprint.outside_transaction { blueprint.make!(attributes) }
- @warehouse[blueprint, attributes] << blueprint.box(object)
- object
- else
- blueprint.unbox(shelf.shift)
- end
- end
-
- end
-end
View
27 spec/active_record_spec.rb
@@ -5,18 +5,9 @@
include ActiveRecordEnvironment
before(:each) do
- Machinist::Shop.instance.reset!
empty_database!
end
- def fake_a_test
- ActiveRecord::Base.transaction do
- Machinist.reset_before_test
- yield
- raise ActiveRecord::Rollback
- end
- end
-
context "make" do
it "should return an unsaved object" do
Post.blueprint { }
@@ -40,24 +31,6 @@ def fake_a_test
User.make!(:username => "")
}.should raise_error(ActiveRecord::RecordInvalid)
end
-
- it "should buy objects from the shop" do
- 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 == 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
94 spec/shop_spec.rb
@@ -1,94 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper'
-require 'support/active_record_environment'
-
-describe Machinist::Shop do
-
- before(:each) do
- @shop = Machinist::Shop.new
- end
-
- def fake_a_test
- ActiveRecord::Base.transaction do
- @shop.restock
- yield
- raise ActiveRecord::Rollback
- end
- end
-
- it "should cache an object" do
- blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
-
- post_a, post_b = nil, nil
- fake_a_test { post_a = @shop.buy(blueprint) }
- fake_a_test { post_b = @shop.buy(blueprint) }
-
- post_b.should == post_a
- post_b.should_not equal(post_a)
- end
-
- it "should cache an object with attributes" do
- blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
-
- post_a, post_b = nil, nil
- fake_a_test { post_a = @shop.buy(blueprint, :title => "Test Title") }
- fake_a_test { post_b = @shop.buy(blueprint, :title => "Test Title") }
-
- post_b.should == post_a
- post_b.should_not equal(post_a)
- end
-
- it "should not confuse objects with different blueprints" do
- blueprint_a = Machinist::ActiveRecord::Blueprint.new(Post) { }
- blueprint_b = Machinist::ActiveRecord::Blueprint.new(Post) { }
-
- post_a, post_b = nil, nil
- fake_a_test { post_a = @shop.buy(blueprint_a) }
- fake_a_test { post_b = @shop.buy(blueprint_b) }
-
- post_b.should_not == post_a
- end
-
- it "should not confuse objects with the same blueprint but different attributes" do
- blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
-
- post_a, post_b = nil, nil
- fake_a_test { post_a = @shop.buy(blueprint, :title => "A Title") }
- fake_a_test { post_b = @shop.buy(blueprint, :title => "Not A Title") }
-
- post_b.should_not == post_a
- end
-
- it "should cache multiple similar objects" do
- blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
-
- post_a, post_b = nil, nil
- fake_a_test do
- post_a = @shop.buy(blueprint, :title => "Test Title")
- post_b = @shop.buy(blueprint, :title => "Test Title")
- post_b.should_not == post_a
- end
-
- fake_a_test do
- @shop.buy(blueprint, :title => "Test Title").should == post_a
- @shop.buy(blueprint, :title => "Test Title").should == post_b
- post_c = @shop.buy(blueprint, :title => "Test Title")
- post_c.should_not == post_a
- post_c.should_not == post_b
- end
- end
-
- it "should ensure future copies of a cached object do not reflect changes to the original" do
- blueprint = Machinist::ActiveRecord::Blueprint.new(Post) { }
-
- post_a, post_b = nil, nil
- fake_a_test do
- post_a = @shop.buy(blueprint, :title => "Test Title")
- post_a.title = "Changed Title"
- post_a.save!
- end
- fake_a_test { post_b = @shop.buy(blueprint, :title => "Test Title") }
-
- post_b.title.should == "Test Title"
- end
-
-end
View
4 spec/spec_helper.rb
@@ -5,7 +5,3 @@
require 'test/unit'
require 'rspec'
require 'machinist'
-
-Machinist.configure do |config|
- config.cache_objects = true
-end
View
24 spec/warehouse_spec.rb
@@ -1,24 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper'
-
-describe Machinist::Warehouse do
-
- it "should store and retrieve values" do
- warehouse = Machinist::Warehouse.new
- warehouse[1, 2] = "example"
- warehouse[1, 2].should == "example"
- end
-
- it "should return an empty array for a new key" do
- warehouse = Machinist::Warehouse.new
- warehouse[1, 2].should == []
- warehouse[3, 4] << "example"
- warehouse[3, 4].should == ["example"]
- end
-
- it "should clone" do
- warehouse = Machinist::Warehouse.new
- warehouse[1, 2] = "example"
- warehouse.clone.should == warehouse
- end
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.