materialist 3.6.0 → 3.7.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
- SHA1:
3
- metadata.gz: dc140c911742867585c275c7b87a08cce340b165
4
- data.tar.gz: dd08e90c55127a4b8375f859613ec73cda95970d
2
+ SHA256:
3
+ metadata.gz: a7a241994e95f26afa7c395df4780b3ff7a2b0dc3063498e97a73f5a36ecb008
4
+ data.tar.gz: 014d469aec8cffe93f23d8aafb3036a8d4dab62c95c12f400bad372e4a9fb438
5
5
  SHA512:
6
- metadata.gz: 4bcb195f30deff05ab9c83de55475473f8efee6a599cb694f47fdb55438b5374988a6f12872d5078af57c76c978d45208f0829a962b1d41d79f9ef1c824740eb
7
- data.tar.gz: 0d15e15c152b85765a3dca6c96dbe28787e780a15b68c505cf4dfd6d0c4e1b732c17c64275794733a096790999e3a0d59be32ba9e48a8c7d374c5fd1b4c6e33b
6
+ metadata.gz: acc437009c5508649e436e6bf4bc7d12fedf2d6996c69d8a6e26608c62f84adf70507a37b9ac1ff58f765e676fe3ab005d5305dc9793147d1dc4e98b3f23cf36
7
+ data.tar.gz: 16136c16315f64400870154db95092ade08d595f8c916f145df33c887b27b8fb89bcb91408c621a7fe7a0b9982f74e28fd66fc90afe7c7589f37d1f7d8c651e5
data/README.md CHANGED
@@ -91,7 +91,7 @@ Materialist.configure do |config|
91
91
  end
92
92
  ```
93
93
 
94
- - `topics` (only when using in `.subscribe`): A string array of topics to be used.
94
+ - `topics` (only when using in `.subscribe`): A string array of topics to be used.
95
95
  If not provided nothing would be materialized.
96
96
  - `sidekiq_options` (optional, default: `{ retry: 10 }`) -- See [Sidekiq docs](https://github.com/mperham/sidekiq/wiki/Advanced-Options#workers) for list of options
97
97
  - `api_client` (optional) -- You can pass your `Routemaster::APIClient` instance
@@ -243,6 +243,24 @@ class ZoneMaterializer
243
243
  end
244
244
  ```
245
245
 
246
+ #### `before_upsert_with_payload <method> (, <method>(, ...))`
247
+ describes the name of the instance method(s) to be invoked before a record is
248
+ materialized, with the record as it exists in the database, or nil if it has
249
+ not been created yet. The function will get as a second argument the `payload`
250
+ of the HTTP response, this can be used to add additional information/persist
251
+ other objects.
252
+
253
+
254
+ ```ruby
255
+ class ZoneMaterializer
256
+ include Materialist::Materializer
257
+
258
+ before_upsert_with_payload :my_method
259
+
260
+ def my_method(record, payload); end
261
+ end
262
+ ```
263
+
246
264
 
247
265
  #### `after_upsert <method> (, <method>(, ...))` -- also `after_destroy`
248
266
  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.
@@ -43,6 +43,15 @@ module Materialist
43
43
  __materialist_options[:url_parser] = url_parser_block
44
44
  end
45
45
 
46
+ # This method is meant to be used for cases when the application needs
47
+ # to have access to the `payload` that is returned on the HTTP call.
48
+ # Such an example would be if the application logic requires all
49
+ # relationships to be present before the `resource` is saved in the
50
+ # database. Introduced in https://github.com/deliveroo/materialist/pull/47
51
+ def before_upsert_with_payload(*method_array)
52
+ __materialist_options[:before_upsert_with_payload] = method_array
53
+ end
54
+
46
55
  def before_upsert(*method_array)
47
56
  __materialist_options[:before_upsert] = method_array
48
57
  end
@@ -61,6 +61,7 @@ module Materialist
61
61
  def upsert_record
62
62
  model_class.find_or_initialize_by(source_lookup(url)).tap do |entity|
63
63
  send_messages(before_upsert, entity) unless before_upsert.nil?
64
+ before_upsert_with_payload&.each { |m| instance.send(m, entity, resource) }
64
65
  entity.update_attributes!(attributes)
65
66
  end
66
67
  end
@@ -74,7 +75,6 @@ module Materialist
74
75
  return unless link = resource.dig(:_links, key)
75
76
  return unless materializer_class = MaterializerFactory.class_from_topic(opts.fetch(:topic))
76
77
 
77
- # TODO: perhaps consider doing this asynchronously some how?
78
78
  materializer_class.perform(link[:href], :noop)
79
79
  end
80
80
 
@@ -86,6 +86,10 @@ module Materialist
86
86
  options[:before_upsert]
87
87
  end
88
88
 
89
+ def before_upsert_with_payload
90
+ options[:before_upsert_with_payload]
91
+ end
92
+
89
93
  def after_upsert
90
94
  options[:after_upsert]
91
95
  end
@@ -1,3 +1,3 @@
1
1
  module Materialist
2
- VERSION = '3.6.0'
2
+ VERSION = '3.7.0'
3
3
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = %w(lib)
18
18
 
19
19
  spec.add_runtime_dependency 'sidekiq', '>= 5.0'
20
- spec.add_runtime_dependency 'activesupport'
20
+ spec.add_runtime_dependency 'activesupport', '< 6.0'
21
21
  spec.add_runtime_dependency 'routemaster-drain', '>= 3.0'
22
22
 
23
23
  spec.add_development_dependency 'activerecord'
@@ -199,8 +199,13 @@ RSpec.describe Materialist::Materializer::Internals::Materializer do
199
199
 
200
200
  persist_to :foobar
201
201
  before_upsert :before_hook
202
+ before_upsert_with_payload :before_hook_with_payload
202
203
  after_upsert :after_hook
203
204
 
205
+ def before_hook_with_payload(entity, payload)
206
+ self.actions_called[:before_hook_with_payload] = true
207
+ end
208
+
204
209
  def before_hook(entity); self.actions_called[:before_hook] = true; end
205
210
  def after_hook(entity); self.actions_called[:after_hook] = true; end
206
211
  end
@@ -209,6 +214,11 @@ RSpec.describe Materialist::Materializer::Internals::Materializer do
209
214
  %i(create update noop).each do |action_name|
210
215
  context "when action is :#{action_name}" do
211
216
  let(:action) { action_name }
217
+
218
+ it "calls before_upsert_with_payload method" do
219
+ expect{ perform }.to change { actions_called[:before_hook_with_payload] }
220
+ end
221
+
212
222
  it "calls before_upsert method" do
213
223
  expect{ perform }.to change { actions_called[:before_hook] }
214
224
  end
@@ -225,8 +235,16 @@ RSpec.describe Materialist::Materializer::Internals::Materializer do
225
235
 
226
236
  persist_to :foobar
227
237
  before_upsert :before_hook, :before_hook2
238
+ before_upsert_with_payload :before_hook_with_payload, :before_hook_with_payload2
228
239
  after_upsert :after_hook, :after_hook2
229
240
 
241
+ def before_hook_with_payload(entity, payload)
242
+ self.actions_called[:before_hook_with_payload] = true
243
+ end
244
+ def before_hook_with_payload2(entity, payload)
245
+ self.actions_called[:before_hook_with_payload2] = true
246
+ end
247
+
230
248
  def before_hook(entity); self.actions_called[:before_hook] = true; end
231
249
  def before_hook2(entity); self.actions_called[:before_hook2] = true; end
232
250
  def after_hook(entity); self.actions_called[:after_hook] = true; end
@@ -239,6 +257,8 @@ RSpec.describe Materialist::Materializer::Internals::Materializer do
239
257
  .and change { actions_called[:before_hook2] }
240
258
  .and change { actions_called[:after_hook] }
241
259
  .and change { actions_called[:after_hook2] }
260
+ .and change { actions_called[:before_hook_with_payload] }
261
+ .and change { actions_called[:before_hook_with_payload2] }
242
262
  end
243
263
  end
244
264
  end
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: 3.6.0
4
+ version: 3.7.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: 2019-06-12 00:00:00.000000000 Z
11
+ date: 2019-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "<"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '6.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "<"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '6.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: routemaster-drain
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -247,8 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
247
  - !ruby/object:Gem::Version
248
248
  version: '0'
249
249
  requirements: []
250
- rubyforge_project:
251
- rubygems_version: 2.6.14.1
250
+ rubygems_version: 3.0.6
252
251
  signing_key:
253
252
  specification_version: 4
254
253
  summary: Utilities to materialize routemaster topics