deep_unrest 0.1.4 → 0.1.5

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: 6f717bfb628fffff42b0c527f08ab2ef3326ce35
4
- data.tar.gz: cb1b1d5078c77fd7091aae3cfa98dbba3c3f5c74
3
+ metadata.gz: 0cbcad9bd5ad113da59e1f8590695631ca23b6b4
4
+ data.tar.gz: b3b7d09394d01662f09bddb26be968624de2b4fc
5
5
  SHA512:
6
- metadata.gz: 35c90c8413e1186500ceb02dc3eb6409774c5ee7c50d827c7c09d5b9ca4e5d217e23932df17b3932a293e291789c3a60423cc50dfa0a069cc630781a2e2ca779
7
- data.tar.gz: 1950cc80610063bb97fdf39427cd93cead04d83ac993da8a04cd571cfbe38f53fc391e4d723cc32d2f679749d7e722750b49803084a081197b5c9c6bf73d1ba4
6
+ metadata.gz: 122f06b02cb3267e0750a25c8e02c5630bd5357544636d86a685d2a35644027c6222e9e6fbc7f92aaa8ffebd902c24d1cc0244c27541fea16e79a1132abf4863
7
+ data.tar.gz: 8b9e2e3aff5ac8f76078a5312d28cbcbbecb4ed1bcef1ea4160e0a118cf64e6c71a5cebf0d2ceb60d148b26f200a92ab47fbe49d1d817fa51c2795c8adede97a
data/lib/deep_unrest.rb CHANGED
@@ -73,6 +73,15 @@ module DeepUnrest
73
73
  end
74
74
  end
75
75
 
76
+ def self.temp_id?(str)
77
+ /^\[\w+\]$/.match(str)
78
+ end
79
+
80
+ def self.plural?(s)
81
+ str = s.to_s
82
+ str.pluralize == str && str.singularize != str
83
+ end
84
+
76
85
  # verify that this is an actual association of the parent class.
77
86
  def self.add_parent_scope(parent, type)
78
87
  reflection = parent[:klass].reflect_on_association(to_assoc(type))
@@ -102,7 +111,6 @@ module DeepUnrest
102
111
  else
103
112
  add_parent_scope(memo[memo.size - 1], type)
104
113
  end
105
- # TODO: delete this condition
106
114
  when :all
107
115
  { base: to_class(type), method: :all }
108
116
  end
@@ -175,16 +183,20 @@ module DeepUnrest
175
183
  end
176
184
 
177
185
  def self.parse_id(id_str)
178
- id_match = id_str.match(/^\.(?<id>\d+)$/)
186
+ id_match = id_str.match(/^\.?(?<id>\d+)$/)
179
187
  id_match && id_match[:id]
180
188
  end
181
189
 
182
190
  def self.set_action(cursor, operation, type, user)
183
191
  # TODO: this is horrible. find a better way to go about this
184
- action = get_scope_type(parse_path(operation[:path]).last[1],
192
+ id_str = parse_path(operation[:path]).last[1]
193
+ id = parse_id(id_str)
194
+ action = get_scope_type(id_str,
185
195
  true,
186
196
  operation[:destroy])
187
197
 
198
+ cursor[:id] = id || id_str
199
+
188
200
  case action
189
201
  when :destroy
190
202
  cursor[:_destroy] = true
@@ -199,8 +211,14 @@ module DeepUnrest
199
211
 
200
212
  def self.get_mutation_cursor(memo, cursor, addr, type, id, temp_id, scope_type)
201
213
  if memo
202
- cursor[addr] = [{}]
203
- next_cursor = cursor[addr][0]
214
+ record = { id: id || temp_id }
215
+ if plural?(type)
216
+ cursor[addr] = [record]
217
+ next_cursor = cursor[addr][0]
218
+ else
219
+ cursor[addr] = record
220
+ next_cursor = cursor[addr]
221
+ end
204
222
  else
205
223
  method = scope_type == :show ? :update : scope_type
206
224
  cursor = {}
@@ -212,18 +230,51 @@ module DeepUnrest
212
230
  klass: klass
213
231
  }
214
232
  cursor[type_sym][:operations] = {}
215
- cursor[type_sym][:operations][id] = {}
216
- cursor[type_sym][:operations][id][method] = {
233
+ cursor[type_sym][:operations][id || temp_id] = {}
234
+ cursor[type_sym][:operations][id || temp_id][method] = {
217
235
  method: method,
218
236
  body: body
219
237
  }
220
- cursor[type_sym][:operations][id][method][:temp_id] = temp_id if temp_id
238
+ cursor[type_sym][:operations][id || temp_id][method][:temp_id] = temp_id if temp_id
221
239
  memo = cursor
222
- next_cursor = cursor[type_sym][:operations][id][method][:body]
240
+ next_cursor = cursor[type_sym][:operations][id || temp_id][method][:body]
223
241
  end
224
242
  [memo, next_cursor]
225
243
  end
226
244
 
245
+ def self.merge_siblings!(mutations)
246
+ mutations.each do |k, v|
247
+ case v
248
+ when Array
249
+ h = v.each_with_object({}) do |item, memo|
250
+ memo[item[:id]] ||= {}
251
+ memo[item[:id]].deeper_merge(item)
252
+ merge_siblings!(item)
253
+ end
254
+ mutations[k] = h.values
255
+ when Hash
256
+ merge_siblings!(v)
257
+ end
258
+ end
259
+ mutations
260
+ end
261
+
262
+ def self.remove_temp_ids!(mutations)
263
+ case mutations
264
+ when Hash
265
+ mutations.map do |key, val|
266
+ if ['id', :id].include?(key)
267
+ mutations.delete(key) unless parse_id(val)
268
+ else
269
+ remove_temp_ids!(val)
270
+ end
271
+ end
272
+ when Array
273
+ mutations.map { |val| remove_temp_ids!(val) }
274
+ end
275
+ mutations
276
+ end
277
+
227
278
  def self.build_mutation_fragment(op, user, rest = nil, memo = nil, cursor = nil, type = nil)
228
279
  rest ||= parse_path(op[:path])
229
280
 
@@ -263,13 +314,13 @@ module DeepUnrest
263
314
  record = case action[:method]
264
315
  when :update_all
265
316
  DeepUnrest.authorization_strategy
266
- .get_authorized_scope(user, item[:klass])
267
- .update(action[:body])
317
+ .get_authorized_scope(user, item[:klass])
318
+ .update(action[:body])
268
319
  nil
269
320
  when :destroy_all
270
321
  DeepUnrest.authorization_strategy
271
- .get_authorized_scope(user, item[:klass])
272
- .destroy_all
322
+ .get_authorized_scope(user, item[:klass])
323
+ .destroy_all
273
324
  nil
274
325
  when :update
275
326
  item[:klass].update(id, action[:body])
@@ -343,6 +394,9 @@ module DeepUnrest
343
394
  # bulid update arguments
344
395
  mutations = build_mutation_body(params, user)
345
396
 
397
+ merge_siblings!(mutations)
398
+ remove_temp_ids!(mutations)
399
+
346
400
  # perform update
347
401
  results = mutate(mutations, user).flatten
348
402
 
@@ -353,6 +407,7 @@ module DeepUnrest
353
407
  .map(&:messages)
354
408
  .reject(&:empty?)
355
409
  .compact
410
+
356
411
  if errors.empty?
357
412
  temp_ids = results.map { |res| res[:temp_ids] }
358
413
  .compact
@@ -367,3 +422,4 @@ module DeepUnrest
367
422
  raise Conflict, formatted_errors.to_json unless formatted_errors.empty?
368
423
  end
369
424
  end
425
+
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeepUnrest
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_unrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lynn Hurley