Sha256: e473ea01ea75682311c6fd333d77396b2710d103d19fdcaf906b838556a2806f

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module RemoteResource
  class Collection
    include Enumerable

    attr_reader :resource_klass, :resources_collection
    attr_accessor :last_request, :last_response, :meta

    delegate :[], :at, :reverse, :size, to: :to_a

    def initialize(resource_klass, resources_collection, options = {})
      @resource_klass       = resource_klass
      @resources_collection = resources_collection
      @options              = options
    end

    def each(&block)
      if resources_collection.is_a?(Array)
        if defined?(@collection)
          @collection.each(&block)
        else
          @collection = []
          resources_collection.each do |element|
            record = resource_klass.new(element.merge(@options))
            @collection.push(record)
            yield(record)
          end
        end
      end
    end

    def empty?
      resources_collection.blank?
    end

    def success?
      last_response.success?
    end

    def last_request
      @last_request ||= @options[:last_request]
    end

    def last_response
      @last_response ||= @options[:last_response]
    end

    def meta
      @meta ||= @options[:meta]
    end

    def _response
      warn '[DEPRECATION] `._response` is deprecated. Please use `.last_response` instead.'
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ddy_remote_resource-1.0.0.rc5 lib/remote_resource/collection.rb
ddy_remote_resource-1.0.0.rc4 lib/remote_resource/collection.rb
ddy_remote_resource-1.0.0.rc3 lib/remote_resource/collection.rb