@@ -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
0 commit comments