@@ -223,49 +223,17 @@ def _update_attribute(key, value)
223223 instance_variable_set ( "@#{ key } " , value )
224224 end
225225
226- # rubocop: disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize
227226 def _load ( resource )
228227 return if resource . nil?
229228
230229 @_attributes = { } . with_indifferent_access
231230
232- resource . each do |key , value |
233- definition = self . class . schema [ key ]
234-
235- if definition == :time
236- _update_attribute ( key , value ? Time . parse ( value ) : nil )
237- next
238- end
239-
240- if definition . is_a? ( Class ) && definition . include? ( EntryLoader )
241- _update_attribute ( key , value ? definition . new ( client , value ) : nil )
242- next
243- end
244-
245- # if schema definition is [Class]
246- if definition . is_a? ( Array ) && definition . first . include? ( EntryLoader )
247-
248- # just set attribute to an empty array if value is no array or empty
249- if !value . is_a? ( Array ) || value . empty?
250- _update_attribute ( key , [ ] )
251- next
252- end
253-
254- if value . first . is_a? ( Integer )
255- # If value is an integer, this is the id of an object which's class can be
256- # retreived from definition. Load a future object that can on access retreive the
257- # data from the api and convert it to a proper object.
258- _update_attribute ( key , value . map { |id | Future . new ( client , definition . first , id ) } )
259- else
260- # Otherwise the value *is* the content of the object
261- _update_attribute ( key , value . map { |item | definition . first . new ( client , item ) } )
262- end
263- next
264- end
231+ loader = Hcloud ::ResourceLoader . new ( self . class . schema , client : client )
232+ loaded_data = loader . load ( resource )
265233
234+ loaded_data . each do |key , value |
266235 _update_attribute ( key , value . is_a? ( Hash ) ? value . with_indifferent_access : value )
267236 end
268237 end
269- # rubocop: enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize
270238 end
271239end
0 commit comments