Class: Diff::LCS::ContextChange

Inherits:
Data
  • Object
show all
Defined in:
lib/diff/lcs/change.rb,
lib/diff/lcs/change.rb

Overview

Represents a contextual change. Contains the position and values of the elements in the old and the new sequenced enumerable values as well as the action taken.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action:, old_position:, old_element:, new_position:, new_element:) ⇒ ContextChange

Returns the new element being changed. :attr_reader: new_element



107
108
109
110
111
112
113
# File 'lib/diff/lcs/change.rb', line 107

def initialize(action:, old_position:, old_element:, new_position:, new_element:)
  fail "Invalid Change Action '#{action}'" unless Diff::LCS::Change.valid_action?(action)
  fail "Invalid (Old) Position Type" unless old_position.nil? || old_position.is_a?(Integer)
  fail "Invalid (New) Position Type" unless new_position.nil? || new_position.is_a?(Integer)

  super
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action

Returns:

  • the current value of action



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

def action
  @action
end

#new_elementObject (readonly)

Returns the value of attribute new_element

Returns:

  • the current value of new_element



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

def new_element
  @new_element
end

#new_positionObject (readonly)

Returns the value of attribute new_position

Returns:

  • the current value of new_position



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

def new_position
  @new_position
end

#old_elementObject (readonly)

Returns the value of attribute old_element

Returns:

  • the current value of old_element



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

def old_element
  @old_element
end

#old_positionObject (readonly)

Returns the value of attribute old_position

Returns:

  • the current value of old_position



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

def old_position
  @old_position
end

Class Method Details

.from_a(arr) ⇒ Object



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

def self.from_a(arr) = Diff::LCS::Change.from_a(arr)

.simplify(event) ⇒ Object

Simplifies a context change for use in some diff callbacks. ‘<’ actions are converted to ‘-’ and ‘>’ actions are converted to ‘+’.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/diff/lcs/change.rb', line 125

def self.simplify(event)
  case event.action
  when "-"
    event.with(new_element: nil)
  when "<"
    event.with(action: "-", new_element: nil)
  when "+"
    event.with(old_element: nil)
  when ">"
    event.with(action: "+", old_element: nil)
  else
    event
  end
end

Instance Method Details

#<=>(other) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/diff/lcs/change.rb', line 149

def <=>(other)
  r = old_position <=> other.old_position
  r = new_position <=> other.new_position if r.zero?
  if r.zero?
    r = Diff::LCS::Change::VALID_ACTIONS.index(action) <=>
      Diff::LCS::Change::VALID_ACTIONS.index(other.action)
  end
  r = old_element <=> other.old_element if r.zero?
  r = new_element <=> other.new_element if r.zero?
  r
end

#==(other) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/diff/lcs/change.rb', line 140

def ==(other)
  (self.class == other.class) &&
    (action == other.action) &&
    (old_position == other.old_position) &&
    (new_position == other.new_position) &&
    (old_element == other.old_element) &&
    (new_element == other.new_element)
end

#adding?Boolean

Returns:



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

def adding? = action == "+"

#changed?Boolean

Returns:



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

def changed? = action == "!"

#deconstruct_keys(_) ⇒ Object



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

def deconstruct_keys(_) = {action:, old_position:, old_element:, new_position:, new_element:}

#deleting?Boolean

Returns:



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

def deleting? = action == "-"

#finished_a?Boolean

Returns:



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

def finished_a? = action == ">"

#finished_b?Boolean

Returns:



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

def finished_b? = action == "<"

#to_aObject Also known as: to_ary, deconstruct



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

def to_a = [action, [old_position, old_element], [new_position, new_element]]

#unchanged?Boolean

Returns:



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

def unchanged? = action == "="