Skip to content

Commit d9b202b

Browse files
author
Kevin Paulisse
committed
Add a construct method that may return the original object
1 parent 50dd5f9 commit d9b202b

2 files changed

Lines changed: 48 additions & 22 deletions

File tree

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,28 @@ module V1
1919
class Diff
2020
attr_reader :raw, :diff_type, :type, :title, :structure
2121

22+
# Public: Construct a OctocatalogDiff::API::V1::Diff object from many different types of
23+
# input. This includes passing a OctocatalogDiff::API::V1::Diff object and getting that
24+
# identical object back.
25+
# @param object_in [?] Object in
26+
# @return [OctocatalogDiff::API::V1::Diff] Object out
27+
def self.construct(object_in)
28+
return object_in if object_in.is_a?(OctocatalogDiff::API::V1::Diff)
29+
30+
return new(object_in) if object_in.is_a?(Array)
31+
32+
raise ArgumentError, "Cannot construct OctocatalogDiff::API::V1::Diff from #{object_in.class}"
33+
end
34+
2235
# Constructor: Accepts a diff in the traditional array format and stores it.
2336
# @param raw [Array] Diff in the traditional format
2437
def initialize(raw)
25-
if raw.is_a?(OctocatalogDiff::API::V1::Diff)
26-
@raw = raw.raw
27-
return
28-
end
29-
3038
unless raw.is_a?(Array)
3139
raise ArgumentError, "OctocatalogDiff::API::V1::Diff#initialize expects Array argument (got #{raw.class})"
3240
end
3341

3442
@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]
43+
initialize_helper
4844
end
4945

5046
# Public: Retrieve an indexed value from the array
@@ -168,6 +164,24 @@ def inspect
168164
def to_s
169165
raw.inspect
170166
end
167+
168+
private
169+
170+
# Private: Initialize further instance variables
171+
def initialize_helper
172+
unless ['+', '-', '~', '!'].include?(@raw[0])
173+
raise ArgumentError, 'Invalid first element array: diff type needs to be one of: +, -, ~, !'
174+
end
175+
@diff_type = @raw[0]
176+
177+
unless @raw[1].is_a?(String)
178+
raise ArgumentError, "Invalid second element array: type-title-structure needs to be a string not #{@raw[1].class}"
179+
end
180+
raw_1_split = @raw[1].split(/\f/)
181+
@type = raw_1_split[0]
182+
@title = raw_1_split[1]
183+
@structure = raw_1_split[2..-1]
184+
end
171185
end
172186
end
173187
end

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,25 @@
349349
end
350350
end
351351

352-
describe '#initialize' do
353-
it 'should set up raw object when called with an instance of itself' do
352+
describe '#self.construct' do
353+
it 'should return object as-is when passed a OctocatalogDiff::API::V1::Diff' do
354354
obj1 = described_class.new(chg_2)
355-
testobj = described_class.new(obj1)
356-
expect(testobj.raw).to eq(chg_2)
355+
testobj = described_class.construct(obj1)
356+
expect(testobj).to eq(obj1)
357+
end
358+
359+
it 'should return new OctocatalogDiff::API::V1::Diff when passed an array' do
360+
obj1 = described_class.construct(chg_2)
361+
expect(obj1).to be_a_kind_of(OctocatalogDiff::API::V1::Diff)
362+
expect(obj1.raw).to eq(chg_2)
357363
end
358364

365+
it 'should raise error when passed something else' do
366+
expect { described_class.construct(foo: true) }.to raise_error(ArgumentError, /Cannot construct .+ from Hash/)
367+
end
368+
end
369+
370+
describe '#initialize' do
359371
it 'should raise ArgumentError if called with a non-array' do
360372
expect { described_class.new('foo') }.to raise_error(ArgumentError, /initialize expects Array argument/)
361373
end

0 commit comments

Comments
 (0)