Class: Hyperclient::ResourceCollection

Inherits:
Collection
  • Object
show all
Defined in:
lib/hyperclient/resource_collection.rb

Overview

A wrapper class to easily acces the embedded resources in a Resource.

Examples:

resource.embedded['comments']
resource.embedded.comments

Instance Method Summary collapse

Methods inherited from Collection

#[], #each, #fetch, #include?, #method_missing, #respond_to_missing?, #to_h, #to_s

Constructor Details

#initialize(collection, entry_point) ⇒ ResourceCollection

Initializes a ResourceCollection.

Parameters:

  • collection

    The Hash with the embedded resources.

  • entry_point

    The EntryPoint object to inject the configuration.


16
17
18
19
20
21
# File 'lib/hyperclient/resource_collection.rb', line 16

def initialize(collection, entry_point)
  @entry_point = entry_point
  @collection = (collection || {}).reduce({}) do |hash, (name, resource)|
    hash.update(name => build_resource(resource))
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hyperclient::Collection

Instance Method Details

#build_resource(representation) ⇒ Object (private)


25
26
27
28
29
30
31
# File 'lib/hyperclient/resource_collection.rb', line 25

def build_resource(representation)
  if representation.is_a?(Array)
    return representation.map(&method(:build_resource))
  end

  Resource.new(representation, @entry_point)
end