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