Class: Diff::LCS::ContextChange
- Inherits:
-
Data
- Object
- Data
- Diff::LCS::ContextChange
- 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
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#new_element ⇒ Object
readonly
Returns the value of attribute new_element.
-
#new_position ⇒ Object
readonly
Returns the value of attribute new_position.
-
#old_element ⇒ Object
readonly
Returns the value of attribute old_element.
-
#old_position ⇒ Object
readonly
Returns the value of attribute old_position.
Class Method Summary collapse
- .from_a(arr) ⇒ Object
-
.simplify(event) ⇒ Object
Simplifies a context change for use in some diff callbacks.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #adding? ⇒ Boolean
- #changed? ⇒ Boolean
- #deconstruct_keys(_) ⇒ Object
- #deleting? ⇒ Boolean
- #finished_a? ⇒ Boolean
- #finished_b? ⇒ Boolean
-
#initialize(action:, old_position:, old_element:, new_position:, new_element:) ⇒ ContextChange
constructor
Returns the new element being changed.
- #to_a ⇒ Object (also: #to_ary, #deconstruct)
- #unchanged? ⇒ Boolean
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
#action ⇒ Object (readonly)
Returns the value of attribute action
4 5 6 |
# File 'lib/diff/lcs/change.rb', line 4 def action @action end |
#new_element ⇒ Object (readonly)
Returns the value of attribute new_element
4 5 6 |
# File 'lib/diff/lcs/change.rb', line 4 def new_element @new_element end |
#new_position ⇒ Object (readonly)
Returns the value of attribute new_position
4 5 6 |
# File 'lib/diff/lcs/change.rb', line 4 def new_position @new_position end |
#old_element ⇒ Object (readonly)
Returns the value of attribute old_element
4 5 6 |
# File 'lib/diff/lcs/change.rb', line 4 def old_element @old_element end |
#old_position ⇒ Object (readonly)
Returns the value of attribute 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
161 |
# File 'lib/diff/lcs/change.rb', line 161 def adding? = action == "+" |
#changed? ⇒ Boolean
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
163 |
# File 'lib/diff/lcs/change.rb', line 163 def deleting? = action == "-" |
#finished_a? ⇒ Boolean
169 |
# File 'lib/diff/lcs/change.rb', line 169 def finished_a? = action == ">" |
#finished_b? ⇒ Boolean
171 |
# File 'lib/diff/lcs/change.rb', line 171 def finished_b? = action == "<" |
#to_a ⇒ Object 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
165 |
# File 'lib/diff/lcs/change.rb', line 165 def unchanged? = action == "=" |