Sha256: 9fd40b0914266b21a2621bee714ae3d74e039add224a4db2a36536f35bd840d0

Contents?: true

Size: 772 Bytes

Versions: 5

Compression:

Stored size: 772 Bytes

Contents

# frozen_string_literal: true

module ActionTracker
  class CollectionProxy
    include Enumerable
    include ActionTracker::Pagination

    attr_reader :model_name

    def initialize(response, model_name)
      @response = response
      @model_name = model_name
    end

    def code
      @response.code
    end

    def each
      raw_data.each do |record|
        yield record_klass.new(record)
      end
    end

    def raw_data
      @response.dig(model_name.plural) || @response.dig('data')
    end

    def entry_name(options = {})
      model_name.singular.pluralize(options[:count])
    end

    private

    def meta
      OpenStruct.new(@response.dig('meta'))
    end

    def record_klass
      model_name.instance_variable_get(:@klass)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
action_tracker_client-0.1.8 lib/action_tracker/utils/collection_proxy.rb
action_tracker_client-0.1.7 lib/action_tracker/utils/collection_proxy.rb
action_tracker_client-0.1.6 lib/action_tracker/utils/collection_proxy.rb
action_tracker_client-0.1.5 lib/action_tracker/utils/collection_proxy.rb
action_tracker_client-0.1.4 lib/action_tracker/utils/collection_proxy.rb