ar-simple-idmap 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.2.0
@@ -56,8 +56,6 @@ end
56
56
 
57
57
  if defined? ::ActionDispatch
58
58
  ActionDispatch::Callbacks.send :include, DispatcherMethods
59
- RAILS_DEFAULT_LOGGER.warn "CACHING OBJECTS"
60
59
  else
61
60
  ActionController::Dispatcher.send :include, DispatcherMethods
62
- RAILS_DEFAULT_LOGGER.warn "CACHING OBJECTS"
63
61
  end
@@ -12,6 +12,7 @@ module ActiveRecord
12
12
  alias_method_chain :instantiate, :identity_map
13
13
  end
14
14
  alias_method_chain :create, :identity_map
15
+ alias_method_chain :destroy, :identity_map
15
16
  end
16
17
  end
17
18
  end
@@ -49,10 +50,11 @@ module ActiveRecord
49
50
  from_arg0 = args.size == 1 ||
50
51
  args[1].is_a?(Hash) && !args[1].values.any?
51
52
  from_condition_ids = !from_arg0 &&
52
- (args[0] == :all || args[0] == :first)
53
+ (args[0] == :all || args[0] == :first) &&
53
54
  args.size == 2 && args[1].is_a?(Hash) &&
54
55
  args[1].all?{|key, value| key == :conditions || value.blank?} &&
55
- args[1][:conditions].try(:keys) == [:id]
56
+ args[1][:conditions].is_a?(Hash) &&
57
+ args[1][:conditions].keys == [:id]
56
58
  if from_arg0 || from_condition_ids
57
59
  ids = from_arg0 ? args[0] : args[1][:conditions][:id]
58
60
  if ids.is_a?(Array)
@@ -21,7 +21,7 @@ module ActiveRecord
21
21
 
22
22
  def delete(id)
23
23
  @object.id = id
24
- @objects.delete(id)
24
+ @objects.delete(@object.id)
25
25
  end
26
26
  end
27
27
 
@@ -86,4 +86,4 @@ module ActiveRecord
86
86
  extend ThreadIdentityMap::ClassMethods
87
87
  # self.class.send :alias_method_chain, :uncached, :identity_map
88
88
  end
89
- end
89
+ end
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  describe "Customers" do
4
4
 
5
5
  before(:each) do
6
- Thread.current["identity_map"] = Cache.new
6
+ ActiveRecord::Base.create_identity_map
7
7
  end
8
8
 
9
9
  it "should load the same model twice" do
@@ -11,27 +11,105 @@ describe "Customers" do
11
11
  c2 = Customer.first
12
12
  c1.__id__.should == c2.__id__
13
13
  end
14
+
15
+ it "should fetch loaded model from cache" do
16
+ c1 = Customer.first
17
+ Customer.connection.should_not_receive(:select_all)
18
+ c2 = Customer.find(c1.id)
19
+ end
14
20
 
15
21
  it "should work for has_many associations" do
16
22
  p1 = PhoneNumber.first
17
- p2 = Customer.first.phone_numbers.first
23
+ ps = Customer.first.phone_numbers
24
+ p2 = ps.first
18
25
  p1.__id__.should == p2.__id__
19
26
  end
20
27
 
21
28
  it "should work for belongs_to assocations" do
22
29
  d1 = Customer.first
23
- d2 = PhoneNumber.first.customer
30
+ p1 = PhoneNumber.first
31
+ Customer.connection.should_not_receive(:select_all)
32
+ d2 = p1.customer
24
33
  d1.__id__.should == d2.target.__id__
25
34
  end
26
35
 
27
- it "should work for creating objects" do
28
- c1 = Customer.create(:name => "billy")
29
- c2 = Customer.find_by_name("billy")
30
- c1.__id__.should == c2.__id__
31
- end
36
+ describe "creation and deletion:" do
37
+ before(:each) do
38
+ @billy = Customer.create(:name => "billy")
39
+ end
40
+
41
+ it "should work for creating objects" do
42
+ c2 = Customer.find_by_name("billy")
43
+ @billy.__id__.should == c2.__id__
44
+ Customer.connection.should_not_receive(:select_all)
45
+ c3 = Customer.find(@billy.id)
46
+ @billy.__id__.should == c3.__id__
47
+ end
32
48
 
49
+ it "should work for destroyed objects" do
50
+ @billy.destroy
51
+ c2 = Customer.find_by_id(@billy.id)
52
+ c2.should be_nil
53
+ end
54
+
55
+ after(:each) do
56
+ @billy.destroy unless @billy.destroyed?
57
+ end
58
+ end
59
+
60
+ describe "switching identity map:" do
61
+ it "should disable id_map with `without_id_map`" do
62
+ c1, c2, c3 = Customer.first, nil, nil
63
+ Customer.without_id_map do
64
+ c2 = Customer.first
65
+ c3 = Customer.first
66
+ end
67
+ c1.__id__.should_not == c2.__id__
68
+ c1.__id__.should_not == c3.__id__
69
+ c3.__id__.should_not == c2.__id__
70
+ end
71
+
72
+ it "should use current id_map with `with_id_map(false)`" do
73
+ c1, c2, c3 = Customer.first, nil, nil
74
+ Customer.with_id_map(false) do
75
+ c2 = Customer.first
76
+ Customer.connection.should_not_receive(:select_all)
77
+ c3 = Customer.find(c2.id)
78
+ end
79
+ c1.__id__.should == c2.__id__
80
+ c1.__id__.should == c3.__id__
81
+ c3.__id__.should == c2.__id__
82
+ end
83
+
84
+ it "should create new id_map with `with_id_map`" do
85
+ c1, c2, c3 = Customer.first, nil, nil
86
+ Customer.with_id_map do
87
+ c2 = Customer.first
88
+ Customer.connection.should_not_receive(:select_all)
89
+ c3 = Customer.find(c2.id)
90
+ end
91
+ c1.__id__.should_not == c2.__id__
92
+ c1.__id__.should_not == c3.__id__
93
+ c3.__id__.should == c2.__id__
94
+ end
95
+
96
+ it "should reenable id_map with `with_id_map`" do
97
+ c1, c2, c3 = Customer.first, nil, nil
98
+ Customer.without_id_map do
99
+ Customer.with_id_map do
100
+ c2 = Customer.first
101
+ Customer.connection.should_not_receive(:select_all)
102
+ c3 = Customer.find(c2.id)
103
+ end
104
+ end
105
+ c1.__id__.should_not == c2.__id__
106
+ c1.__id__.should_not == c3.__id__
107
+ c3.__id__.should == c2.__id__
108
+ end
109
+ end
110
+
33
111
  after(:each) do
34
- Thread.current["identity_map"] = nil
112
+ ActiveRecord::Base.drop_identity_map
35
113
  end
36
114
 
37
- end
115
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,38 +1,42 @@
1
- begin
2
- require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
- rescue LoadError
4
- puts "You need to install rspec in your base app"
5
- exit
6
- end
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'active_support'
4
+ require 'active_support/test_case'
5
+ require 'active_record'
6
+ require 'active_record/test_case'
7
+ require 'action_controller'
8
+ require 'action_view'
9
+ require 'identity_map'
10
+
11
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
12
+
13
+ ActiveRecord::Base.establish_connection(
14
+ :adapter=>'sqlite3',
15
+ :database=>'spec/identity_map.test.sqlite3'
16
+ )
7
17
 
8
- plugin_spec_dir = File.dirname(__FILE__)
9
- ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
-
11
- def load_schema
12
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
13
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
14
-
15
- db_adapter = ENV['DB']
16
- # no db passed, try one of these fine config-free DBs before bombing.
17
- db_adapter ||=
18
- begin
19
- require 'rubygems'
20
- require 'sqlite'
21
- 'sqlite'
22
- rescue MissingSourceFile
23
- begin
24
- require 'sqlite3'
25
- 'sqlite3'
26
- rescue MissingSourceFile
27
- end
28
- end
29
-
30
- if db_adapter.nil?
31
- raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
18
+ ActiveRecord::Schema.define(:version => 0) do
19
+ puts "Creating Schema"
20
+ create_table :customers, :force => true do |t|
21
+ t.string :name
32
22
  end
23
+ create_table :phone_numbers, :force => true do |t|
24
+ t.string :number
25
+ t.integer :customer_id
26
+ end
27
+ end
33
28
 
34
- ActiveRecord::Base.establish_connection(config[db_adapter])
35
- load(File.dirname(__FILE__) + "/db/schema.rb")
36
- require File.dirname(__FILE__) + '/../init.rb'
29
+ class Customer < ActiveRecord::Base
30
+ use_id_map
31
+ has_many :phone_numbers
37
32
  end
38
- require File.dirname(__FILE__) + '/test_models.rb'
33
+
34
+ customer = Customer.create(:name => "Boneman")
35
+
36
+ class PhoneNumber < ActiveRecord::Base
37
+ use_id_map
38
+ belongs_to :customer
39
+ end
40
+
41
+ phone_number = customer.phone_numbers.create(:number => "8675309")
42
+
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 6
10
- version: 0.1.6
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sokolov Yura aka funny_falcon
@@ -54,12 +54,9 @@ files:
54
54
  - lib/identity_map/active_record/associations.rb
55
55
  - lib/identity_map/active_record/base.rb
56
56
  - lib/identity_map/cache.rb
57
- - spec/database.yml
58
- - spec/db/schema.rb
59
57
  - spec/identity_map_spec.rb
60
58
  - spec/spec.opts
61
59
  - spec/spec_helper.rb
62
- - spec/test_models.rb
63
60
  has_rdoc: true
64
61
  homepage: http://github.com/funny-falcon/identity-map
65
62
  licenses: []
@@ -95,7 +92,5 @@ signing_key:
95
92
  specification_version: 3
96
93
  summary: Simple identity map for ActiveRecord
97
94
  test_files:
98
- - spec/db/schema.rb
99
- - spec/test_models.rb
100
95
  - spec/identity_map_spec.rb
101
96
  - spec/spec_helper.rb
data/spec/database.yml DELETED
@@ -1,21 +0,0 @@
1
- sqlite:
2
- :adapter: sqlite
3
- :database: vendor/plugins/identity_map/spec/db/identity_map_plugin.sqlite.db
4
-
5
- sqlite3:
6
- :adapter: sqlite3
7
- :database: vendor/plugins/identity_map/spec/db/identity_map_plugin.sqlite3.db
8
-
9
- postgresql:
10
- :adapter: postgresql
11
- :username: postgres
12
- :password: postgres
13
- :database: identity_map_plugin_test
14
- :min_messages: ERROR
15
-
16
- mysql:
17
- :adapter: mysql
18
- :host: localhost
19
- :username: root
20
- :password:
21
- :database: identity_map_plugin_test
data/spec/db/schema.rb DELETED
@@ -1,10 +0,0 @@
1
- ActiveRecord::Schema.define(:version => 0) do
2
- puts "Creating Schema"
3
- create_table :customers, :force => true do |t|
4
- t.string :name
5
- end
6
- create_table :phone_numbers, :force => true do |t|
7
- t.string :number
8
- t.integer :customer_id
9
- end
10
- end
data/spec/test_models.rb DELETED
@@ -1,13 +0,0 @@
1
- load_schema
2
-
3
- class Customer < ActiveRecord::Base
4
- has_many :phone_numbers
5
- end
6
-
7
- customer = Customer.create(:name => "Boneman")
8
-
9
- class PhoneNumber < ActiveRecord::Base
10
- belongs_to :customer
11
- end
12
-
13
- phone_number = customer.phone_numbers.create(:number => "8675309")