class HappyMapper::Differ

Differ compares the differences betwee two HappyMapper objects.

Two step process First step is map all nodes into a DiffedItem Step two is present all the changes via DiffedItem.changes

Constants

VERSION

Public Class Methods

new(left, right) click to toggle source
# File lib/happymapper/differ.rb, line 12
def initialize(left, right)
  @left = left
  @right = right
end

Public Instance Methods

diff() click to toggle source

Diff is a method to find what elements and attributes have changed and how.It extends each element and attribute with the DiffedItem module

# File lib/happymapper/differ.rb, line 19
def diff
  @left = DiffedItem.create(@left, @right)

  # setup for each element (has_one and has_many) and attribute
  all_items.each do |item|
    lvalue = get_value(@left, item.name)
    rvalue = get_value(@right, item.name)

    # skip if both sides are nil
    next if rvalue.nil? && lvalue.nil?

    if ! item.options[:single]
      setup_element(lvalue, rvalue)
      # Find the side with the most items. If the right has more, the left
      # will be padded with UnExtendable instances
      count = [lvalue.size, (rvalue || []).size].max

      count.times do |i|
        lvalue[i] = setup_element(lvalue[i], (rvalue || [])[i])
      end
    else
      lvalue = setup_element(lvalue, rvalue)
    end

    @left.send("#{item.name}=", lvalue)
  end

  @left
end

Protected Instance Methods

all_items() click to toggle source

returns all the elements and attributes for the left class

# File lib/happymapper/differ.rb, line 60
def all_items
  @left.class.attributes + @left.class.elements
end
get_value(side, name) click to toggle source
# File lib/happymapper/differ.rb, line 51
def get_value(side, name)
  begin
    side.send(name)
  rescue
    nil
  end
end
setup_element(item, compared) click to toggle source
# File lib/happymapper/differ.rb, line 64
def setup_element(item, compared)
  if item.is_a?(HappyMapper)
    Differ.new(item, compared).diff
  else
    DiffedItem.create(item, compared)
  end
end