materialist 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f32f9a147b1122264cdef5eb29571ef0080c064c
4
- data.tar.gz: 8b1dee6b091fa976459723bf8adf096098d0e0fe
3
+ metadata.gz: 742d36bce66476aa11b5a36399c4fcdb7b6d4b48
4
+ data.tar.gz: 8f22cc1e86242ca63fd87145d5cbbd57b56f889c
5
5
  SHA512:
6
- metadata.gz: a9c94a53752b839970d4293870ab7de99bda93019e1df6e1320c574771173188d31fe27d801d0f3b0dffdce98a80f047bd1be311dde849759166e59a62226467
7
- data.tar.gz: 1204895096c3f37b369bac5219d6808b174acf0054cf7e32a66a4030d05d3d2b8b52d296d779445fb42babf3f26fdf438cffb2a27474e34afa3b4635afaeb48c
6
+ metadata.gz: 37148508f6fb84debd7e0975462164ba71f98855299fb8d9aa9f09eef7aba38dd21d5ddacf10bfbbb9496eb51204860f30ced33127c727c11e1c152c9d14ad8a
7
+ data.tar.gz: 4930c84c558fcb9d5fcc89353d4f4098d39958cbbe25bc4e811a9e1c9e3cc7b4f75143185f2d81246fd4417f6fdbd9f110f35aa228f92b731cd010586253fed7
data/README.md CHANGED
@@ -169,14 +169,14 @@ describes materializing the linked entity.
169
169
  This simulates a `:noop` event on the given topic and the `url` of the
170
170
  liked resource `<key>` as it appears on the response (`_links`) -- meaning the materializer for the given topic will be invoked.
171
171
 
172
- #### `after_upsert <method> (, <method>(, ...))` -- also `after_destroy`
173
- describes the name of the instance method(s) to be invoked after a record was materialized.
172
+ #### `before_upsert <method> (, <method>(, ...))` -- also `before_destroy`
173
+ describes the name of the instance method(s) to be invoked before a record is materialized, with the record as it exists in the database, or nil if it has not been created yet.
174
174
 
175
175
  ```ruby
176
176
  class ZoneMaterializer
177
177
  include Materialist::Materializer
178
178
 
179
- after_upsert :my_method, :my_second_method
179
+ before_upsert :my_method, :my_second_method
180
180
 
181
181
  def my_method(record)
182
182
  end
@@ -186,6 +186,11 @@ class ZoneMaterializer
186
186
  end
187
187
  ```
188
188
 
189
+
190
+ #### `after_upsert <method> (, <method>(, ...))` -- also `after_destroy`
191
+ describes the name of the instance method(s) to be invoked after a record was materialized, with the updated record as a parameter. See above for a similar example implementation.
192
+
193
+
189
194
  ### Materialized record
190
195
 
191
196
  Imagine you have materialized rider from a routemaster topic and you need to access a key from the remote source that you HAVEN'T materialized locally.
data/RELEASE_NOTES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 2.3.0
2
+
3
+ Features:
4
+ - Add `before_upsert` and `before_destroy` (#31)
5
+
1
6
  ## 2.2.0
2
7
 
3
8
  Features:
@@ -85,6 +85,10 @@ module Materialist
85
85
  __materialist_options[:url_parser] = url_parser_block
86
86
  end
87
87
 
88
+ def before_upsert(*method_array)
89
+ __materialist_options[:before_upsert] = method_array
90
+ end
91
+
88
92
  def after_upsert(*method_array)
89
93
  __materialist_options[:after_upsert] = method_array
90
94
  end
@@ -92,6 +96,10 @@ module Materialist
92
96
  def after_destroy(*method_array)
93
97
  __materialist_options[:after_destroy] = method_array
94
98
  end
99
+
100
+ def before_destroy(*method_array)
101
+ __materialist_options[:before_destroy] = method_array
102
+ end
95
103
  end
96
104
 
97
105
  class Materializer
@@ -126,6 +134,7 @@ module Materialist
126
134
  def destroy
127
135
  return unless materialize_self?
128
136
  model_class.find_by(source_lookup(url)).tap do |entity|
137
+ send_messages(before_destroy, entity) unless before_destroy.nil?
129
138
  entity.destroy!.tap do |entity|
130
139
  send_messages(after_destroy, entity) unless after_destroy.nil?
131
140
  end if entity
@@ -142,6 +151,7 @@ module Materialist
142
151
 
143
152
  def upsert_record
144
153
  model_class.find_or_initialize_by(source_lookup(url)).tap do |entity|
154
+ send_messages(before_upsert, entity) unless before_upsert.nil?
145
155
  entity.update_attributes attributes
146
156
  entity.save!
147
157
  end
@@ -169,10 +179,18 @@ module Materialist
169
179
  options.fetch :mapping
170
180
  end
171
181
 
182
+ def before_upsert
183
+ options[:before_upsert]
184
+ end
185
+
172
186
  def after_upsert
173
187
  options[:after_upsert]
174
188
  end
175
189
 
190
+ def before_destroy
191
+ options[:before_destroy]
192
+ end
193
+
176
194
  def after_destroy
177
195
  options[:after_destroy]
178
196
  end
data/lib/materialist.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Materialist
2
- VERSION = '2.2.0'
2
+ VERSION = '2.3.0'
3
3
  end
@@ -274,38 +274,54 @@ RSpec.describe Materialist::Materializer do
274
274
  end
275
275
  end
276
276
 
277
- context "when after_upsert is configured" do
277
+ context "when {after, before}_upsert is configured" do
278
278
  let!(:record) { Foobar.create!(source_url: source_url, name: 'mo') }
279
279
  let!(:materializer_class) do
280
280
  class FoobarMaterializer
281
281
  include Materialist::Materializer
282
282
 
283
283
  persist_to :foobar
284
+ before_upsert :my_before_method
284
285
  after_upsert :my_method
285
286
 
286
287
  def my_method(entity)
287
288
  entity.actions_called[:after_upsert] = true
288
289
  end
290
+
291
+ def my_before_method(entity)
292
+ entity.actions_called[:before_upsert] = true
293
+ end
289
294
  end
290
295
  end
291
296
 
292
297
  %i(create update noop).each do |action_name|
293
298
  context "when action is :#{action_name}" do
294
299
  let(:action) { action_name }
300
+ it "calls before_upsert method" do
301
+ expect{ perform }.to change { record.actions_called[:before_upsert] }
302
+ end
303
+
295
304
  it "calls after_upsert method" do
296
305
  expect{ perform }.to change { record.actions_called[:after_upsert] }
297
306
  end
298
307
 
299
- it "calls more than one after_upsert method" do
308
+ it "calls more than one method" do
300
309
  class FoobarMaterializer
310
+ before_upsert :my_before_method, :my_before_method2
301
311
  after_upsert :my_method, :my_method2
302
312
 
303
313
  def my_method2(entity)
304
314
  entity.actions_called[:after_upsert2] = true
305
315
  end
316
+
317
+ def my_before_method2(entity)
318
+ entity.actions_called[:before_upsert2] = true
319
+ end
306
320
  end
307
- expect{ perform }.to change { record.actions_called[:after_upsert] }
308
- .and change { record.actions_called[:after_upsert2] }
321
+ expect{ perform }.to change { record.actions_called[:after_upsert] }
322
+ .and change { record.actions_called[:after_upsert2] }
323
+ .and change { record.actions_called[:before_upsert] }
324
+ .and change { record.actions_called[:before_upsert2] }
309
325
  end
310
326
  end
311
327
  end
@@ -316,22 +332,31 @@ RSpec.describe Materialist::Materializer do
316
332
  it "does not call after_upsert method" do
317
333
  expect{ perform }.to_not change { record.actions_called[:after_upsert] }
318
334
  end
335
+
336
+ it "does call after_upsert method" do
337
+ expect{ perform }.to_not change { record.actions_called[:before_upsert] }
338
+ end
319
339
  end
320
340
 
321
341
  end
322
342
 
323
- context "when after_destroy is configured" do
343
+ context "when {before, after}_destroy is configured" do
324
344
  let!(:record) { Foobar.create!(source_url: source_url, name: 'mo') }
325
345
  let!(:materializer_class) do
326
346
  class FoobarMaterializer
327
347
  include Materialist::Materializer
328
348
 
329
349
  persist_to :foobar
350
+ before_destroy :my_before_method
330
351
  after_destroy :my_method
331
352
 
332
353
  def my_method(entity)
333
354
  entity.actions_called[:after_destroy] = true
334
355
  end
356
+
357
+ def my_before_method(entity)
358
+ entity.actions_called[:before_destroy] = true unless entity.nil?
359
+ end
335
360
  end
336
361
  end
337
362
 
@@ -341,6 +366,10 @@ RSpec.describe Materialist::Materializer do
341
366
  it "does not call after_destroy method" do
342
367
  expect{ perform }.to_not change { record.actions_called[:after_destroy] }
343
368
  end
369
+
370
+ it "does not call before_destroy method" do
371
+ expect{ perform }.to_not change { record.actions_called[:before_destroy] }
372
+ end
344
373
  end
345
374
  end
346
375
 
@@ -351,16 +380,27 @@ RSpec.describe Materialist::Materializer do
351
380
  expect{ perform }.to change { record.actions_called[:after_destroy] }
352
381
  end
353
382
 
354
- it "calls more than one after_destroy method" do
383
+ it "calls before_destroy method" do
384
+ expect{ perform }.to change { record.actions_called[:before_destroy] }
385
+ end
386
+
387
+ it "calls more than one method" do
355
388
  class FoobarMaterializer
389
+ before_destroy :my_before_method, :my_before_method2
356
390
  after_destroy :my_method, :my_method2
357
391
 
358
392
  def my_method2(entity)
359
393
  entity.actions_called[:after_destroy2] = true
360
394
  end
395
+
396
+ def my_before_method2(entity)
397
+ entity.actions_called[:before_destroy2] = true unless entity.nil?
398
+ end
361
399
  end
362
- expect{ perform }.to change { record.actions_called[:after_destroy] }
363
- .and change { record.actions_called[:after_destroy2] }
400
+ expect{ perform }.to change { record.actions_called[:after_destroy] }
401
+ .and change { record.actions_called[:after_destroy2] }
402
+ .and change { record.actions_called[:before_destroy] }
403
+ .and change { record.actions_called[:before_destroy2] }
364
404
  end
365
405
 
366
406
  context "when resource doesn't exist locally" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: materialist
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mo Valipour
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-16 00:00:00.000000000 Z
11
+ date: 2017-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq