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

IntClass =

Fixnum is deprecated in Ruby 2.4 # rubocop:disable Naming/ConstantName

1.class
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[+ - = ! > <].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Change

Returns a new instance of Change.



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

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

  fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
  fail "Invalid Position Type" unless @position.is_a? IntClass
end

Instance Attribute Details

#actionObject (readonly)

Returns the action this Change represents.



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

def action
  @action
end

#elementObject (readonly)

Returns the sequence element of the Change.



25
26
27
# File 'lib/diff/lcs/change.rb', line 25

def element
  @element
end

#positionObject (readonly)

Returns the position of the Change.



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

def position
  @position
end

Class Method Details

.from_a(arr) ⇒ Object



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

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

.valid_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/diff/lcs/change.rb', line 15

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

Instance Method Details

#<=>(other) ⇒ Object



65
66
67
68
69
70
# File 'lib/diff/lcs/change.rb', line 65

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

#==(other) ⇒ Object



58
59
60
61
62
63
# File 'lib/diff/lcs/change.rb', line 58

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

#adding?Boolean

Returns:

  • (Boolean)


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

def adding?
  @action == "+"
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  @action == "!"
end

#deleting?Boolean

Returns:

  • (Boolean)


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

def deleting?
  @action == "-"
end

#finished_a?Boolean

Returns:

  • (Boolean)


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

def finished_a?
  @action == ">"
end

#finished_b?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/diff/lcs/change.rb', line 92

def finished_b?
  @action == "<"
end

#inspect(*_args) ⇒ Object



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

def inspect(*_args)
  "#<#{self.class}: #{to_a.inspect}>"
end

#to_aObject Also known as: to_ary



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

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

#unchanged?Boolean

Returns:

  • (Boolean)


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

def unchanged?
  @action == "="
end