Method: Dynamoid::Dirty::DeepDupper.dup_attribute

Defined in:
lib/dynamoid/dirty.rb

.dup_attribute(value, type_options) ⇒ Object



328
329
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/dynamoid/dirty.rb', line 328

def self.dup_attribute(value, type_options)
  type, of = type_options.values_at(:type, :of)

  case value
  when NilClass, TrueClass, FalseClass, Numeric, Symbol, IO
    # Till Ruby 2.4 these immutable objects could not be duplicated.
    # IO objects (used for the binary type) cannot be duplicated as well.
    value
  when Array
    if of.is_a? Class
      # custom type
      value.map { |e| dup_attribute(e, type: of) }
    else
      value.deep_dup
    end
  when Set
    Set.new(value.map { |e| dup_attribute(e, type: of) })
  else
    if type.is_a? Class
      # custom type
      dump = Dumping.dump_field(value, type_options)
      Undumping.undump_field(dump.deep_dup, type_options)
    else
      value.deep_dup
    end
  end
end