Class: Diff::LCS::ContextChange

Inherits:
Object
  • Object
show all
Includes:
Comparable, ChangeTypeTests
Defined in:
lib/watobo/external/diff/lcs/change.rb

Overview

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

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, old_position, old_element, new_position, new_element) ⇒ ContextChange

Returns a new instance of ContextChange.



146
147
148
149
150
151
152
# File 'lib/watobo/external/diff/lcs/change.rb', line 146

def initialize(action, old_position, old_element, new_position, new_element)
  @action = action
  @old_position = old_position
  @old_element = old_element
  @new_position = new_position
  @new_element = new_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?).



117
118
119
# File 'lib/watobo/external/diff/lcs/change.rb', line 117

def action
  @action
end

#new_elementObject (readonly)

Returns the value of attribute new_element.



121
122
123
# File 'lib/watobo/external/diff/lcs/change.rb', line 121

def new_element
  @new_element
end

#new_positionObject (readonly)

Returns the value of attribute new_position.



120
121
122
# File 'lib/watobo/external/diff/lcs/change.rb', line 120

def new_position
  @new_position
end

#old_elementObject (readonly)

Returns the value of attribute old_element.



119
120
121
# File 'lib/watobo/external/diff/lcs/change.rb', line 119

def old_element
  @old_element
end

#old_positionObject (readonly)

Returns the value of attribute old_position.



118
119
120
# File 'lib/watobo/external/diff/lcs/change.rb', line 118

def old_position
  @old_position
end

Class Method Details

.from_a(arr) ⇒ Object

Creates a ContextChange from an array produced by ContextChange#to_a.



159
160
161
162
163
164
165
166
# File 'lib/watobo/external/diff/lcs/change.rb', line 159

def self.from_a(arr)
  if arr.size == 5
    Diff::LCS::ContextChange.new(arr[0], arr[1], arr[2], arr[3], arr[4])
  else
    Diff::LCS::ContextChange.new(arr[0], arr[1][0], arr[1][1], arr[2][0],
                                 arr[2][1])
  end
end

.simplify(event) ⇒ Object

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/watobo/external/diff/lcs/change.rb', line 170

def self.simplify(event)
  ea = event.to_a

  case ea[0]
  when '-'
    ea[2][1] = nil
  when '<'
    ea[0] = '-'
    ea[2][1] = nil
  when '+'
    ea[1][1] = nil
  when '>'
    ea[0] = '+'
    ea[1][1] = nil
  end

  Diff::LCS::ContextChange.from_a(ea)
end

Instance Method Details

#<=>(other) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/watobo/external/diff/lcs/change.rb', line 137

def <=>(other)
  r = @action <=> other.action
  r = @old_position <=> other.old_position if r.zero?
  r = @new_position <=> other.new_position if r.zero?
  r = @old_element <=> other.old_element if r.zero?
  r = @new_element <=> other.new_element if r.zero?
  r
end

#==(other) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/watobo/external/diff/lcs/change.rb', line 125

def ==(other)
  (@action == other.action) and
  (@old_position == other.old_position) and
  (@new_position == other.new_position) and
  (@old_element == other.old_element) and
  (@new_element == other.new_element)
end

#inspect(*args) ⇒ Object



133
134
135
# File 'lib/watobo/external/diff/lcs/change.rb', line 133

def inspect(*args)
  %Q(#<#{self.class.name}:#{__id__} @action=#{action} positions=#{old_position},#{new_position} elements=#{old_element.inspect},#{new_element.inspect}>)
end

#to_aObject



154
155
156
# File 'lib/watobo/external/diff/lcs/change.rb', line 154

def to_a
  [@action, [@old_position, @old_element], [@new_position, @new_element]]
end