Method: Jinx::Mergeable#merge_attributes

Defined in:
lib/jinx/resource/mergeable.rb

#merge_attributes(other, attributes = nil, matches = nil) {|value| ... } ⇒ Mergeable

Merges the values of the other attributes into this object and returns self. The other argument can be either a Hash or an object whose class responds to the mergeable_attributes method. The optional attributes argument can be either a single attribute symbol or a collection of attribute symbols.

A hash argument consists of attribute name => value associations. For example, given a Mergeable person object with attributes ssn and children, the call:

person.merge_attributes(:ssn => '555-55-5555', :children => children)

is equivalent to:

person.ssn ||= '555-55-5555'
person.children ||= []
person.children.merge(children, :deep)

An unrecognized attribute is ignored.

If other is not a Hash, then the other object’s attributes values are merged into this object. The default attributes is this mergeable’s class Propertied#mergeable_attributes.

The merge is performed by calling #merge_attribute on each attribute with the matches and filter block given to this method.

Parameters:

Yields:

  • (value)

    the optional filter block

Yield Parameters:

  • value

    the source merge attribute value

Returns:

Raises:

  • (ArgumentError)

    if none of the following are true:

    • other is a Hash

    • attributes is non-nil

    • the other class responds to mergeable_attributes



38
39
40
41
42
43
44
45
46
47
# File 'lib/jinx/resource/mergeable.rb', line 38

def merge_attributes(other, attributes=nil, matches=nil, &filter)
  return self if other.nil? or other.equal?(self)
  attributes = [attributes] if Symbol === attributes
  attributes ||= self.class.mergeable_attributes
  # If the source object is not a hash, then convert it to an attribute => value hash.
  vh = Hasher === other ? other : other.value_hash(attributes)
  # Merge the Java values hash.
  vh.each { |pa, value| merge_attribute(pa, value, matches, &filter) }
  self
end