Skip to content

Commit 0bab80d

Browse files
author
Kevin Paulisse
committed
Add old_location, new_location, and to_h_with_string_keys
1 parent 1c2072f commit 0bab80d

3 files changed

Lines changed: 86 additions & 20 deletions

File tree

doc/dev/api/v1/objects/diff.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Note that this is a pass-through of information provided in the Puppet catalog,
4444

4545
Note also that if the diff represents removal of a resource, this will return `nil`, because the resource does not exist in the new catalog.
4646

47+
#### `#new_location` (Hash)
48+
49+
Returns a hash containing `:file` (equal to `#new_file`) and `:line` (equal to `#new_line`) when either is defined. Returns `nil` if both are undefined.
50+
4751
#### `#new_value` (Object)
4852

4953
Returns the value of the resource from the new catalog.
@@ -111,6 +115,10 @@ Note that this is a pass-through of information provided in the Puppet catalog,
111115

112116
Note also that if the diff represents addition of a resource, this will return `nil`, because the resource does not exist in the old catalog.
113117

118+
#### `#old_location` (Hash)
119+
120+
Returns a hash containing `:file` (equal to `#old_file`) and `:line` (equal to `#old_line`) when either is defined. Returns `nil` if both are undefined.
121+
114122
#### `#old_value` (Object)
115123

116124
Returns the value of the resource from the old catalog.
@@ -249,4 +257,5 @@ These methods are available for debugging or development purposes but are not gu
249257
- `#inspect` (String): Returns inspection of object
250258
- `#raw` (Array): Returns internal array data structure of the "diff"
251259
- `#to_h` (Hash): Returns object as a hash, where keys are above described methods
260+
- `#to_h_with_string_keys` (Hash): Returns object as a hash, where keys are above described methods; keys are strings, not symbols
252261
- `#[]` (Object): Retrieve indexed array elements from raw internal array object

lib/octocatalog-diff/api/v1/diff.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ def new_line
119119
x.nil? ? nil : x['line']
120120
end
121121

122+
# Public: Get the "old" location, i.e. location in the "from" catalog
123+
# @return [Hash] <file:, line:> of resource
124+
def old_location
125+
return nil if addition?
126+
return @raw[3] if removal?
127+
@raw[4]
128+
end
129+
130+
# Public: Get the "new" location, i.e. location in the "to" catalog
131+
# @return [Hash] <file:, line:> of resource
132+
def new_location
133+
return @raw[3] if addition?
134+
return nil if removal?
135+
@raw[5]
136+
end
137+
122138
# Public: Convert this object to a hash
123139
# @return [Hash] Hash with keys set by these methods
124140
def to_h
@@ -132,10 +148,20 @@ def to_h
132148
old_file: old_file,
133149
old_line: old_line,
134150
new_file: new_file,
135-
new_line: new_line
151+
new_line: new_line,
152+
old_location: old_location,
153+
new_location: new_location
136154
}
137155
end
138156

157+
# Public: Convert this object to a hash with string keys
158+
# @return [Hash] Hash with keys set by these methods, with string keys
159+
def to_h_with_string_keys
160+
result = {}
161+
to_h.each { |key, val| result[key.to_s] = val }
162+
result
163+
end
164+
139165
# Public: String inspection
140166
# @return [String] String for inspection
141167
def inspect
@@ -147,24 +173,6 @@ def inspect
147173
def to_s
148174
raw.inspect
149175
end
150-
151-
private
152-
153-
# Private: Get the "old" location, i.e. location in the "from" catalog
154-
# @return [Hash] <file:, line:> of resource
155-
def old_location
156-
return nil if addition?
157-
return @raw[3] if removal?
158-
@raw[4]
159-
end
160-
161-
# Private: Get the "new" location, i.e. location in the "to" catalog
162-
# @return [Hash] <file:, line:> of resource
163-
def new_location
164-
return @raw[3] if addition?
165-
return nil if removal?
166-
@raw[5]
167-
end
168176
end
169177
end
170178
end

spec/octocatalog-diff/tests/api/v1/diff_spec.rb

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,44 @@
273273
end
274274
end
275275

276+
describe '#old_location' do
277+
it 'should return nil for addition' do
278+
testobj = described_class.new(add_2)
279+
expect(testobj.old_location).to be_nil
280+
end
281+
282+
it 'should return location for removal' do
283+
testobj = described_class.new(del_2)
284+
expect(testobj.old_location).to eq(loc_1)
285+
end
286+
287+
it 'should return location for change' do
288+
testobj = described_class.new(chg_2)
289+
expect(testobj.old_location).to eq(loc_1)
290+
end
291+
end
292+
293+
describe '#new_location' do
294+
it 'should return location for addition' do
295+
testobj = described_class.new(add_2)
296+
expect(testobj.new_location).to eq(loc_2)
297+
end
298+
299+
it 'should return nil for removal' do
300+
testobj = described_class.new(del_2)
301+
expect(testobj.new_location).to be_nil
302+
end
303+
304+
it 'should return location for change' do
305+
testobj = described_class.new(chg_2)
306+
expect(testobj.new_location).to eq(loc_2)
307+
end
308+
end
309+
276310
describe '#to_h' do
277311
it 'should return a hash with the expected keys and values' do
278-
methods = %w(diff_type type title structure old_value new_value old_line new_line old_file new_file)
312+
methods = %w(diff_type type title structure old_value new_value)
313+
.concat %w(old_line new_line old_file new_file old_location new_location)
279314
testobj = described_class.new(chg_2)
280315
result = testobj.to_h
281316
methods.each do |method_name|
@@ -286,6 +321,20 @@
286321
end
287322
end
288323

324+
describe '#to_h_with_string_keys' do
325+
it 'should return a hash with the expected keys and values' do
326+
methods = %w(diff_type type title structure old_value new_value)
327+
.concat %w(old_line new_line old_file new_file old_location new_location)
328+
testobj = described_class.new(chg_2)
329+
result = testobj.to_h_with_string_keys
330+
methods.each do |method_name|
331+
method = method_name.to_sym
332+
expect(result.key?(method.to_s)).to eq(true)
333+
expect(result[method.to_s]).to eq(testobj.send(method))
334+
end
335+
end
336+
end
337+
289338
describe '#inspect' do
290339
it 'should return a string' do
291340
testobj = described_class.new(chg_2)

0 commit comments

Comments
 (0)