@@ -17,20 +17,30 @@ 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
21+
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 . factory ( 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
2134
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
41+
3342 @raw = raw
43+ initialize_helper
3444 end
3545
3646 # Public: Retrieve an indexed value from the array
@@ -39,12 +49,6 @@ def [](i)
3949 @raw [ i ]
4050 end
4151
42- # Public: Get the change type
43- # @return [String] Change type symbol (~, !, +, -)
44- def diff_type
45- @raw [ 0 ]
46- end
47-
4852 # Public: Is this an addition?
4953 # @return [Boolean] True if this is an addition
5054 def addition?
@@ -63,35 +67,17 @@ def change?
6367 diff_type == '~' || diff_type == '!'
6468 end
6569
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-
8470 # Public: Get the "old" value, i.e. "from" catalog
8571 # @return [?] "old" value
8672 def old_value
87- return nil if addition?
73+ return if addition?
8874 @raw [ 2 ]
8975 end
9076
9177 # Public: Get the "new" value, i.e. "to" catalog
92- # @return [?] "old " value
78+ # @return [?] "new " value
9379 def new_value
94- return nil if removal?
80+ return if removal?
9581 return @raw [ 2 ] if addition?
9682 @raw [ 3 ]
9783 end
@@ -127,7 +113,7 @@ def new_line
127113 # Public: Get the "old" location, i.e. location in the "from" catalog
128114 # @return [Hash] <file:, line:> of resource
129115 def old_location
130- return nil if addition?
116+ return if addition?
131117 return @raw [ 3 ] if removal?
132118 @raw [ 4 ]
133119 end
@@ -136,7 +122,7 @@ def old_location
136122 # @return [Hash] <file:, line:> of resource
137123 def new_location
138124 return @raw [ 3 ] if addition?
139- return nil if removal?
125+ return if removal?
140126 @raw [ 5 ]
141127 end
142128
@@ -178,6 +164,24 @@ def inspect
178164 def to_s
179165 raw . inspect
180166 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
181185 end
182186 end
183187 end
0 commit comments