Class: Zonesync::Diff

Inherits:
Struct
  • Object
show all
Defined in:
lib/zonesync/diff.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fromObject

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



4
5
6
# File 'lib/zonesync/diff.rb', line 4

def from
  @from
end

#toObject

Returns the value of attribute to

Returns:

  • (Object)

    the current value of to



4
5
6
# File 'lib/zonesync/diff.rb', line 4

def to
  @to
end

Class Method Details

.call(from:, to:) ⇒ Object



5
6
7
# File 'lib/zonesync/diff.rb', line 5

def self.call(from:, to:)
  new(from, to).call
end

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/zonesync/diff.rb', line 9

def call
  changes = ::Diff::LCS.sdiff(from.diffable_records, to.diffable_records)
  changes.map do |change|
    case change.action
    when "-"
      [:remove, [change.old_element.to_h]]
    when "!"
      [:change, [change.old_element.to_h, change.new_element.to_h]]
    when "+"
      [:add, [change.new_element.to_h]]
    end
  end.compact
end