Skip to content

Commit db0aefa

Browse files
author
Kevin Paulisse
committed
Pre-determination of static diff attributes via attr_reader
1 parent 6ea8e40 commit db0aefa

3 files changed

Lines changed: 31 additions & 33 deletions

File tree

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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module V1
1717
# easier to deal with. We recommend using the named options, rather than #raw or the indexed array,
1818
# as the raw object and indexed array are not guaranteed to be stable.
1919
class Diff
20-
attr_reader :raw
20+
attr_reader :raw, :diff_type, :type, :title, :structure
2121

2222
# Constructor: Accepts a diff in the traditional array format and stores it.
2323
# @param raw [Array] Diff in the traditional format
@@ -30,7 +30,21 @@ def initialize(raw)
3030
unless raw.is_a?(Array)
3131
raise ArgumentError, "OctocatalogDiff::API::V1::Diff#initialize expects Array argument (got #{raw.class})"
3232
end
33+
3334
@raw = raw
35+
36+
unless ['+', '-', '~', '!'].include?(raw[0])
37+
raise ArgumentError, 'Invalid first element array: diff type needs to be one of: +, -, ~, !'
38+
end
39+
@diff_type = raw[0]
40+
41+
unless raw[1].is_a?(String)
42+
raise ArgumentError, "Invalid second element array: type-title-structure needs to be a string not #{raw[1].class}"
43+
end
44+
raw_1_split = raw[1].split(/\f/)
45+
@type = raw_1_split[0]
46+
@title = raw_1_split[1]
47+
@structure = raw_1_split[2..-1]
3448
end
3549

3650
# Public: Retrieve an indexed value from the array
@@ -39,12 +53,6 @@ def [](i)
3953
@raw[i]
4054
end
4155

42-
# Public: Get the change type
43-
# @return [String] Change type symbol (~, !, +, -)
44-
def diff_type
45-
@raw[0]
46-
end
47-
4856
# Public: Is this an addition?
4957
# @return [Boolean] True if this is an addition
5058
def addition?
@@ -63,35 +71,17 @@ def change?
6371
diff_type == '~' || diff_type == '!'
6472
end
6573

66-
# Public: Get the resource type
67-
# @return [String] Resource type
68-
def type
69-
@raw[1].split(/\f/)[0]
70-
end
71-
72-
# Public: Get the resource title
73-
# @return [String] Resource title
74-
def title
75-
@raw[1].split(/\f/)[1]
76-
end
77-
78-
# Public: Get the structure of the resource as an array
79-
# @return [Array] Structure of resource
80-
def structure
81-
@raw[1].split(/\f/)[2..-1]
82-
end
83-
8474
# Public: Get the "old" value, i.e. "from" catalog
8575
# @return [?] "old" value
8676
def old_value
87-
return nil if addition?
77+
return if addition?
8878
@raw[2]
8979
end
9080

9181
# Public: Get the "new" value, i.e. "to" catalog
9282
# @return [?] "new" value
9383
def new_value
94-
return nil if removal?
84+
return if removal?
9585
return @raw[2] if addition?
9686
@raw[3]
9787
end
@@ -127,7 +117,7 @@ def new_line
127117
# Public: Get the "old" location, i.e. location in the "from" catalog
128118
# @return [Hash] <file:, line:> of resource
129119
def old_location
130-
return nil if addition?
120+
return if addition?
131121
return @raw[3] if removal?
132122
@raw[4]
133123
end
@@ -136,7 +126,7 @@ def old_location
136126
# @return [Hash] <file:, line:> of resource
137127
def new_location
138128
return @raw[3] if addition?
139-
return nil if removal?
129+
return if removal?
140130
@raw[5]
141131
end
142132

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
expect(OctocatalogDiff::Util::Catalogs).to receive(:new).and_return(catalog_obj)
3030

3131
diffs_obj = double
32-
allow(diffs_obj).to receive(:diffs).and_return([['diff-1'], ['diff-2']])
32+
allow(diffs_obj).to receive(:diffs).and_return([['+', ''], ['-', '']])
3333
expect(OctocatalogDiff::Cli::Diffs).to receive(:new).and_return(diffs_obj)
3434

3535
logger, @logger_str = OctocatalogDiff::Spec.setup_logger
3636
@result = described_class.catalog_diff(logger: logger, node: 'foo')
3737
end
3838

3939
it 'should return the expected data structure' do
40-
expect(@result.diffs[0].raw).to eq(['diff-1'])
41-
expect(@result.diffs[1].raw).to eq(['diff-2'])
40+
expect(@result.diffs[0].raw).to eq(['+', ''])
41+
expect(@result.diffs[1].raw).to eq(['-', ''])
4242
expect(@result.from.raw).to eq(@from_catalog)
4343
expect(@result.to.raw).to eq(@to_catalog)
4444
end

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,15 @@
357357
end
358358

359359
it 'should raise ArgumentError if called with a non-array' do
360-
expect { described_class.new('foo') }.to raise_error(ArgumentError)
360+
expect { described_class.new('foo') }.to raise_error(ArgumentError, /initialize expects Array argument/)
361+
end
362+
363+
it 'should raise ArgumentError if first element is not a valid diff type' do
364+
expect { described_class.new(['chicken', '']) }.to raise_error(ArgumentError, /Invalid first element array/)
365+
end
366+
367+
it 'should raise ArgumentError if second element is not a string' do
368+
expect { described_class.new(['+']) }.to raise_error(ArgumentError, /Invalid second element array/)
361369
end
362370
end
363371
end

0 commit comments

Comments
 (0)