Skip to content

Commit 7b1cff3

Browse files
author
Kevin Paulisse
committed
Handle diff method being initialized with an instance of itself
1 parent c4d3b99 commit 7b1cff3

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ class Diff
2222
# Constructor: Accepts a diff in the traditional array format and stores it.
2323
# @param raw [Array] Diff in the traditional format
2424
def initialize(raw)
25+
if raw.is_a?(OctocatalogDiff::API::V1::Diff)
26+
@raw = raw.raw
27+
return
28+
end
29+
2530
unless raw.is_a?(Array)
26-
raise ArgumentError, 'OctocatalogDiff::API::V1::Diff#initialize expects Array argument'
31+
raise ArgumentError, "OctocatalogDiff::API::V1::Diff#initialize expects Array argument (got #{raw.class})"
2732
end
2833
@raw = raw
2934
end

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@
350350
end
351351

352352
describe '#initialize' do
353+
it 'should set up raw object when called with an instance of itself' do
354+
obj1 = described_class.new(chg_2)
355+
testobj = described_class.new(obj1)
356+
expect(testobj.raw).to eq(chg_2)
357+
end
358+
353359
it 'should raise ArgumentError if called with a non-array' do
354360
expect { described_class.new('foo') }.to raise_error(ArgumentError)
355361
end

0 commit comments

Comments
 (0)