Sha256: 801ffcc5408f16147e75495a829ecae5bcfd95eae6a8c0876e628c489d831a3a

Contents?: true

Size: 1.98 KB

Versions: 24

Compression:

Stored size: 1.98 KB

Contents

# A collection is a special type of data
# that contains multiple items
class LHS::Collection < LHS::Proxy
  autoload :HandleNested,
    'lhs/concerns/collection/handle_nested'
  autoload :InternalCollection,
    'lhs/concerns/collection/internal_collection'

  include HandleNested
  include InternalCollection
  include Create

  METHOD_NAMES_EXLCUDED_FROM_WRAPPING = %w(to_a to_ary map).freeze

  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
    return _data._raw[:href] if _data._raw.is_a? Hash
    nil
  end

  def _collection
    raw = _data._raw if _data._raw.is_a?(Array)
    raw ||= _data.access(input: _data._raw, record: _record)
    Collection.new(raw, _data, _record)
  end

  def collection?
    true
  end

  def item?
    false
  end

  def raw_items
    if _raw.is_a?(Array)
      _raw
    else
      access(input: _raw, record: _record)
    end
  end

  protected

  def method_missing(name, *args, &block)
    if _collection.respond_to?(name)
      value = _collection.send(name, *args, &block)
      record = LHS::Record.for_url(value[:href]) if value.is_a?(Hash) && value[:href]
      value = enclose_item_in_data(value) if value.is_a?(Hash)
      return value if METHOD_NAMES_EXLCUDED_FROM_WRAPPING.include?(name.to_s)
      wrap_return(value, record, name, args)
    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.include?(name.to_s) && !name.to_s[/=$/]
  end

  private

  # Encloses accessed collection item
  # by wrapping it in an LHS::Item
  def enclose_item_in_data(value)
    data = LHS::Data.new(value, _data)
    item = LHS::Item.new(data)
    LHS::Data.new(item, _data)
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
lhs-15.4.1 lib/lhs/collection.rb
lhs-15.4.0 lib/lhs/collection.rb
lhs-15.4.0.pre.hasone.1 lib/lhs/collection.rb
lhs-15.3.3 lib/lhs/collection.rb
lhs-15.3.3.pre.fixoptions.1 lib/lhs/collection.rb
lhs-15.3.2 lib/lhs/collection.rb
lhs-15.3.1 lib/lhs/collection.rb
lhs-15.3.1.pre.fixlhc.1 lib/lhs/collection.rb
lhs-15.3.0 lib/lhs/collection.rb
lhs-15.2.5 lib/lhs/collection.rb
lhs-15.2.4 lib/lhs/collection.rb
lhs-15.2.3 lib/lhs/collection.rb
lhs-15.2.3.pre.favorites.1 lib/lhs/collection.rb
lhs-15.2.2.pre.favorites.1 lib/lhs/collection.rb
lhs-15.2.2 lib/lhs/collection.rb
lhs-15.2.1 lib/lhs/collection.rb
lhs-15.2.0 lib/lhs/collection.rb
lhs-15.1.1 lib/lhs/collection.rb
lhs-15.1.0 lib/lhs/collection.rb
lhs-15.0.2 lib/lhs/collection.rb