Class: Diff::LCS::Change

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/diff/lcs/change.rb,
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.

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action:, position:, element:) ⇒ Change

Returns the sequence element of the Change. :attr_reader: element



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

def initialize(action:, position:, element:)
  fail "Invalid Change Action '#{action}'" unless Diff::LCS::Change.valid_action?(action)
  fail "Invalid Position Type" unless position.is_a?(Integer)

  super
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



3
4
5
# File 'lib/diff/lcs/change.rb', line 3

def action
  @action
end

#elementObject (readonly)

Returns the value of attribute element

Returns:

  • (Object)

    the current value of element



3
4
5
# File 'lib/diff/lcs/change.rb', line 3

def element
  @element
end

#positionObject (readonly)

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



3
4
5
# File 'lib/diff/lcs/change.rb', line 3

def position
  @position
end

Class Method Details

.from_a(arr) ⇒ Object



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

def self.from_a(arr)
  case arr
  in [action, [old_position, old_element], [new_position, new_element]]
    Diff::LCS::ContextChange[action, old_position, old_element, new_position, new_element]
  in [action, position, element]
    new(action, position, element)
  else
    fail "Invalid change array format provided."
  end
end

.valid_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#adding?Boolean

Returns:

  • (Boolean)


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

def adding? = action == "+"

#changed?Boolean

Returns:

  • (Boolean)


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

def changed? = action == "!"

#deconstruct_keys(_) ⇒ Object



42
# File 'lib/diff/lcs/change.rb', line 42

def deconstruct_keys(_) = {action:, position:, element:}

#deleting?Boolean

Returns:

  • (Boolean)


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

def deleting? = action == "-"

#finished_a?Boolean

Returns:

  • (Boolean)


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

def finished_a? = action == ">"

#finished_b?Boolean

Returns:

  • (Boolean)


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

def finished_b? = action == "<"

#inspect(*_args) ⇒ Object



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

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

#to_aObject Also known as: to_ary, deconstruct



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

def to_a = [action, position, element]

#unchanged?Boolean

Returns:

  • (Boolean)


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

def unchanged? = action == "="