Class: Diff::LCS::Change

Inherits:
Object show all
Includes:
Comparable, ChangeTypeTests
Defined in:
lib/gems/diff-lcs-1.1.2/lib/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.



70
71
72
73
74
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 70

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?).



52
53
54
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 52

def action
  @action
end

#elementObject (readonly)

Returns the value of attribute element.



54
55
56
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 54

def element
  @element
end

#positionObject (readonly)

Returns the value of attribute position.



53
54
55
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 53

def position
  @position
end

Class Method Details

.from_a(arr) ⇒ Object



81
82
83
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 81

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

Instance Method Details

#<=>(other) ⇒ Object



63
64
65
66
67
68
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 63

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



57
58
59
60
61
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 57

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.



77
78
79
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs/change.rb', line 77

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