tire_async_index 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee879c3935e505e60740944e6c5b2227eaeff745
4
- data.tar.gz: ae62e2506ff7edd585553ea10320c12c1683b11f
3
+ metadata.gz: b862d7239b23af431da5f68fc6569e8e2ab79555
4
+ data.tar.gz: 3ac36b477df231387b642c84622041f5a17feff0
5
5
  SHA512:
6
- metadata.gz: 1a60f3de5dc39526a7128dbd38e76618db72c629ee1d71ca27453e95663a7d41ee7e3b461a300ac80cf49871df94586850c9b5f1367eaf2f7c324e6ba130b7a6
7
- data.tar.gz: 676449173b46273ffa10fd6839cc4514b8aedefcb9c7bd5b363268fd8ecf5fea552f67294ed6ab59327563f3ae59bb9d19b9066d53c9b677dab9317e83c4da4e
6
+ metadata.gz: 99db27b1eb4445aa70225b45fa0d34664775ad1d7dc2cebe3ad49101acce707b981b941b9819e812ef5ce9eea7dc2a8fb1f826d65b12cbf304a19a0f337aa32b
7
+ data.tar.gz: 70157720a282e54a349e8b9df1a304370ab2f861bea42d5c07d72de9f51779c46e3cd6cac1cc1ff966cf62f8f76fad35c3d33d39fe57515abd1a5366e09177cb
@@ -1,3 +1,8 @@
1
+ ## 0.3.0
2
+
3
+ * refactor loading structure
4
+ * custom identificator & finders
5
+
1
6
  ## 0.2.1
2
7
 
3
8
  * added `find_class_const` for full suuport of rubies (@we4tech)
data/README.md CHANGED
@@ -8,10 +8,8 @@ Requirements: Ruby 1.9, 2.0, Rails => 3.0
8
8
 
9
9
  ## Installation
10
10
 
11
- Add this line to your application's Gemfile after `tire` and `sidekiq` or `resque` gems:
11
+ Add this line to your application's Gemfile:
12
12
 
13
- gem 'tire'
14
- gem 'sidekiq' #'resque'
15
13
  gem 'tire_async_index'
16
14
 
17
15
  And then execute:
@@ -44,6 +42,23 @@ Just add AsyncCallbacks to your model:
44
42
 
45
43
  That's all.
46
44
 
45
+ ## Custom identificator or finder
46
+
47
+ If you need more complex solution to define identificators and/or finder. You could simply add methods to override default:
48
+
49
+ class User < ActiveRecord::Base
50
+ include Tire::Model::Search
51
+ include Tire::Model::AsyncCallbacks
52
+
53
+ def self.tire_async_finder(id)
54
+ User.where(...).first
55
+ end
56
+
57
+ def async_tire_object_id
58
+ "#{id}-#{name}"
59
+ end
60
+ end
61
+
47
62
  ## TODO
48
63
 
49
64
  * Add support for custom filter / custom finders
@@ -32,30 +32,27 @@ module Tire
32
32
  end
33
33
 
34
34
  def async_tire_callback(type)
35
- case TireAsyncIndex.engine
36
- when :sidekiq
37
- TireAsyncIndex::Workers::SidekiqUpdateIndex.
38
- perform_async type, self.class.name, async_tire_object_id
39
-
40
- when :resque
41
- Resque.enqueue TireAsyncIndex::Workers::ResqueUpdateIndex,
42
- type, self.class.name, async_tire_object_id
43
-
44
- else
45
- case type
46
- when :update
47
- tire.update_index
48
- when :delete
49
- tire.index.remove self
50
- end
35
+ if TireAsyncIndex.engine == :none
36
+ case type
37
+ when :update
38
+ tire.update_index
39
+ when :delete
40
+ tire.index.remove self
41
+ end
42
+ else
43
+ TireAsyncIndex.worker.run(type, self.class.name, get_async_tire_object_id)
51
44
  end
52
45
  end
53
46
 
54
- def async_tire_object_id
55
- if (method = ID_CONVERSION[self.id.class.name])
56
- self.id.send(method)
47
+ def get_async_tire_object_id
48
+ if self.respond_to?(:async_tire_object_id)
49
+ self.send(:async_tire_object_id)
57
50
  else
58
- self.id
51
+ if (method = ID_CONVERSION[self.id.class.name])
52
+ self.id.send(method)
53
+ else
54
+ self.id
55
+ end
59
56
  end
60
57
  end
61
58
 
@@ -9,7 +9,6 @@ module TireAsyncIndex
9
9
  def configure
10
10
  self.configuration ||= Configuration.new
11
11
  yield(configuration)
12
- reconfig_workers
13
12
  end
14
13
 
15
14
  def queue
@@ -20,20 +19,25 @@ module TireAsyncIndex
20
19
  self.configuration.engine
21
20
  end
22
21
 
23
- def reconfig_workers
24
- if defined?(Sidekiq)
25
- Workers::SidekiqUpdateIndex.sidekiq_options_hash["queue"] = TireAsyncIndex.queue
22
+ def worker
23
+ case configuration.engine
24
+ when :sidekiq
25
+ TireAsyncIndex::Workers::Sidekiq
26
+ when :resque
27
+ TireAsyncIndex::Workers::Resque
28
+ else
29
+ TireAsyncIndex::Workers::UpdateIndex
26
30
  end
27
31
  end
28
32
 
29
33
  module Workers
30
34
  autoload :UpdateIndex, 'tire_async_index/workers/update_index'
35
+ autoload :Sidekiq, 'tire_async_index/workers/sidekiq'
36
+ autoload :Resque, 'tire_async_index/workers/resque'
31
37
  end
32
38
 
33
39
  end
34
40
 
35
- require 'tire_async_index/workers/sidekiq_update_index' if defined?(Sidekiq)
36
- require 'tire_async_index/workers/resque_update_index' if defined?(Resque)
37
41
  require 'tire/model/async_callbacks'
38
42
 
39
43
  TireAsyncIndex.configure {}
@@ -1,3 +1,3 @@
1
1
  module TireAsyncIndex
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,15 @@
1
+ module TireAsyncIndex
2
+ module Workers
3
+ class Resque < UpdateIndex
4
+ def self.queue; TireAsyncIndex.queue end
5
+
6
+ def self.run(action_type, class_name, id)
7
+ ::Resque.enqueue TireAsyncIndex::Workers::Resque, action_type, class_name, id
8
+ end
9
+
10
+ def self.perform(action_type, class_name, id)
11
+ self.new.process(action_type, class_name, id)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module TireAsyncIndex
2
+ module Workers
3
+ class Sidekiq < UpdateIndex
4
+ include ::Sidekiq::Worker
5
+ sidekiq_options queue: TireAsyncIndex.queue
6
+
7
+ def self.run(action_type, class_name, id)
8
+ TireAsyncIndex::Workers::Sidekiq
9
+ .perform_async(action_type, class_name, id)
10
+ end
11
+
12
+ def perform(action_type, class_name, id)
13
+ process(action_type, class_name, id)
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -11,12 +11,13 @@ module TireAsyncIndex
11
11
  # class_name - Model class name
12
12
  # id - Document id
13
13
  #
14
- def perform(action_type, class_name, id)
14
+
15
+ def process(action_type, class_name, id)
15
16
  klass = find_class_const(class_name)
16
17
 
17
18
  case action_type.to_sym
18
19
  when :update
19
- object = klass.find(id)
20
+ object = klass.send(get_finder_method(klass), id)
20
21
 
21
22
  if object.present? && object.respond_to?(:tire)
22
23
  object.tire.update_index
@@ -38,6 +39,10 @@ module TireAsyncIndex
38
39
  end
39
40
  end
40
41
  end
42
+
43
+ def get_finder_method(klass)
44
+ klass.respond_to?(:tire_async_finder) ? :tire_async_finder : :find
45
+ end
41
46
  end
42
47
  end
43
48
  end
@@ -86,7 +86,7 @@ describe TireAsyncIndex do
86
86
  config.background_engine :sidekiq
87
87
  end
88
88
 
89
- TireAsyncIndex::Workers::SidekiqUpdateIndex.should_receive(:perform_async).with(:update, "AmUser", instance_of(Fixnum))
89
+ TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:update, "AmUser", instance_of(Fixnum))
90
90
 
91
91
  AmUser.new.tap { |a| a.id = 23 }.save
92
92
  end
@@ -96,7 +96,7 @@ describe TireAsyncIndex do
96
96
  config.background_engine :resque
97
97
  end
98
98
 
99
- Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::ResqueUpdateIndex, :update, "AmUser", instance_of(Fixnum))
99
+ Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :update, "AmUser", instance_of(Fixnum))
100
100
 
101
101
  AmUser.new.tap { |a| a.id = 23 }.save
102
102
  end
@@ -122,7 +122,7 @@ describe TireAsyncIndex do
122
122
  config.background_engine :sidekiq
123
123
  end
124
124
 
125
- TireAsyncIndex::Workers::SidekiqUpdateIndex.should_receive(:perform_async).with(:delete, "AmUser", instance_of(Fixnum))
125
+ TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:delete, "AmUser", instance_of(Fixnum))
126
126
 
127
127
  AmUser.new.tap { |a| a.id = 23 }.destroy
128
128
  end
@@ -132,11 +132,11 @@ describe TireAsyncIndex do
132
132
  config.background_engine :resque
133
133
  end
134
134
 
135
- Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::ResqueUpdateIndex, :delete, "AmUser", instance_of(Fixnum))
135
+ Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :delete, "AmUser", instance_of(Fixnum))
136
136
 
137
137
  AmUser.new.tap { |a| a.id = 23 }.destroy
138
138
  end
139
139
  end
140
140
 
141
141
  end
142
- end
142
+ end
@@ -8,6 +8,14 @@ class ArUser < ActiveRecord::Base
8
8
  include Tire::Model::AsyncCallbacks
9
9
  end
10
10
 
11
+ class ArIdUser < ActiveRecord::Base
12
+ include Tire::Model::AsyncCallbacks
13
+
14
+ def async_tire_object_id
15
+ "text_id"
16
+ end
17
+ end
18
+
11
19
  describe TireAsyncIndex do
12
20
 
13
21
  before(:all) do
@@ -72,7 +80,7 @@ describe TireAsyncIndex do
72
80
  config.background_engine :sidekiq
73
81
  end
74
82
 
75
- TireAsyncIndex::Workers::SidekiqUpdateIndex.should_receive(:perform_async).with(:update, "ArUser", instance_of(Fixnum))
83
+ TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:update, "ArUser", instance_of(Fixnum))
76
84
 
77
85
  a = ArUser.new
78
86
  a.save
@@ -83,11 +91,33 @@ describe TireAsyncIndex do
83
91
  config.background_engine :resque
84
92
  end
85
93
 
86
- Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::ResqueUpdateIndex, :update, "ArUser", instance_of(Fixnum))
94
+ Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :update, "ArUser", instance_of(Fixnum))
87
95
 
88
96
  a = ArUser.new
89
97
  a.save
90
98
  end
99
+
100
+ it "should start sidekiq with custom id" do
101
+ TireAsyncIndex.configure do |config|
102
+ config.background_engine :sidekiq
103
+ end
104
+
105
+ TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:update, "ArIdUser", "text_id")
106
+
107
+ a = ArIdUser.new
108
+ a.save
109
+ end
110
+
111
+ it "should start resque with custom id" do
112
+ TireAsyncIndex.configure do |config|
113
+ config.background_engine :resque
114
+ end
115
+
116
+ Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :update, "ArIdUser", "text_id")
117
+
118
+ a = ArIdUser.new
119
+ a.save
120
+ end
91
121
  end
92
122
 
93
123
  describe '#after_destroy' do
@@ -110,7 +140,7 @@ describe TireAsyncIndex do
110
140
  config.background_engine :sidekiq
111
141
  end
112
142
 
113
- TireAsyncIndex::Workers::SidekiqUpdateIndex.should_receive(:perform_async).with(:delete, "ArUser", instance_of(Fixnum))
143
+ TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:delete, "ArUser", instance_of(Fixnum))
114
144
 
115
145
  ArUser.new.tap { |a| a.id = 23 }.destroy
116
146
  end
@@ -120,11 +150,11 @@ describe TireAsyncIndex do
120
150
  config.background_engine :resque
121
151
  end
122
152
 
123
- Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::ResqueUpdateIndex, :delete, "ArUser", instance_of(Fixnum))
153
+ Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :delete, "ArUser", instance_of(Fixnum))
124
154
 
125
155
  ArUser.new.tap { |a| a.id = 23 }.destroy
126
156
  end
127
157
  end
128
158
 
129
159
  end
130
- end
160
+ end
@@ -4,4 +4,4 @@ ActiveRecord::Schema.define(version: 20130315203352) do
4
4
  t.datetime "updated_at"
5
5
  t.string "name"
6
6
  end
7
- end
7
+ end
@@ -11,16 +11,38 @@ module Level1
11
11
  end
12
12
  end
13
13
 
14
+ class ArUser < ActiveRecord::Base
15
+ include Tire::Model::AsyncCallbacks
16
+ end
17
+
18
+ class ArUserFinder < ActiveRecord::Base
19
+ include Tire::Model::AsyncCallbacks
20
+
21
+ def self.tire_async_finder(id)
22
+ nil
23
+ end
24
+ end
25
+
14
26
  describe TireAsyncIndex::Workers::UpdateIndex do
15
27
 
16
- describe '#perform' do
28
+ describe '#process' do
17
29
  let(:instance) { described_class.new }
18
30
 
19
31
  it 'resolves class constant' do
20
32
  expect {
21
- instance.perform(:nothing, 'Level1::Level2::Model', 123)
33
+ instance.process(:nothing, 'Level1::Level2::Model', 123)
22
34
  }.not_to raise_error
23
35
  end
36
+
37
+ it 'trigger find on simple ar model' do
38
+ ArUser.should_receive(:find).with(123).and_return(nil)
39
+ instance.process(:update, 'ArUser', 123)
40
+ end
41
+
42
+ it 'trigger finder on ar mode with custom finder' do
43
+ ArUserFinder.should_receive(:tire_async_finder).with(123).and_return(nil)
44
+ instance.process(:update, 'ArUserFinder', 123)
45
+ end
24
46
  end
25
47
 
26
- end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tire_async_index
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Efremov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-21 00:00:00.000000000 Z
11
+ date: 2013-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tire
@@ -142,8 +142,8 @@ files:
142
142
  - lib/tire_async_index/configuration.rb
143
143
  - lib/tire_async_index/exceptions.rb
144
144
  - lib/tire_async_index/version.rb
145
- - lib/tire_async_index/workers/resque_update_index.rb
146
- - lib/tire_async_index/workers/sidekiq_update_index.rb
145
+ - lib/tire_async_index/workers/resque.rb
146
+ - lib/tire_async_index/workers/sidekiq.rb
147
147
  - lib/tire_async_index/workers/update_index.rb
148
148
  - spec/active_model_tire_async_index_spec.rb
149
149
  - spec/active_record_tire_async_index_spec.rb
@@ -1,11 +0,0 @@
1
- module TireAsyncIndex
2
- module Workers
3
- class ResqueUpdateIndex < UpdateIndex
4
- def self.queue; TireAsyncIndex.queue end
5
-
6
- def self.perform(*args)
7
- self.new.perform *args
8
- end
9
- end
10
- end
11
- end
@@ -1,8 +0,0 @@
1
- module TireAsyncIndex
2
- module Workers
3
- class SidekiqUpdateIndex < UpdateIndex
4
- include Sidekiq::Worker
5
- sidekiq_options queue: :normal
6
- end
7
- end
8
- end