Class: Diff::LCS::Change

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
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.

Direct Known Subclasses

ContextChange

Constant Summary collapse

VALID_ACTIONS =

The only actions valid for changes are ‘+’ (add), ‘-’ (delete), ‘=’ (no change), ‘!’ (changed), ‘<’ (tail changes from first sequence), or ‘>’ (tail changes from second sequence). The last two (‘<>’) are only found with Diff::LCS::diff and Diff::LCS::sdiff.

%W(+ - = ! > <)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Change

Returns a new instance of Change.



25
26
27
28
29
30
31
32
# File 'lib/diff/lcs/change.rb', line 25

def initialize(*args)
  @action, @position, @element = *args

  unless Diff::LCS::Change.valid_action?(@action)
    raise "Invalid Change Action '#{@action}'"
  end
  raise "Invalid Position Type" unless @position.kind_of? Fixnum
end

Instance Attribute Details

#actionObject (readonly)

Returns the action this Change represents.



18
19
20
# File 'lib/diff/lcs/change.rb', line 18

def action
  @action
end

#elementObject (readonly)

Returns the sequence element of the Change.



23
24
25
# File 'lib/diff/lcs/change.rb', line 23

def element
  @element
end

#positionObject (readonly)

Returns the position of the Change.



21
22
23
# File 'lib/diff/lcs/change.rb', line 21

def position
  @position
end

Class Method Details

.from_a(arr) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/diff/lcs/change.rb', line 42

def self.from_a(arr)
  arr = arr.flatten
  case arr.size
  when 5
    Diff::LCS::ContextChange.new(*(arr[0...5]))
  when 3
    Diff::LCS::Change.new(*(arr[0...3]))
  else
    raise "Invalid change array format provided."
  end
end

.valid_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/diff/lcs/change.rb', line 13

def self.valid_action?(action)
  VALID_ACTIONS.include? action
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
65
66
67
# File 'lib/diff/lcs/change.rb', line 62

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



56
57
58
59
60
# File 'lib/diff/lcs/change.rb', line 56

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

#adding?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/diff/lcs/change.rb', line 69

def adding?
  @action == '+'
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  @action == '!'
end

#deleting?Boolean

Returns:

  • (Boolean)


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

def deleting?
  @action == '-'
end

#finished_a?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/diff/lcs/change.rb', line 85

def finished_a?
  @action == '>'
end

#finished_b?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/diff/lcs/change.rb', line 89

def finished_b?
  @action == '<'
end

#inspectObject



34
35
36
# File 'lib/diff/lcs/change.rb', line 34

def inspect
  to_a.inspect
end

#to_aObject



38
39
40
# File 'lib/diff/lcs/change.rb', line 38

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

#unchanged?Boolean

Returns:

  • (Boolean)


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

def unchanged?
  @action == '='
end