Class: Diff::LCS::ContextChange

Inherits:
Change
  • Object
show all
Defined in:
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 enumerables as well as the action taken.

Constant Summary

Constants inherited from Change

Diff::LCS::Change::IntClass, Diff::LCS::Change::VALID_ACTIONS

Instance Attribute Summary collapse

Attributes inherited from Change

#action, #element, #position

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Change

#adding?, #changed?, #deleting?, #finished_a?, #finished_b?, #unchanged?, valid_action?

Constructor Details

#initialize(*args) ⇒ ContextChange

Returns a new instance of ContextChange.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/diff/lcs/change.rb', line 114

def initialize(*args)
  @action, @old_position, @old_element, @new_position, @new_element = *args

  unless Diff::LCS::Change.valid_action?(@action)
    raise "Invalid Change Action '#{@action}'"
  end
  unless @old_position.nil? or @old_position.kind_of? IntClass
    raise "Invalid (Old) Position Type"
  end
  unless @new_position.nil? or @new_position.kind_of? IntClass
    raise "Invalid (New) Position Type"
  end
end

Instance Attribute Details

#new_elementObject (readonly)

Returns the new element being changed.



112
113
114
# File 'lib/diff/lcs/change.rb', line 112

def new_element
  @new_element
end

#new_positionObject (readonly)

Returns the new position being changed.



108
109
110
# File 'lib/diff/lcs/change.rb', line 108

def new_position
  @new_position
end

#old_elementObject (readonly)

Returns the old element being changed.



110
111
112
# File 'lib/diff/lcs/change.rb', line 110

def old_element
  @old_element
end

#old_positionObject (readonly)

Returns the old position being changed.



106
107
108
# File 'lib/diff/lcs/change.rb', line 106

def old_position
  @old_position
end

Class Method Details

.from_a(arr) ⇒ Object



139
140
141
# File 'lib/diff/lcs/change.rb', line 139

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

.simplify(event) ⇒ Object

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/diff/lcs/change.rb', line 145

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



173
174
175
176
177
178
179
180
# File 'lib/diff/lcs/change.rb', line 173

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



164
165
166
167
168
169
170
171
# File 'lib/diff/lcs/change.rb', line 164

def ==(other)
  (self.class == other.class) and
  (@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



135
136
137
# File 'lib/diff/lcs/change.rb', line 135

def inspect(*args)
  to_a.inspect
end

#to_aObject



128
129
130
131
132
133
# File 'lib/diff/lcs/change.rb', line 128

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