Class: Diff::LCS::Change
- Inherits:
-
Object
- Object
- Diff::LCS::Change
- Includes:
- Comparable
- Defined in:
- 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.
Direct Known Subclasses
Constant Summary collapse
- IntClass =
Fixnum is deprecated in Ruby 2.4 # rubocop:disable Naming/ConstantName
1.class
- 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 action this Change represents.
-
#element ⇒ Object
readonly
Returns the sequence element of the Change.
-
#position ⇒ Object
readonly
Returns the position of the Change.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #adding? ⇒ Boolean
- #changed? ⇒ Boolean
- #deleting? ⇒ Boolean
- #finished_a? ⇒ Boolean
- #finished_b? ⇒ Boolean
-
#initialize(*args) ⇒ Change
constructor
A new instance of Change.
- #inspect(*_args) ⇒ Object
- #to_a ⇒ Object (also: #to_ary)
- #unchanged? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Change
Returns a new instance of Change.
27 28 29 30 31 32 |
# File 'lib/diff/lcs/change.rb', line 27 def initialize(*args) @action, @position, @element = *args fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action) fail "Invalid Position Type" unless @position.is_a? IntClass end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the action this Change represents.
20 21 22 |
# File 'lib/diff/lcs/change.rb', line 20 def action @action end |
#element ⇒ Object (readonly)
Returns the sequence element of the Change.
25 26 27 |
# File 'lib/diff/lcs/change.rb', line 25 def element @element end |
#position ⇒ Object (readonly)
Returns the position of the Change.
23 24 25 |
# File 'lib/diff/lcs/change.rb', line 23 def position @position end |
Class Method Details
.from_a(arr) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/diff/lcs/change.rb', line 44 def self.from_a(arr) arr = arr.flatten(1) case arr.size when 5 Diff::LCS::ContextChange.new(*(arr[0...5])) when 3 Diff::LCS::Change.new(*(arr[0...3])) else fail "Invalid change array format provided." end end |
.valid_action?(action) ⇒ Boolean
15 16 17 |
# File 'lib/diff/lcs/change.rb', line 15 def self.valid_action?(action) VALID_ACTIONS.include? action end |
Instance Method Details
#<=>(other) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/diff/lcs/change.rb', line 65 def <=>(other) r = action <=> other.action r = position <=> other.position if r.zero? r = element <=> other.element if r.zero? r end |
#==(other) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/diff/lcs/change.rb', line 58 def ==(other) (self.class == other.class) and (action == other.action) and (position == other.position) and (element == other.element) end |
#adding? ⇒ Boolean
72 73 74 |
# File 'lib/diff/lcs/change.rb', line 72 def adding? @action == "+" end |
#changed? ⇒ Boolean
84 85 86 |
# File 'lib/diff/lcs/change.rb', line 84 def changed? @action == "!" end |
#deleting? ⇒ Boolean
76 77 78 |
# File 'lib/diff/lcs/change.rb', line 76 def deleting? @action == "-" end |
#finished_a? ⇒ Boolean
88 89 90 |
# File 'lib/diff/lcs/change.rb', line 88 def finished_a? @action == ">" end |
#finished_b? ⇒ Boolean
92 93 94 |
# File 'lib/diff/lcs/change.rb', line 92 def finished_b? @action == "<" end |
#inspect(*_args) ⇒ Object
34 35 36 |
# File 'lib/diff/lcs/change.rb', line 34 def inspect(*_args) "#<#{self.class}: #{to_a.inspect}>" end |
#to_a ⇒ Object Also known as: to_ary
38 39 40 |
# File 'lib/diff/lcs/change.rb', line 38 def to_a [@action, @position, @element] end |
#unchanged? ⇒ Boolean
80 81 82 |
# File 'lib/diff/lcs/change.rb', line 80 def unchanged? @action == "=" end |