Class: OctocatalogDiff::API::V1::Diff
- Inherits:
-
Object
- Object
- OctocatalogDiff::API::V1::Diff
- Defined in:
- lib/octocatalog-diff/api/v1/diff.rb
Overview
This class represents a ‘diff` produced by a catalog-diff operation. This has traditionally been stored as an array with:
[0] Type of change - '+', '-', '!', '~'
[1] Type, title, and maybe structure, delimited by "\f"
[2] Content of the "old" catalog
[3] Content of the "new" catalog
[4] File and line of the "old" catalog
[5] File and line of the "new" catalog
This object seeks to preserve this traditional structure, while providing methods to make it easier to deal with. We recommend using the named options, rather than #raw or the indexed array, as the raw object and indexed array are not guaranteed to be stable.
Instance Attribute Summary collapse
-
#diff_type ⇒ Object
readonly
Returns the value of attribute diff_type.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#structure ⇒ Object
readonly
Returns the value of attribute structure.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.factory(object_in) ⇒ OctocatalogDiff::API::V1::Diff
Public: Construct a OctocatalogDiff::API::V1::Diff object from many different types of input.
Instance Method Summary collapse
-
#[](i) ⇒ ?
Public: Retrieve an indexed value from the array.
-
#[]=(i, new_value) ⇒ Object
Public: Set an element of the array.
-
#addition? ⇒ Boolean
Public: Is this an addition?.
-
#change? ⇒ Boolean
Public: Is this a change?.
-
#initialize(raw) ⇒ Diff
constructor
Constructor: Accepts a diff in the traditional array format and stores it.
-
#inspect ⇒ String
Public: String inspection.
-
#new_file ⇒ String
Public: Get the filename from the “new” location.
-
#new_line ⇒ String
Public: Get the line number from the “new” location.
-
#new_location ⇒ Hash
Public: Get the “new” location, i.e.
-
#new_value ⇒ ?
Public: Get the “new” value, i.e.
-
#old_file ⇒ String
Public: Get the filename from the “old” location.
-
#old_line ⇒ String
Public: Get the line number from the “old” location.
-
#old_location ⇒ Hash
Public: Get the “old” location, i.e.
-
#old_value ⇒ ?
Public: Get the “old” value, i.e.
-
#removal? ⇒ Boolean
Public: Is this a removal?.
-
#to_h ⇒ Hash
Public: Convert this object to a hash.
-
#to_h_with_string_keys ⇒ Hash
Public: Convert this object to a hash with string keys.
-
#to_s ⇒ String
Public: To string.
Constructor Details
#initialize(raw) ⇒ Diff
Constructor: Accepts a diff in the traditional array format and stores it.
37 38 39 40 41 42 43 44 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 37 def initialize(raw) unless raw.is_a?(Array) raise ArgumentError, "OctocatalogDiff::API::V1::Diff#initialize expects Array argument (got #{raw.class})" end @raw = raw initialize_helper end |
Instance Attribute Details
#diff_type ⇒ Object (readonly)
Returns the value of attribute diff_type.
20 21 22 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20 def diff_type @diff_type end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
20 21 22 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20 def raw @raw end |
#structure ⇒ Object (readonly)
Returns the value of attribute structure.
20 21 22 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20 def structure @structure end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
20 21 22 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
20 21 22 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20 def type @type end |
Class Method Details
.factory(object_in) ⇒ OctocatalogDiff::API::V1::Diff
Public: Construct a OctocatalogDiff::API::V1::Diff object from many different types of input. This includes passing a OctocatalogDiff::API::V1::Diff object and getting that identical object back.
27 28 29 30 31 32 33 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 27 def self.factory(object_in) return object_in if object_in.is_a?(OctocatalogDiff::API::V1::Diff) return new(object_in) if object_in.is_a?(Array) raise ArgumentError, "Cannot construct OctocatalogDiff::API::V1::Diff from #{object_in.class}" end |
Instance Method Details
#[](i) ⇒ ?
Public: Retrieve an indexed value from the array
48 49 50 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 48 def [](i) @raw[i] end |
#[]=(i, new_value) ⇒ Object
Public: Set an element of the array
54 55 56 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 54 def []=(i, new_value) @raw[i] = new_value end |
#addition? ⇒ Boolean
Public: Is this an addition?
60 61 62 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 60 def addition? diff_type == '+' end |
#change? ⇒ Boolean
Public: Is this a change?
72 73 74 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 72 def change? diff_type == '~' || diff_type == '!' end |
#inspect ⇒ String
Public: String inspection
164 165 166 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 164 def inspect to_h.inspect end |
#new_file ⇒ String
Public: Get the filename from the “new” location
107 108 109 110 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 107 def new_file x = new_location x.nil? ? nil : x['file'] end |
#new_line ⇒ String
Public: Get the line number from the “new” location
114 115 116 117 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 114 def new_line x = new_location x.nil? ? nil : x['line'] end |
#new_location ⇒ Hash
Public: Get the “new” location, i.e. location in the “to” catalog
129 130 131 132 133 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 129 def new_location return @raw[3] if addition? return if removal? @raw[5] end |
#new_value ⇒ ?
Public: Get the “new” value, i.e. “to” catalog
85 86 87 88 89 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 85 def new_value return if removal? return @raw[2] if addition? @raw[3] end |
#old_file ⇒ String
Public: Get the filename from the “old” location
93 94 95 96 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 93 def old_file x = old_location x.nil? ? nil : x['file'] end |
#old_line ⇒ String
Public: Get the line number from the “old” location
100 101 102 103 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 100 def old_line x = old_location x.nil? ? nil : x['line'] end |
#old_location ⇒ Hash
Public: Get the “old” location, i.e. location in the “from” catalog
121 122 123 124 125 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 121 def old_location return if addition? return @raw[3] if removal? @raw[4] end |
#old_value ⇒ ?
Public: Get the “old” value, i.e. “from” catalog
78 79 80 81 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 78 def old_value return if addition? @raw[2] end |
#removal? ⇒ Boolean
Public: Is this a removal?
66 67 68 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 66 def removal? diff_type == '-' end |
#to_h ⇒ Hash
Public: Convert this object to a hash
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 137 def to_h { diff_type: diff_type, type: type, title: title, structure: structure, old_value: old_value, new_value: new_value, old_file: old_file, old_line: old_line, new_file: new_file, new_line: new_line, old_location: old_location, new_location: new_location } end |
#to_h_with_string_keys ⇒ Hash
Public: Convert this object to a hash with string keys
156 157 158 159 160 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 156 def to_h_with_string_keys result = {} to_h.each { |key, val| result[key.to_s] = val } result end |
#to_s ⇒ String
Public: To string
170 171 172 |
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 170 def to_s raw.inspect end |