class HappyMapper::ChangeLister

ChangeLister creates a hash of all changes between the two objects

Public Class Methods

new(current, compared) click to toggle source
# File lib/happymapper/differ.rb, line 131
def initialize(current, compared)
  @current = current
  @compared = compared
  @changes = {}
end

Public Instance Methods

elements_and_attributes() click to toggle source
# File lib/happymapper/differ.rb, line 182
def elements_and_attributes
  @current.class.attributes + @current.class.elements
end
eq(a,b) click to toggle source
# File lib/happymapper/differ.rb, line 137
def eq(a,b)
  if a.respond_to?(:to_xml) && b.respond_to?(:to_xml)
    a.to_xml == b.to_xml
  else
    a == b
  end
end
find_changes() click to toggle source
# File lib/happymapper/differ.rb, line 145
def find_changes
  elements_and_attributes.map(&:name).each do |name|
    el  = @current.send(name)

    if el.is_a?(Array)
      many_changes(el, key: name)
    else
      other_el = get_compared_value(name)
      if ! eq(el, other_el)
        @changes[name] = other_el
      end
    end
  end

  @changes
end
get_compared_value(key) click to toggle source
# File lib/happymapper/differ.rb, line 174
def get_compared_value(key)
  if @compared.respond_to?(key)
    @compared.send(key)
  else
    nil
  end
end
many_changes(els, key:) click to toggle source

Handle change for has_many elements

# File lib/happymapper/differ.rb, line 163
def many_changes(els, key:)
  other_els = get_compared_value(key) || []

  els.each_with_index do |el, i|
    if ! eq(el, other_els[i])
      @changes[key] ||= []
      @changes[key] << other_els[i]
    end
  end
end