Sha256: ba4e441d3cca8090e5a27189eb6fafb496378ce2a52bf33e09258702518e2ba0

Contents?: true

Size: 960 Bytes

Versions: 1

Compression:

Stored size: 960 Bytes

Contents

require 'frenetic/concerns/hal_linked'
require 'frenetic/concerns/collection_rest_methods'

class Frenetic
  class ResourceCollection < Delegator
    include HalLinked
    include CollectionRestMethods

    def initialize( resource, params = {} )
      @resource_class = resource
      @resources      = []
      @params         = params || {}

      extract_resources!
    end

    def resource_type
      @resource_type ||= @resource_class.to_s.demodulize.underscore
    end

    def collection_key
      @collection_key ||= resource_type.pluralize
    end

    def __getobj__
      @resources
    end

    def __setobj__
      @resources
    end

    def api
      @resources.first.api
    end

  private

    def extract_resources!
      @resources = embedded_collection.collect do |resource|
        @resource_class.new resource
      end
    end

    def embedded_collection
      @params.fetch('_embedded',{}).fetch(collection_key, [])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
frenetic-1.0.0.alpha.1 lib/frenetic/resource_collection.rb