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::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.



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/diff/lcs/change.rb', line 111

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? Fixnum
    raise "Invalid (Old) Position Type"
  end
  unless @new_position.nil? or @new_position.kind_of? Fixnum
    raise "Invalid (New) Position Type"
  end
end

Instance Attribute Details

#new_elementObject (readonly)

Returns the new element being changed.



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

def new_element
  @new_element
end

#new_positionObject (readonly)

Returns the new position being changed.



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

def new_position
  @new_position
end

#old_elementObject (readonly)

Returns the old element being changed.



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

def old_element
  @old_element
end

#old_positionObject (readonly)

Returns the old position being changed.



103
104
105
# File 'lib/diff/lcs/change.rb', line 103

def old_position
  @old_position
end

Class Method Details

.from_a(arr) ⇒ Object



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

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 ‘+’.



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

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



169
170
171
172
173
174
175
176
# File 'lib/diff/lcs/change.rb', line 169

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



161
162
163
164
165
166
167
# File 'lib/diff/lcs/change.rb', line 161

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



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

def inspect(*args)
  to_a.inspect
end

#to_aObject



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

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