Class: HappyMapper::Differ

Inherits:
Object
  • Object
show all
Defined in:
lib/happymapper/differ.rb

Overview

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

Constant Summary collapse

VERSION =
"0.2"

Instance Method Summary collapse

Constructor Details

#initialize(left, right) ⇒ Differ

Returns a new instance of Differ.



12
13
14
15
# File 'lib/happymapper/differ.rb', line 12

def initialize(left, right)
  @left = left
  @right = right
end

Instance Method Details

#diffObject

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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