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
- 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(+ - = ! > <)
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 ⇒ Object
- #to_a ⇒ Object
- #unchanged? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Change
Returns a new instance of Change.
25 26 27 28 29 30 31 32 |
# File 'lib/diff/lcs/change.rb', line 25 def initialize(*args) @action, @position, @element = *args unless Diff::LCS::Change.valid_action?(@action) raise "Invalid Change Action '#{@action}'" end raise "Invalid Position Type" unless @position.kind_of? Fixnum end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the action this Change represents.
18 19 20 |
# File 'lib/diff/lcs/change.rb', line 18 def action @action end |
#element ⇒ Object (readonly)
Returns the sequence element of the Change.
23 24 25 |
# File 'lib/diff/lcs/change.rb', line 23 def element @element end |
#position ⇒ Object (readonly)
Returns the position of the Change.
21 22 23 |
# File 'lib/diff/lcs/change.rb', line 21 def position @position end |
Class Method Details
.from_a(arr) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/diff/lcs/change.rb', line 42 def self.from_a(arr) arr = arr.flatten case arr.size when 5 Diff::LCS::ContextChange.new(*(arr[0...5])) when 3 Diff::LCS::Change.new(*(arr[0...3])) else raise "Invalid change array format provided." end end |
.valid_action?(action) ⇒ Boolean
13 14 15 |
# File 'lib/diff/lcs/change.rb', line 13 def self.valid_action?(action) VALID_ACTIONS.include? action end |
Instance Method Details
#<=>(other) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/diff/lcs/change.rb', line 62 def <=>(other) r = self.action <=> other.action r = self.position <=> other.position if r.zero? r = self.element <=> other.element if r.zero? r end |
#==(other) ⇒ Object
56 57 58 59 60 |
# File 'lib/diff/lcs/change.rb', line 56 def ==(other) (self.action == other.action) and (self.position == other.position) and (self.element == other.element) end |
#adding? ⇒ Boolean
69 70 71 |
# File 'lib/diff/lcs/change.rb', line 69 def adding? @action == '+' end |
#changed? ⇒ Boolean
81 82 83 |
# File 'lib/diff/lcs/change.rb', line 81 def changed? @action == '!' end |
#deleting? ⇒ Boolean
73 74 75 |
# File 'lib/diff/lcs/change.rb', line 73 def deleting? @action == '-' end |
#finished_a? ⇒ Boolean
85 86 87 |
# File 'lib/diff/lcs/change.rb', line 85 def finished_a? @action == '>' end |
#finished_b? ⇒ Boolean
89 90 91 |
# File 'lib/diff/lcs/change.rb', line 89 def finished_b? @action == '<' end |
#inspect ⇒ Object
34 35 36 |
# File 'lib/diff/lcs/change.rb', line 34 def inspect to_a.inspect end |
#to_a ⇒ Object
38 39 40 |
# File 'lib/diff/lcs/change.rb', line 38 def to_a [ @action, @position, @element ] end |
#unchanged? ⇒ Boolean
77 78 79 |
# File 'lib/diff/lcs/change.rb', line 77 def unchanged? @action == '=' end |