lib/hcloud/entry_loader.rb in hcloud-1.2.0 vs lib/hcloud/entry_loader.rb in hcloud-1.3.0

- old
+ new

@@ -221,51 +221,19 @@ def _update_attribute(key, value) _attributes[key] = value instance_variable_set("@#{key}", value) end - # rubocop: disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize def _load(resource) return if resource.nil? @_attributes = {}.with_indifferent_access - resource.each do |key, value| - definition = self.class.schema[key] + loader = Hcloud::ResourceLoader.new(self.class.schema, client: client) + loaded_data = loader.load(resource) - if definition == :time - _update_attribute(key, value ? Time.parse(value) : nil) - next - end - - if definition.is_a?(Class) && definition.include?(EntryLoader) - _update_attribute(key, value ? definition.new(client, value) : nil) - next - end - - # if schema definition is [Class] - if definition.is_a?(Array) && definition.first.include?(EntryLoader) - - # just set attribute to an empty array if value is no array or empty - if !value.is_a?(Array) || value.empty? - _update_attribute(key, []) - next - end - - if value.first.is_a?(Integer) - # If value is an integer, this is the id of an object which's class can be - # retreived from definition. Load a future object that can on access retreive the - # data from the api and convert it to a proper object. - _update_attribute(key, value.map { |id| Future.new(client, definition.first, id) }) - else - # Otherwise the value *is* the content of the object - _update_attribute(key, value.map { |item| definition.first.new(client, item) }) - end - next - end - + loaded_data.each do |key, value| _update_attribute(key, value.is_a?(Hash) ? value.with_indifferent_access : value) end end - # rubocop: enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize end end