Skip to content

Commit 8482345

Browse files
author
Kevin Paulisse
committed
Add, test, and document change type methods
1 parent 27e9a59 commit 8482345

3 files changed

Lines changed: 110 additions & 44 deletions

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,24 @@ The return value from the `diffs` computed by the [`catalog-diff`](/doc/dev/api/
88

99
## Methods
1010

11-
## Exceptions
11+
#### `#addition?` (Boolean)
12+
13+
Returns true if this diff is an addition (resource exists in new catalog but not old catalog).
14+
15+
#### `#change?` (Boolean)
16+
17+
Returns true if this diff is a change (resource exists in both catalogs but is different between them).
18+
19+
#### `#change_type` (String)
20+
21+
Returns the change type of the diff, which is one of the following characters:
22+
23+
- `+` for addition (resource exists in new catalog but not old catalog)
24+
- `-` for removal (resource exists in old catalog but not new catalog)
25+
- `~` or `!` for change (resource exists in both catalogs but is different between them)
26+
27+
Note: Internally `~` and `!` represent different types of changes, but when presenting the output, these can generally be considered equivalent.
28+
29+
#### `removal?` (Boolean)
30+
31+
Returns true if this diff is a removal (resource exists in old catalog but not new catalog).

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

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,22 @@ def change_type
4040
@raw[0]
4141
end
4242

43-
# Public: Get the change type (English word)
44-
# @return [String] Change type word
45-
def change_type_word
46-
return 'change' if @raw[0] == '!' || @raw[0] == '~'
47-
return 'addition' if @raw[0] == '+'
48-
return 'removal' if @raw[0] == '-'
49-
raise ArgumentError, "No change type corresponds to #{@raw[0].inspect}"
43+
# Public: Is this an addition?
44+
# @return [Boolean] True if this is an addition
45+
def addition?
46+
change_type == '+'
5047
end
5148

52-
# Public: Get the type_title_structure string
53-
# @return [?] Type_title_structure
54-
def type_title_structure
55-
@raw[1]
49+
# Public: Is this a removal?
50+
# @return [Boolean] True if this is an addition
51+
def removal?
52+
change_type == '-'
5653
end
5754

58-
# Public: For shortening, `tts` = `Type Title Structure`
59-
# @return [?] Type_title_structure
60-
def tts
61-
type_title_structure
55+
# Public: Is this a change?
56+
# @return [Boolean] True if this is an change
57+
def change?
58+
change_type == '~' || change_type == '!'
6259
end
6360

6461
# Public: Get the resource type
@@ -82,31 +79,31 @@ def structure
8279
# Public: Get the "old" value, i.e. "from" catalog
8380
# @return [?] "old" value
8481
def old_value
85-
return nil if @raw[0] == '+'
82+
return nil if addition?
8683
@raw[2]
8784
end
8885

8986
# Public: Get the "new" value, i.e. "to" catalog
9087
# @return [?] "old" value
9188
def new_value
92-
return nil if @raw[0] == '-'
93-
return @raw[2] if @raw[0] == '+'
89+
return nil if removal?
90+
return @raw[2] if addition?
9491
@raw[3]
9592
end
9693

9794
# Public: Get the "old" location, i.e. location in the "from" catalog
9895
# @return [Hash] <file:, line:> of resource
9996
def old_location
100-
return nil if @raw[0] == '+'
101-
return @raw[3] if @raw[0] == '-'
97+
return nil if addition?
98+
return @raw[3] if removal?
10299
@raw[4]
103100
end
104101

105102
# Public: Get the "new" location, i.e. location in the "to" catalog
106103
# @return [Hash] <file:, line:> of resource
107104
def new_location
108-
return @raw[3] if @raw[0] == '+'
109-
return nil if @raw[0] == '-'
105+
return @raw[3] if addition?
106+
return nil if removal?
110107
@raw[5]
111108
end
112109

@@ -137,6 +134,38 @@ def new_line
137134
x = new_location
138135
x.nil? ? nil : x['line']
139136
end
137+
138+
# Public: Convert this object to a hash
139+
# @return [Hash] Hash with keys set by these methods
140+
def to_h
141+
{
142+
change_type: change_type,
143+
change_type_word: change_type_word,
144+
type: type,
145+
title: title,
146+
structure: structure,
147+
old_value: old_value,
148+
new_value: new_value,
149+
old_location: old_location,
150+
new_location: new_location,
151+
old_file: old_file,
152+
old_line: old_line,
153+
new_file: new_file,
154+
new_line: new_line
155+
}
156+
end
157+
158+
# Public: String inspection
159+
# @return [String] String for inspection
160+
def inspect
161+
to_h.inspect
162+
end
163+
164+
# Public: To string
165+
# @return [String] Compact string representation
166+
def to_s
167+
raw.inspect
168+
end
140169
end
141170
end
142171
end

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

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,55 @@
4949
end
5050
end
5151

52-
describe '#change_type_word' do
53-
it 'should identify addition' do
54-
testobj = described_class.new(add_1)
55-
expect(testobj.change_type_word).to eq('addition')
52+
describe '#addition?' do
53+
it 'should return true for an addition' do
54+
testobj = described_class.new(add_2)
55+
expect(testobj.addition?).to eq(true)
56+
end
57+
58+
it 'should return false for a removal' do
59+
testobj = described_class.new(del_2)
60+
expect(testobj.addition?).to eq(false)
5661
end
5762

58-
it 'should identify removal' do
59-
testobj = described_class.new(del_1)
60-
expect(testobj.change_type_word).to eq('removal')
63+
it 'should return false for a change' do
64+
testobj = described_class.new(chg_2)
65+
expect(testobj.addition?).to eq(false)
6166
end
67+
end
6268

63-
it 'should identify change with ~' do
64-
testobj = described_class.new(chg_1)
65-
expect(testobj.change_type_word).to eq('change')
69+
describe '#removal?' do
70+
it 'should return true for an addition' do
71+
testobj = described_class.new(add_2)
72+
expect(testobj.removal?).to eq(false)
6673
end
6774

68-
it 'should identify change with !' do
69-
x = chg_1.dup
70-
x[0] = '!'
71-
testobj = described_class.new(x)
72-
expect(testobj.change_type_word).to eq('change')
75+
it 'should return false for a removal' do
76+
testobj = described_class.new(del_2)
77+
expect(testobj.removal?).to eq(true)
7378
end
7479

75-
it 'should raise ArgumentError for unknown symbol' do
76-
x = chg_1.dup
77-
x[0] = '#'
78-
testobj = described_class.new(x)
79-
expect { testobj.change_type_word }.to raise_error(ArgumentError)
80+
it 'should return false for a change' do
81+
testobj = described_class.new(chg_2)
82+
expect(testobj.removal?).to eq(false)
8083
end
8184
end
8285

83-
describe '#type_title' do
86+
describe '#change?' do
87+
it 'should return true for an addition' do
88+
testobj = described_class.new(add_2)
89+
expect(testobj.change?).to eq(false)
90+
end
91+
92+
it 'should return false for a removal' do
93+
testobj = described_class.new(del_2)
94+
expect(testobj.change?).to eq(false)
95+
end
96+
97+
it 'should return false for a change' do
98+
testobj = described_class.new(chg_2)
99+
expect(testobj.change?).to eq(true)
100+
end
84101
end
85102

86103
describe '#type' do

0 commit comments

Comments
 (0)