Class: Diff::LCS::Change

Inherits:
Object
  • Object
show all
Includes:
Comparable, ChangeTypeTests
Defined in:
lib/watobo/external/diff/lcs/change.rb

Overview

Represents a simplistic (non-contextual) change. Represents the removal or addition of an element from either the old or the new sequenced enumerable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChangeTypeTests

#adding?, #changed?, #deleting?, #finished_a?, #finished_b?, #unchanged?

Constructor Details

#initialize(action, position, element) ⇒ Change

Returns a new instance of Change.


91
92
93
94
95
# File 'lib/watobo/external/diff/lcs/change.rb', line 91

def initialize(action, position, element)
  @action = action
  @position = position
  @element = element
end

Instance Attribute Details

#actionObject (readonly)

Returns the action this Change represents. Can be '+' (#adding?), '-' (#deleting?), '=' (#unchanged?), # or '!' (#changed?). When created by Diff::LCS#diff or Diff::LCS#sdiff, it may also be '>' (#finished_a?) or '<' (#finished_b?).


73
74
75
# File 'lib/watobo/external/diff/lcs/change.rb', line 73

def action
  @action
end

#elementObject (readonly)

Returns the value of attribute element


75
76
77
# File 'lib/watobo/external/diff/lcs/change.rb', line 75

def element
  @element
end

#positionObject (readonly)

Returns the value of attribute position


74
75
76
# File 'lib/watobo/external/diff/lcs/change.rb', line 74

def position
  @position
end

Class Method Details

.from_a(arr) ⇒ Object


102
103
104
# File 'lib/watobo/external/diff/lcs/change.rb', line 102

def self.from_a(arr)
  Diff::LCS::Change.new(arr[0], arr[1], arr[2])
end

Instance Method Details

#<=>(other) ⇒ Object


84
85
86
87
88
89
# File 'lib/watobo/external/diff/lcs/change.rb', line 84

def <=>(other)
  r = self.action <=> other.action
  r = self.position <=> other.position if r.zero?
  r = self.element <=> other.element if r.zero?
  r
end

#==(other) ⇒ Object


78
79
80
81
82
# File 'lib/watobo/external/diff/lcs/change.rb', line 78

def ==(other)
  (self.action == other.action) and
  (self.position == other.position) and
  (self.element == other.element)
end

#to_aObject

Creates a Change from an array produced by Change#to_a.


98
99
100
# File 'lib/watobo/external/diff/lcs/change.rb', line 98

def to_a
  [@action, @position, @element]
end