Module: DeepUnrest

Defined in:
lib/deep_unrest.rb,
lib/deep_unrest/read.rb,
lib/deep_unrest/write.rb,
lib/deep_unrest/engine.rb,
lib/deep_unrest/version.rb,
lib/deep_unrest/paginators/basic.rb,
app/jobs/deep_unrest/application_job.rb,
lib/deep_unrest/concerns/map_temp_ids.rb,
lib/deep_unrest/concerns/null_concern.rb,
lib/deep_unrest/concerns/resource_scope.rb,
app/models/deep_unrest/application_record.rb,
app/helpers/deep_unrest/application_helper.rb,
app/mailers/deep_unrest/application_mailer.rb,
lib/deep_unrest/authorization/base_strategy.rb,
lib/deep_unrest/authorization/none_strategy.rb,
lib/deep_unrest/authorization/pundit_strategy.rb,
app/controllers/deep_unrest/application_controller.rb

Overview

Update deeply nested associations wholesale

Defined Under Namespace

Modules: ApplicationHelper, Authorization, Concerns, Paginators, Read, Write Classes: ApplicationController, ApplicationJob, ApplicationMailer, ApplicationRecord, Conflict, Engine, InvalidAssociation, InvalidId, InvalidParentScope, InvalidPath, InvalidQuery, Unauthorized, UnpermittedParams, ValidationError

Constant Summary collapse

VERSION =
'0.1.76'

Class Method Summary collapse

Class Method Details

.add_parent_scope(parent, type) ⇒ Object

verify that this is an actual association of the parent class.



111
112
113
114
# File 'lib/deep_unrest.rb', line 111

def self.add_parent_scope(parent, type)
  reflection = parent[:klass].reflect_on_association(to_assoc(type))
  { base: parent[:scope], method: reflection.name }
end

.build_mutation_body(ops, scopes, user) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/deep_unrest.rb', line 409

def self.build_mutation_body(ops, scopes, user)
  err_path_memo = {}
  ops.each_with_object(HashWithIndifferentAccess.new({})) do |op, memo|
    memo.deep_merge!(build_mutation_fragment(op, scopes, user, err_path_memo)) do |_key, a, b|
      if a.is_a? Array
        combine_arrays(a, b)
      else
        b
      end
    end
  end
end

.build_mutation_fragment(op, scopes, user, err_path_memo, rest = nil, memo = nil, cursor = nil, type = nil) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/deep_unrest.rb', line 330

def self.build_mutation_fragment(op, scopes, user, err_path_memo, rest = nil, memo = nil, cursor = nil, type = nil)
  rest ||= parse_path(op[:path])

  if rest.empty?
    set_action(cursor, op, type, user, scopes, err_path_memo)
    return memo
  end

  type, id_str = rest.shift
  addr = to_update_body_key(type)
  id = parse_id(id_str)
  scope_type = get_scope_type(id_str, rest.blank?, op[:destroy])
  temp_id = scope_type == :create ? id_str : nil

  memo, next_cursor = get_mutation_cursor(memo,
                                          cursor,
                                          addr,
                                          type,
                                          id,
                                          temp_id,
                                          scope_type)

  next_cursor[:id] = id if id
  build_mutation_fragment(op, scopes, user, err_path_memo, rest, memo, next_cursor, type)
end

.build_redirect_regex(replacements) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/deep_unrest.rb', line 535

def self.build_redirect_regex(replacements)
  replacements ||= []

  replace_ops = replacements.map do |k, v|
    proc { |str| str.sub(k.to_s, v.to_s) }
  end

  proc do |str|
    replace_ops.each { |op| str = op.call(str) }
    str
  end
end

.collect_action_scopes(operation) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/deep_unrest.rb', line 181

def self.collect_action_scopes(operation)
  resources = parse_path(operation[:path])
  resources.each_with_object([]) do |(type, id), memo|
    validate_association(memo.last, type)
    scope_type = get_scope_type(id,
                                memo.size == resources.size - 1,
                                operation[:destroy])
    scope = get_scope(scope_type, memo, type, id)
    context = { type: type,
                scope_type: scope_type,
                scope: scope,
                klass: to_class(type),
                error_path: operation[:errorPath],
                id: id }

    context[:path] = operation[:path] unless scope_type == :show
    memo.push(context)
  end
end

.collect_all_scopes(params) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/deep_unrest.rb', line 201

def self.collect_all_scopes(params)
  idx = {}
  params.map { |operation| collect_action_scopes(operation) }
        .flatten
        .each_with_object({}) do |op, memo|
          # ensure no duplicate scopes
          memo["#{op[:scope_type]}-#{op[:type]}-#{op[:id]}"] ||= {}
          memo["#{op[:scope_type]}-#{op[:type]}-#{op[:id]}"].merge!(op)
        end.values
        .map do |op|
          unless op[:scope_type] == :show
            op[:index] = update_indices(idx, op[:type])[op[:type]] - 1
          end
          op
        end
end

.collect_authorized_scopes(mappings, user) ⇒ Object



696
697
698
699
700
# File 'lib/deep_unrest.rb', line 696

def self.collect_authorized_scopes(mappings, user)
  mappings.each do |mapping|
    mapping[:scope] = DeepUnrest.authorization_strategy.get_authorized_scope(user, mapping[:klass])
  end
end

.combine_arrays(a, b) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/deep_unrest.rb', line 356

def self.combine_arrays(a, b)
  # get list of items duped by id
  groups = (a + b).flatten.group_by { |item| item[:id] }
  dupes = groups.select { |_, v| v.size > 1 }.values

  # filter non-dupe
  non_dupes = groups.select { |_, v| v.size == 1 }.values

  # recrsively merge dupes
  merged = dupes.map do |(a2, b2)|
    a2.deep_merge(b2) do |_, a3, b3|
      if a3.is_a? Array
        combine_arrays(a3, b3)
      else
        b3
      end
    end
  end

  # add merged dupes to non-dupes
  (non_dupes + merged).flatten
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (DeepUnrest)

    the object that the method was called on



30
31
32
# File 'lib/deep_unrest/engine.rb', line 30

def self.configure(&_block)
  yield self
end

.convert_temp_ids!(ctx, mutations) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/deep_unrest.rb', line 309

def self.convert_temp_ids!(ctx, mutations)
  case mutations
  when Hash
    mutations.keys.map do |key|
      val = mutations[key]
      if ['id', :id].include?(key)
        unless parse_id(val)
          mutations.delete(key)
          mutations[:deep_unrest_temp_id] = val
          mutations[:deep_unrest_context] = ctx
        end
      else
        convert_temp_ids!(ctx, val)
      end
    end
  when Array
    mutations.map { |val| convert_temp_ids!(ctx, val) }
  end
  mutations
end

.deep_camelize_keys(query) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/deep_unrest.rb', line 681

def self.deep_camelize_keys(query)
  query&.deep_transform_keys! do |key|
    k = begin
          key.to_s.camelize(:lower)
        rescue StandardError
          key
        end
    begin
      k.to_sym
    rescue StandardError
      key
    end
  end
end

.deep_underscore_keys(query) ⇒ Object

SHARED ###



666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/deep_unrest.rb', line 666

def self.deep_underscore_keys(query)
  query&.deep_transform_keys! do |key|
    k = begin
          key.to_s.underscore
        rescue StandardError
          key
        end
    begin
      k.to_sym
    rescue StandardError
      key
    end
  end
end

.format_error_keys(res) ⇒ Object



548
549
550
551
# File 'lib/deep_unrest.rb', line 548

def self.format_error_keys(res)
  record = res[:record]
  record&.errors&.messages
end

.format_error_title(title) ⇒ Object

handle error titles in cases where error value is an array



484
485
486
487
488
489
490
# File 'lib/deep_unrest.rb', line 484

def self.format_error_title(title)
  if title.is_a?(Array)
    title.join(', ')
  else
    title
  end
end

.format_errors(operation, path_info, values) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/deep_unrest.rb', line 492

def self.format_errors(operation, path_info, values)
  if operation
    return values.map do |msg|
      base_path = (
        operation[:error_path] ||
        operation[:dr_error_key] ||
        operation[:ar_error_key]
      )
      # TODO: case field name according to jsonapi_resources settings
      field_name = path_info[:field].camelize(:lower)
      pointer = [base_path, field_name].compact.join('.')
      active_record_path = [operation[:ar_error_key],
                            field_name].reject(&:empty?).compact.join('.')
      deep_unrest_path = [operation[:dr_error_key],
                          field_name].compact.join('.')
      { title: "#{path_info[:field].humanize} #{format_error_title(msg)}",
        detail: msg,
        source: { pointer: pointer,
                  deepUnrestPath: deep_unrest_path,
                  activeRecordPath: active_record_path } }
    end
  end
  values.map do |msg|
    { title: msg, detail: msg, source: { pointer: nil } }
  end
end

.get_mutation_cursor(memo, cursor, addr, type, id, temp_id, scope_type) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/deep_unrest.rb', line 275

def self.get_mutation_cursor(memo, cursor, addr, type, id, temp_id, scope_type)
  if memo
    record = { id: id || temp_id }
    if plural?(type)
      cursor[addr] = [record]
      next_cursor = cursor[addr][0]
    else
      cursor[addr] = record
      next_cursor = cursor[addr]
    end
  else
    method = scope_type == :show ? :update : scope_type
    cursor = {}
    type_sym = type.to_sym
    klass = to_class(type)
    body = {}
    body[klass.primary_key.to_sym] = id if id
    cursor[type_sym] = {
      klass: klass,
      resource: get_resource(type)
    }
    cursor[type_sym][:operations] = {}
    cursor[type_sym][:operations][id || temp_id] = {}
    cursor[type_sym][:operations][id || temp_id][method] = {
      method: method,
      body: body
    }
    cursor[type_sym][:operations][id || temp_id][method][:temp_id] = temp_id if temp_id
    memo = cursor
    next_cursor = cursor[type_sym][:operations][id || temp_id][method][:body]
  end
  [memo, next_cursor]
end

.get_resource(type) ⇒ Object



68
69
70
# File 'lib/deep_unrest.rb', line 68

def self.get_resource(type)
  "#{type.classify}Resource".constantize
end

.get_scope(scope_type, memo, type, id_str = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/deep_unrest.rb', line 130

def self.get_scope(scope_type, memo, type, id_str = nil)
  case scope_type
  when :show, :update, :destroy
    id = /^\.(?<id>[\w-]+)$/.match(id_str)[:id]
    { base: to_class(type), method: :find, arguments: [id] }
  when :update_all, :index
    if memo.empty?
      { base: to_class(type), method: :all }
    else
      add_parent_scope(memo[memo.size - 1], type)
    end
  when :all
    { base: to_class(type), method: :all }
  end
end

.get_scope_type(id, last, destroy) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/deep_unrest.rb', line 72

def self.get_scope_type(id, last, destroy)
  case id
  when /^\[[\w+\-]+\]$/
    :create
  when /^\.[\w-]+$/
    if last
      if destroy.present?
        :destroy
      else
        :update
      end
    else
      :show
    end
  when /^\.\*$/
    if last
      if destroy.present?
        :destroy_all
      else
        :update_all
      end
    else
      :index
    end
  else
    raise InvalidId, "Unknown ID format: #{id}"
  end
end

.increment_error_indices(path_info, memo) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/deep_unrest.rb', line 227

def self.increment_error_indices(path_info, memo)
  path_info.each_with_index.map do |(type, id), i|
    next if i.zero?

    parent_type, parent_id = path_info[i - 1]
    key = "#{parent_type}#{parent_id}#{type}"
    memo[key] = [] unless memo[key]
    idx = memo[key].find_index(id)
    unless idx
      idx = memo[key].size
      memo[key] << id
    end

    "#{type.underscore}[#{idx}]"
  end.compact.join('.')
end

.map_errors_to_param_keys(scopes, ops) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/deep_unrest.rb', line 519

def self.map_errors_to_param_keys(scopes, ops)
  ops.map do |errors|
    errors.map do |key, values|
      path_info = parse_error_path(key.to_s)
      operation = scopes.find do |s|
        (
          s[:ar_error_key] &&
          s[:ar_error_key] == (path_info[:path] || '') &&
          s[:scope_type] != :show
        )
      end
      format_errors(operation, path_info, values)
    end
  end.flatten
end

.mutate(mutation, context) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/deep_unrest.rb', line 422

def self.mutate(mutation, context)
  user = context[:current_user]
  ActiveRecord::Base.transaction do
    mutation.map do |_, item|
      item[:operations].map do |id, ops|
        ops.map do |_, action|
          record = case action[:method]
                   when :update_all
                     DeepUnrest.authorization_strategy
                               .get_authorized_scope(user, item[:klass])
                               .update(action[:body])
                     nil
                   when :destroy_all
                     DeepUnrest.authorization_strategy
                               .get_authorized_scope(user, item[:klass])
                               .destroy_all
                     nil
                   when :update
                     model = item[:klass].find(id)
                     model.assign_attributes(action[:body])
                     resource = item[:resource].new(model, context)
                     resource.run_callbacks :save do
                       resource.run_callbacks :update do
                         model.save
                         model
                       end
                     end
                   when :create
                     model = item[:klass].new(action[:body])
                     resource = item[:resource].new(model, context)
                     resource.run_callbacks :save do
                       resource.run_callbacks :create do
                         resource._model.save
                         resource._model
                       end
                     end
                   when :destroy
                     model = item[:klass].find(id)
                     resource = item[:resource].new(model, context)
                     resource.run_callbacks :remove do
                       item[:klass].destroy(id)
                     end
                   end

          result = { record: record }
          if action[:temp_id]
            result[:temp_ids] = {}
            result[:temp_ids][action[:temp_id]] = record.id
          end
          result
        end
      end
    end
  end
end

.parse_attributes(type, scope_type, attributes, user) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/deep_unrest.rb', line 162

def self.parse_attributes(type, scope_type, attributes, user)
  p = JSONAPI::RequestParser.new
  resource = get_resource(type)
  p.source_klass = resource
  ctx = { current_user: user }
  opts = if scope_type == :create
           resource.creatable_fields(ctx)
         else
           resource.updatable_fields(ctx)
         end

  p.parse_params(resource, { attributes: attributes }, opts)[:attributes]
rescue JSONAPI::Exceptions::ParameterNotAllowed
  unpermitted_keys = attributes.keys.map(&:to_sym) - opts
  msg = "Attributes #{unpermitted_keys} of #{type.classify} not allowed"
  msg += " to #{user.class} with id '#{user.id}'" if user
  raise UnpermittedParams, [{ title: msg }].to_json
end

.parse_error_path(key) ⇒ Object



478
479
480
481
# File 'lib/deep_unrest.rb', line 478

def self.parse_error_path(key)
  rx = /^(?<path>.*\])?\.?(?<field>[\w\-\.]+)$/
  rx.match(key)
end

.parse_id(id_str) ⇒ Object



218
219
220
221
222
223
224
225
# File 'lib/deep_unrest.rb', line 218

def self.parse_id(id_str)
  return unless id_str.is_a?(String) || id_str.is_a?(Integer)
  return false if id_str.nil?
  return id_str if id_str.is_a? Integer

  id_match = id_str.match(/^\.?(?<id>[\w\-]+)$/)
  id_match && id_match[:id]
end

.parse_path(path) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/deep_unrest.rb', line 146

def self.parse_path(path)
  rx = /(?<type>\w+)(?<id>(?:\[|\.)[\w+\-\*\]]+)/
  result = path.scan(rx)
  unless result.map { |res| res.join('') }.join('.') == path
    raise InvalidPath, "Invalid path: #{path}"
  end

  result
end

.perform_read(ctx, params, user) ⇒ Object



553
554
555
# File 'lib/deep_unrest.rb', line 553

def self.perform_read(ctx, params, user)
  DeepUnrest::Read.read(ctx, params, user)
end

.perform_update(ctx, params, user) ⇒ Object

Raises:



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/deep_unrest.rb', line 603

def self.perform_update(ctx, params, user)
  temp_id_map = DeepUnrest::ApplicationController.class_variable_get(
    '@@temp_ids'
  )

  user = ctx[:current_user]
  uuid = ctx[:uuid]

  temp_id_map[uuid] ||= {}

  # reject new resources marked for destruction
  viable_params = params.reject do |param|
    temp_id?(param[:path]) && param[:destroy].present?
  end

  # identify requested scope(s)
  scopes = collect_all_scopes(viable_params)

  # authorize user for requested scope(s)
  DeepUnrest.authorization_strategy.authorize(scopes, user).flatten

  # bulid update arguments
  mutations = build_mutation_body(viable_params, scopes, user)

  # convert temp_ids from ids to non-activerecord attributes
  convert_temp_ids!(uuid, mutations)

  # perform update
  results = mutate(mutations, ctx).flatten

  # check results for errors
  errors = results.map { |res| format_error_keys(res) }
                  .compact
                  .reject(&:empty?)
                  .compact

  if errors.empty?
    destroyed = DeepUnrest::ApplicationController.class_variable_get(
      '@@destroyed_entities'
    )

    changed = DeepUnrest::ApplicationController.class_variable_get(
      '@@changed_entities'
    )

    diff = serialize_changes(changed, user)

    return {
      redirect_regex: build_redirect_regex(temp_id_map[uuid]),
      temp_ids: temp_id_map[uuid],
      destroyed: destroyed.map { |d| d.except(:query_uuid) },
      changed: diff
    }
  end

  # map errors to their sources
  formatted_errors = { errors: map_errors_to_param_keys(scopes, errors) }

  # raise error if there are any errors
  raise Conflict, formatted_errors.to_json unless formatted_errors.empty?
end

.perform_write(ctx, params, user) ⇒ Object



557
558
559
# File 'lib/deep_unrest.rb', line 557

def self.perform_write(ctx, params, user)
  DeepUnrest::Write.write(ctx, params, user)
end

.plural?(s) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/deep_unrest.rb', line 105

def self.plural?(s)
  str = s.to_s
  str.pluralize == str && str.singularize != str
end

.serialize_changes(diffs, user) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/deep_unrest.rb', line 574

def self.serialize_changes(diffs, user)
  ctx = { current_user: user }
  diffs.each do |diff|
    diff[:resource] = diff[:attributes]
    pk = diff[:klass].primary_key
    diff[:resource][pk] = diff[:id]
    diff[:model] = diff[:klass].new(diff[:resource])
  end

  allowed_models = diffs.select do |diff|
    scope = DeepUnrest.authorization_strategy
                      .get_authorized_scope(user,
                                            diff[:klass])
    scope.exists?(diff[:id])
  rescue NameError
    false
  end

  resources = allowed_models.map do |diff|
    resource_klass = get_resource(diff[:klass].to_s)
    fields = {}
    keys = diff[:resource].keys.map(&:to_sym)
    fields[to_assoc(diff[:klass].to_s.pluralize)] = keys

    serialize_resource(resource_klass, fields, diff[:model].id)
  end
  resources.select { |item| item.dig('attributes') }.compact
end

.serialize_resource(resource_klass, fields, id) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/deep_unrest.rb', line 561

def self.serialize_resource(resource_klass, fields, id)
  resource_identity = JSONAPI::ResourceIdentity.new(resource_klass, id)
  id_tree = JSONAPI::PrimaryResourceIdTree.new
  id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(resource_identity), {})
  resource_set = JSONAPI::ResourceSet.new(id_tree)
  serializer = JSONAPI::ResourceSerializer.new(
      resource_klass,
      fields: fields
  )
  resource_set.populate!(serializer, {}, {})
  serializer.serialize_resource_set_to_hash_single(resource_set)['data'].except('links')
end

.serialize_result(ctx, item) ⇒ Object



702
703
704
705
706
707
708
709
710
# File 'lib/deep_unrest.rb', line 702

def self.serialize_result(ctx, item)
  resource = item[:resource]
  resource_instance = resource.new(item[:record], ctx)
  fields = {}
  keys = item[:query][:fields].map(&:underscore).map(&:to_sym)
  fields[to_assoc(item[:key].pluralize)] = keys

  serialize_resource(resource, fields, item[:record].id).except(:links)
end

.set_action(cursor, operation, type, user, scopes, err_path_memo) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/deep_unrest.rb', line 244

def self.set_action(cursor, operation, type, user, scopes, err_path_memo)
  # TODO: this is horrible. find a better way to go about this
  path_info = parse_path(operation[:path])
  id_str = path_info.last[1]
  id = parse_id(id_str)
  action = get_scope_type(id_str,
                          true,
                          operation[:destroy])

  cursor[:id] = id || id_str

  scope = scopes.find do |s|
    s[:type] == type && s[:id] == id_str
  end

  scope[:ar_error_key] = increment_error_indices(path_info, err_path_memo)
  scope[:dr_error_key] = path_info.map { |pair| pair.join('') }.join('.')

  case action
  when :destroy
    cursor[:_destroy] = true
  when :update, :create, :update_all
    cursor.merge! parse_attributes(type,
                                   operation[:action],
                                   operation[:attributes],
                                   user)
  end

  cursor
end

.set_attr(hash, path, val, cursor = nil) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/deep_unrest.rb', line 379

def self.set_attr(hash, path, val, cursor = nil)
  cursor ||= hash
  key = path.shift
  key = key.to_i if cursor.is_a? Array

  if path.empty?
    case cursor
    when Array, Hash # only do anything if the cursor is iterable
      case cursor[key]
      when Hash  # only attempt to merge if
        cursor[key] = (cursor[key] || {}).deep_merge(val)
      when Array
        cursor[key] = (cursor[key] || []) + val
      else
        cursor[key] = val
      end
    end
    return hash
  end

  next_cursor = case key
                when /\[\]$/
                  cursor[key.gsub('[]', '')] ||= []
                else
                  cursor[key] ||= {}
                end

  set_attr(hash, path, val, next_cursor)
end

.temp_id?(str) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/deep_unrest.rb', line 101

def self.temp_id?(str)
  /\[[\w+\-]+\]$/.match(str)
end

.to_assoc(str) ⇒ Object



60
61
62
# File 'lib/deep_unrest.rb', line 60

def self.to_assoc(str)
  str.underscore.to_sym
end

.to_class(str) ⇒ Object



56
57
58
# File 'lib/deep_unrest.rb', line 56

def self.to_class(str)
  str.classify.constantize
end

.to_update_body_key(str) ⇒ Object



64
65
66
# File 'lib/deep_unrest.rb', line 64

def self.to_update_body_key(str)
  "#{str}Attributes".underscore.to_sym
end

.update_indices(indices, type) ⇒ Object



156
157
158
159
160
# File 'lib/deep_unrest.rb', line 156

def self.update_indices(indices, type)
  indices[type] ||= 0
  indices[type] += 1
  indices
end

.validate_association(parent, type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/deep_unrest.rb', line 116

def self.validate_association(parent, type)
  return unless parent

  reflection = parent[:klass].reflect_on_association(to_assoc(type))
  raise NoMethodError unless reflection.klass == to_class(type)

  unless parent[:id]
    raise InvalidParentScope, 'Unable to update associations of collections '\
                              "('#{parent[:type]}.#{type}')."
  end
rescue NoMethodError
  raise InvalidAssociation, "'#{parent[:type]}' has no association '#{type}'"
end