Class: Diff::LCS::Change
- Inherits:
-
Data
- Object
- Data
- Diff::LCS::Change
- Includes:
- Comparable
- Defined in:
- lib/diff/lcs/change.rb,
lib/diff/lcs/change.rb
Overview
Represents a simplistic (non-contextual) change. Represents the removal or addition of an element from either the old or the new sequenced enumerable.
Constant Summary collapse
- VALID_ACTIONS =
The only actions valid for changes are ‘+’ (add), ‘-’ (delete), ‘=’ (no change), ‘!’ (changed), ‘<’ (tail changes from first sequence), or ‘>’ (tail changes from second sequence). The last two (‘<>’) are only found with Diff::LCS::diff and Diff::LCS::sdiff.
%w[= - + ! > <].freeze
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Class Method Summary collapse
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:, position:, element:) ⇒ Change
constructor
Returns the sequence element of the Change.
- #inspect(*_args) ⇒ Object
- #to_a ⇒ Object (also: #to_ary, #deconstruct)
- #unchanged? ⇒ Boolean
Constructor Details
#initialize(action:, position:, element:) ⇒ Change
Returns the sequence element of the Change. :attr_reader: element
29 30 31 32 33 34 |
# File 'lib/diff/lcs/change.rb', line 29 def initialize(action:, position:, element:) fail "Invalid Change Action '#{action}'" unless Diff::LCS::Change.valid_action?(action) fail "Invalid Position Type" unless position.is_a?(Integer) super end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action
3 4 5 |
# File 'lib/diff/lcs/change.rb', line 3 def action @action end |
#element ⇒ Object (readonly)
Returns the value of attribute element
3 4 5 |
# File 'lib/diff/lcs/change.rb', line 3 def element @element end |
#position ⇒ Object (readonly)
Returns the value of attribute position
3 4 5 |
# File 'lib/diff/lcs/change.rb', line 3 def position @position end |
Class Method Details
.from_a(arr) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/diff/lcs/change.rb', line 44 def self.from_a(arr) case arr in [action, [old_position, old_element], [new_position, new_element]] Diff::LCS::ContextChange[action, old_position, old_element, new_position, new_element] in [action, position, element] new(action, position, element) else fail "Invalid change array format provided." end end |
.valid_action?(action) ⇒ Boolean
15 |
# File 'lib/diff/lcs/change.rb', line 15 def self.valid_action?(action) = VALID_ACTIONS.include?(action) |
Instance Method Details
#<=>(other) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/diff/lcs/change.rb', line 64 def <=>(other) r = position <=> other.position r = VALID_ACTIONS.index(action) <=> VALID_ACTIONS.index(other.action) if r.zero? r = element <=> other.element if r.zero? r end |
#==(other) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/diff/lcs/change.rb', line 57 def ==(other) (self.class == other.class) and (action == other.action) and (position == other.position) and (element == other.element) end |
#adding? ⇒ Boolean
71 |
# File 'lib/diff/lcs/change.rb', line 71 def adding? = action == "+" |
#changed? ⇒ Boolean
77 |
# File 'lib/diff/lcs/change.rb', line 77 def changed? = action == "!" |
#deconstruct_keys(_) ⇒ Object
42 |
# File 'lib/diff/lcs/change.rb', line 42 def deconstruct_keys(_) = {action:, position:, element:} |
#deleting? ⇒ Boolean
73 |
# File 'lib/diff/lcs/change.rb', line 73 def deleting? = action == "-" |
#finished_a? ⇒ Boolean
79 |
# File 'lib/diff/lcs/change.rb', line 79 def finished_a? = action == ">" |
#finished_b? ⇒ Boolean
81 |
# File 'lib/diff/lcs/change.rb', line 81 def finished_b? = action == "<" |
#inspect(*_args) ⇒ Object
36 |
# File 'lib/diff/lcs/change.rb', line 36 def inspect(*_args) = "#<#{self.class}: #{to_a.inspect}>" |
#to_a ⇒ Object Also known as: to_ary, deconstruct
38 |
# File 'lib/diff/lcs/change.rb', line 38 def to_a = [action, position, element] |
#unchanged? ⇒ Boolean
75 |
# File 'lib/diff/lcs/change.rb', line 75 def unchanged? = action == "=" |