class TPX_2_2::MergingHomogeneousList

A list (Array) whose elements are all the same type and have the same id attribute

(MergingHomogeneousList.child_id) and who merges certain attributes on duplicate object insert.

Attributes

attributes_to_merge[RW]

This list of attributes to merge on duplicate object id insert.

Public Class Methods

on_duplicate_addition_merge_attributes(attributes) click to toggle source

Defines the child attribute names that the list will merge when an object of the same id as an existing list member is inserted. Another accessor for ‘attributes_to_merge`.

@param [Array] attributes The list of attribute names to merge.

# File lib/tpx/2_2/merging_homogeneous_list.rb, line 14
def on_duplicate_addition_merge_attributes(attributes)
  self.attributes_to_merge = attributes
end

Public Instance Methods

<<(child) click to toggle source

Add a new child to the MergingHomogeneousList (push). Merge ‘attributes_to_merge` on a duplicate child id insert.

@param [Hash or MergingHomogeneousList.type] child The object to add to the MergingHomogeneousList.

Calls superclass method TPX_2_2::HomogeneousList#<<
# File lib/tpx/2_2/merging_homogeneous_list.rb, line 22
def <<(child)
  unless child.is_a?(self.class.type) && child.has_key?(self.class.child_id)
    raise TPX_2_2::ValidationError, "Element provided to #{self.class}#<< must be #{self.class.type} with key `#{self.class.child_id}`."
  end
  element = self.find {|e| e[self.class.child_id] == child[self.class.child_id] }
  if element.nil?
    super
  else
    self.class.attributes_to_merge.each do |attribute|
      element[attribute].merge!(child[attribute])
    end
  end
end