Module: Dynamoid::Dirty::DeepDupper

Defined in:
lib/dynamoid/dirty.rb

Class Method Summary collapse

Class Method Details

.dup_attribute(value, type_options) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/dynamoid/dirty.rb', line 316

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 cannot be duplicated - is used for binary fields
    value
  when String
    value.dup
  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) })
  when Hash
    value.deep_dup
  else
    if type.is_a? Class # custom type
      Marshal.load(Marshal.dump(value)) # dup instance variables
    else
      value.dup # date, datetime
    end
  end
end

.dup_attributes(attributes, klass) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/dynamoid/dirty.rb', line 308

def self.dup_attributes(attributes, klass)
  attributes.map do |name, value|
    type_options = klass.attributes[name.to_sym]
    value_duplicate = dup_attribute(value, type_options)
    [name, value_duplicate]
  end.to_h
end