Sha256: 58c3e5ff944d2d90f9275090001dc01a9bae62e32a264e9e514aabe5be198356

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

require File.join(__dir__, 'proxy.rb')
Dir[File.dirname(__FILE__) + '/concerns/collection/*.rb'].each { |file| require file }

# A collection is a special type of data
# that contains multiple items
class LHS::Collection < LHS::Proxy
  include InternalCollection
  include Create

  delegate :select, :length, :size, to: :_collection
  delegate :_record, :_raw, to: :_data
  delegate :limit, :count, :total, :offset, :current_page, :start,
           :next?, :previous?, to: :_pagination

  def _pagination
    _record.pagination(_data)
  end

  def href
    _data._raw[:href]
  end

  def _collection
    raw = _data._raw if _data._raw.is_a?(Array)
    raw ||= _data._raw[items_key]
    Collection.new(raw, _data, _record)
  end

  def collection?
    true
  end

  def item?
    false
  end

  protected

  def method_missing(name, *args, &block)
    if _collection.respond_to?(name)
      value = _collection.send(name, *args, &block)
      return enclose_in_data(value) if value.is_a? Hash
      value
    elsif _data._raw.is_a?(Hash)
      get(name, *args)
    end
  end

  def respond_to_missing?(name, _include_all = false)
    # We accept every message that does not belong to set of keywords and is not a setter
    BLACKLISTED_KEYWORDS.exclude?(name.to_s) && !name.to_s[/=$/]
  end

  private

  def items_key
    return _record.items_key if _record
    :items
  end

  def enclose_in_data(value)
    data = LHS::Data.new(value, _data)
    item = LHS::Item.new(data)
    LHS::Data.new(item, _data)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lhs-8.0.0 lib/lhs/collection.rb
lhs-7.4.1 lib/lhs/collection.rb
lhs-7.4.0 lib/lhs/collection.rb