Sha256: dbdd3a1af705e8c37f737fd33c0cba4a57ba3265a4549ec0e3a3c53d8a12c42a

Contents?: true

Size: 883 Bytes

Versions: 2

Compression:

Stored size: 883 Bytes

Contents

module Hyperclient
  # Public: A wrapper class to easily acces the embedded resources in a
  # Resource.
  #
  # Examples
  #
  #   resource.embedded['comments']
  #   resource.embedded.comments
  #
  class ResourceCollection < Collection
    # Public: Initializes a ResourceCollection.
    #
    # collection  - The Hash with the embedded resources.
    # entry_point - The EntryPoint object to inject the configuration.
    #
    def initialize(collection, entry_point)
      @entry_point = entry_point
      @collection = (collection || {}).reduce({}) do |hash, (name, resource)|
        hash.update(name => build_resource(resource))
      end
    end

    private

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

      Resource.new(representation, @entry_point)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hyperclient-1.0.1 lib/hyperclient/resource_collection.rb
hyperclient-0.9.3 lib/hyperclient/resource_collection.rb