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?, #inspect, #unchanged?, valid_action?

Constructor Details

#initialize(*args) ⇒ ContextChange

Returns a new instance of ContextChange.


114
115
116
117
118
119
120
# File 'lib/diff/lcs/change.rb', line 114

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

  fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
  fail 'Invalid (Old) Position Type' unless @old_position.nil? or @old_position.kind_of? IntClass
  fail 'Invalid (New) Position Type' unless @new_position.nil? or @new_position.kind_of? IntClass
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


132
133
134
# File 'lib/diff/lcs/change.rb', line 132

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 '+'.


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/diff/lcs/change.rb', line 138

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


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

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


157
158
159
160
161
162
163
164
# File 'lib/diff/lcs/change.rb', line 157

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

#to_aObject Also known as: to_ary


122
123
124
125
126
127
128
# File 'lib/diff/lcs/change.rb', line 122

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