graphiti 1.0.rc.19 → 1.0.rc.21

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
- SHA1:
3
- metadata.gz: d8967c814b3b30a9726dbba1c4a842678583488b
4
- data.tar.gz: da6879ff4f72a21e08ee468473271d6342329d5a
2
+ SHA256:
3
+ metadata.gz: d9aba6937773f5fccd9d4728c4b9d359e1edc94aa160ec72fab31df0068d1a2c
4
+ data.tar.gz: 1d39ffdcb8c9ed1c543b2bac2c00ccf4c2814831a237369a2e56bb7ff65d2de2
5
5
  SHA512:
6
- metadata.gz: 9d30c8a71c22c4e1d9628dd06a5ed14ca42c63bbe727bb0807b6af5e3ae59b0eede5d9cd38c4ad85a9fa62144685e0173f6c98490d72d05a1405489cdb1f3fcd
7
- data.tar.gz: 8abc3efc907913886f694e9ab165bf3492955984cf9467b11d757604429dcbccf7faf06d94f2b9096aa17ec4a170eeec23ae081100effe590339489e53ac9b52
6
+ metadata.gz: e6e646ddf4069ee3563a8500291649fd62763594c54068bf50a2b044ca82696794a0de8aa40e5ea38e398de7808ffc853736a0978c2ccc56030cdadc0965ad4c
7
+ data.tar.gz: 3e294b2980ca2ab4f117d6e2ef888df7d37a529db9ac1491d44e349f7dd8582f677d2bbb291a40814e0ecc0c2360b2e63a711923ccfe088cd53c631535bd7127
@@ -4,16 +4,16 @@ rvm:
4
4
  - 2.4
5
5
  - 2.5
6
6
  - 2.6
7
-
8
- script: bundle exec rspec && bundle exec appraisal install && bundle exec appraisal rails-5 rspec && bundle exec appraisal rails-4 rspec
7
+ script: bundle exec rspec && bundle exec appraisal install && bundle exec appraisal
8
+ rails-5 rspec && bundle exec appraisal rails-4 rspec
9
9
  install: bundle install --retry=3 --jobs=3
10
-
11
10
  before_install:
12
11
  - gem install bundler -v '< 2'
13
-
14
12
  deploy:
15
13
  provider: rubygems
16
14
  api_key:
17
- secure: Wb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DLbbsVJNHYycGOLclqQq1aNbgz9HayEE5s+AyklPcprJhngxA5WP80AtONAxxLt4OjrY0X80TvOi2b5nfIN7DowGmcLbRahnms98Yk6/i1vCNV4R9bO0AaqpGLV+L8sVp98Qquk2mpvB3Le4W89qm5int1y2FNJbgInBpY1UOsI3wLENt0npC1X/KVMJKkpHvk80rGccI5FIVRUDZ3YD77hi+s1busPSVQ/+l4QdTFHmWp13Pghf6hNaIwfNwFMHwM2Fkv8YXa4c1mWc1cY5WP1ELS8cEnjtmp/9Iuc=
16
+ gem: graphiti
18
17
  on:
19
18
  tags: true
19
+ repo: graphiti-api/graphiti
@@ -61,7 +61,7 @@ require "graphiti/util/relationship_payload"
61
61
  require "graphiti/util/persistence"
62
62
  require "graphiti/util/validation_response"
63
63
  require "graphiti/util/sideload"
64
- require "graphiti/util/hooks"
64
+ require "graphiti/util/transaction_hooks_recorder"
65
65
  require "graphiti/util/attribute_check"
66
66
  require "graphiti/util/serializer_attributes"
67
67
  require "graphiti/util/serializer_relationships"
@@ -121,6 +121,13 @@ module Graphiti
121
121
  end
122
122
  end
123
123
 
124
+ def after_commit(model, metadata)
125
+ hooks = self.class.config[:after_commit][metadata[:method]] || []
126
+ hooks.each do |hook|
127
+ instance_exec(model, metadata, &hook)
128
+ end
129
+ end
130
+
124
131
  def transaction
125
132
  response = nil
126
133
  begin
@@ -128,7 +135,7 @@ module Graphiti
128
135
  response = yield
129
136
  end
130
137
  rescue Errors::ValidationError => e
131
- response = e.validation_response
138
+ response = { result: e.validation_response }
132
139
  end
133
140
  response
134
141
  end
@@ -181,6 +181,7 @@ module Graphiti
181
181
  sorts: {},
182
182
  pagination: nil,
183
183
  before_commit: {},
184
+ after_commit: {},
184
185
  attributes: {},
185
186
  extra_attributes: {},
186
187
  sideloads: {},
@@ -87,6 +87,13 @@ module Graphiti
87
87
  end
88
88
  end
89
89
 
90
+ def after_commit(only: [:create, :update, :destroy], &blk)
91
+ Array(only).each do |verb|
92
+ config[:after_commit][verb] ||= []
93
+ config[:after_commit][verb] << blk
94
+ end
95
+ end
96
+
90
97
  def attribute(name, type, options = {}, &blk)
91
98
  raise Errors::TypeNotFound.new(self, name, type) unless Types[type]
92
99
  attribute_option(options, :readable)
@@ -114,7 +114,7 @@ module Graphiti
114
114
  end
115
115
 
116
116
  def destroy
117
- validator = @resource.transaction do
117
+ transaction_response = @resource.transaction do
118
118
  metadata = { method: :destroy }
119
119
  model = @resource.destroy(@query.filters[:id], metadata)
120
120
  model.instance_variable_set(:@__serializer_klass, @resource.serializer)
@@ -122,9 +122,10 @@ module Graphiti
122
122
  model, @payload
123
123
  validator.validate!
124
124
  @resource.before_commit(model, metadata)
125
- validator
125
+
126
+ { result: validator }
126
127
  end
127
- @data, success = validator.to_a
128
+ @data, success = transaction_response[:result].to_a
128
129
  success
129
130
  end
130
131
 
@@ -154,8 +155,8 @@ module Graphiti
154
155
  private
155
156
 
156
157
  def persist
157
- @resource.transaction do
158
- ::Graphiti::Util::Hooks.record do
158
+ transaction_response = @resource.transaction do
159
+ ::Graphiti::Util::TransactionHooksRecorder.record do
159
160
  model = yield
160
161
  validator = ::Graphiti::Util::ValidationResponse.new \
161
162
  model, @payload
@@ -163,6 +164,15 @@ module Graphiti
163
164
  validator
164
165
  end
165
166
  end
167
+
168
+ data, success = transaction_response[:result].to_a
169
+ if success
170
+ transaction_response[:after_commit_hooks].each do |hook|
171
+ hook.call
172
+ end
173
+ end
174
+
175
+ transaction_response[:result]
166
176
  end
167
177
  end
168
178
  end
@@ -63,7 +63,9 @@ class Graphiti::Util::Persistence
63
63
  post_process(persisted, parents)
64
64
  post_process(persisted, children)
65
65
  before_commit = -> { @resource.before_commit(persisted, metadata) }
66
- add_hook(before_commit)
66
+ add_hook(before_commit, :before_commit)
67
+ after_commit = -> { @resource.after_commit(persisted, metadata) }
68
+ add_hook(after_commit, :after_commit)
67
69
  persisted
68
70
  end
69
71
 
@@ -83,8 +85,8 @@ class Graphiti::Util::Persistence
83
85
  end
84
86
  end
85
87
 
86
- def add_hook(prc)
87
- ::Graphiti::Util::Hooks.add(prc)
88
+ def add_hook(prc, lifecycle_event)
89
+ ::Graphiti::Util::TransactionHooksRecorder.add(prc, lifecycle_event)
88
90
  end
89
91
 
90
92
  # The child's attributes should be modified to nil-out the
@@ -215,7 +217,7 @@ class Graphiti::Util::Persistence
215
217
  group.group_by { |g| g[:sideload] }.each_pair do |sideload, members|
216
218
  objects = members.map { |x| x[:object] }
217
219
  hook = -> { sideload.fire_hooks!(caller_model, objects, method) }
218
- add_hook(hook)
220
+ add_hook(hook, :before_commit)
219
221
  end
220
222
  end
221
223
  end
@@ -0,0 +1,72 @@
1
+ module Graphiti
2
+ module Util
3
+ class TransactionHooksRecorder
4
+ # This is a thread-global singleton class which is used to capture
5
+ # the correct hooks to run when before and after transactions are
6
+ # committed. Consuming code will call the #record method, which will
7
+ # yield to the passed block:
8
+ #
9
+ # ```ruby
10
+ # TransactionHooksRecorder.record do
11
+ # TransactionHooksRecorder.add(->{ do_stuff() }, :before_commit)
12
+ # TransactionHooksRecorder.add(->{ do_more_stuff() }, :after_commit)
13
+ # {
14
+ # result: do_the_main_thing_and_return_a_result()
15
+ # }
16
+ # end
17
+ # ```
18
+ #
19
+ # before_commit hooks will be executed before the record method returns.
20
+ # All after_commit hooks will be added to the returned hash so that consumers
21
+ # can decide when and whether to execute the callbacks.
22
+ #
23
+ # Returns a hash with `result` and `after_commit_hooks` keys.
24
+ class << self
25
+ def record
26
+ reset_hooks
27
+
28
+ begin
29
+ result = yield
30
+ run(:before_commit)
31
+
32
+ unless result.kind_of?(::Hash)
33
+ result = { result: result }
34
+ end
35
+
36
+ result.tap do |r|
37
+ r[:after_commit_hooks] = hook_set(:after_commit)
38
+ end
39
+ ensure
40
+ reset_hooks
41
+ end
42
+ end
43
+
44
+ # Because hooks will be added from the outer edges of
45
+ # the graph, working inwards
46
+ def add(prc, lifecycle_event)
47
+ hook_set(lifecycle_event).unshift(prc)
48
+ end
49
+
50
+ def run(lifecycle_event)
51
+ _hooks[lifecycle_event].each { |h| h.call }
52
+ end
53
+
54
+ private
55
+ def _hooks
56
+ Thread.current[:_graphiti_hooks]
57
+ end
58
+
59
+ def reset_hooks
60
+ Thread.current[:_graphiti_hooks] = {
61
+ before_commit: [],
62
+ after_commit: [],
63
+ }
64
+ end
65
+
66
+ def hook_set(lifecycle_event)
67
+ _hooks[lifecycle_event]
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.0.rc.19"
2
+ VERSION = "1.0.rc.21"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiti
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.rc.19
4
+ version: 1.0.rc.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-23 00:00:00.000000000 Z
11
+ date: 2019-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable
@@ -284,7 +284,6 @@ files:
284
284
  - lib/graphiti/util/class.rb
285
285
  - lib/graphiti/util/field_params.rb
286
286
  - lib/graphiti/util/hash.rb
287
- - lib/graphiti/util/hooks.rb
288
287
  - lib/graphiti/util/include_params.rb
289
288
  - lib/graphiti/util/link.rb
290
289
  - lib/graphiti/util/persistence.rb
@@ -294,6 +293,7 @@ files:
294
293
  - lib/graphiti/util/serializer_attributes.rb
295
294
  - lib/graphiti/util/serializer_relationships.rb
296
295
  - lib/graphiti/util/sideload.rb
296
+ - lib/graphiti/util/transaction_hooks_recorder.rb
297
297
  - lib/graphiti/util/validation_response.rb
298
298
  - lib/graphiti/version.rb
299
299
  homepage:
@@ -316,7 +316,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
316
316
  version: 1.3.1
317
317
  requirements: []
318
318
  rubyforge_project:
319
- rubygems_version: 2.6.13
319
+ rubygems_version: 2.7.7
320
320
  signing_key:
321
321
  specification_version: 4
322
322
  summary: Easily build jsonapi.org-compatible APIs
@@ -1,33 +0,0 @@
1
- module Graphiti
2
- module Util
3
- class Hooks
4
- def self.record
5
- self.hooks = []
6
- begin
7
- yield.tap { run }
8
- ensure
9
- self.hooks = []
10
- end
11
- end
12
-
13
- def self._hooks
14
- Thread.current[:_graphiti_hooks] ||= []
15
- end
16
- private_class_method :_hooks
17
-
18
- def self.hooks=(val)
19
- Thread.current[:_graphiti_hooks] = val
20
- end
21
-
22
- # Because hooks will be added from the outer edges of
23
- # the graph, working inwards
24
- def self.add(prc)
25
- _hooks.unshift(prc)
26
- end
27
-
28
- def self.run
29
- _hooks.each { |h| h.call }
30
- end
31
- end
32
- end
33
- end