Method: Fera::Base#clone_with_nil

Defined in:
lib/fera/models/base.rb

#clone_with_nilObject



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/fera/models/base.rb', line 257

def clone_with_nil
  # Clone all attributes except the pk and any nested ARes
  cloned = attributes.reject { |k, v| k == self.class.primary_key || v.is_a?(ActiveResource::Base) }.map { |k, v| [k, v.clone] }.to_h
  # Form the new resource - bypass initialize of resource with 'new' as that will call 'load' which
  # attempts to convert hashes into member objects and arrays into collections of objects. We want
  # the raw objects to be cloned so we bypass load by directly setting the attributes hash.
  resource = self.class.new({}, true, { cloned: true })
  resource.prefix_options = prefix_options
  resource.send :instance_variable_set, '@attributes', cloned
  resource
end