class Agnostic::Duplicate::ChangeSet::DeepCopy

Defines a changeset where a deep copy wants to be applied to all attributes

Private Class Methods

dup_item(item) click to toggle source

Duplicates the object passed as parameter @param item [Object] object to be duplicated @return [Object] the duplicated new instance object

# File lib/agnostic/duplicate.rb, line 202
def self.dup_item(item)
  if item.respond_to? :duplicate
    item.duplicate
  else
    item.dup
  end
rescue
  item
end

Public Instance Methods

apply(parent, model) click to toggle source

Applies changes needed on the duplicated new instance object @param parent [Duplicate] the original object to be duplicated @param model [Duplicate] the duplicated new instance object

# File lib/agnostic/duplicate.rb, line 164
def apply(parent, model)
  attributes.each do |attribute|
    unless model.respond_to? "#{attribute}="
      fail AttributeNotFound, "Attribute: '#{attribute}'", caller
    end
    deep_copy = dup_attribute(parent, attribute)
    copy_attribute(attribute, model, deep_copy)
  end
end

Private Instance Methods

copy_attribute(attribute, model, deep_copy) click to toggle source

@param attribute [Symbol] attribute to be copied @param parent [Duplicable] the original object to be duplicated @param model [Duplicable] the duplicated new instance object

# File lib/agnostic/duplicate.rb, line 179
def copy_attribute(attribute, model, deep_copy)
  model.send("#{attribute}=", deep_copy)
rescue
  raise_copy_error_for(attribute)
end
dup_attribute(parent, attribute) click to toggle source

@param parent [Duplicate] the original object to be duplicated @param attribute [Symbol] the attribute to be duplicated @return from a duplicable object the duplicated value for the attribute specified

# File lib/agnostic/duplicate.rb, line 189
def dup_attribute(parent, attribute)
  value = parent.send(attribute)
  klass = self.class
  if value && value.respond_to?(:collect)
    value.map { |item| klass.dup_item(item) }
  else
    value && klass.dup_item(value)
  end
end