class LHS::Collection::InternalCollection::Collection

The internal collection class that includes enumerable and insures to return LHS::Items in case of iterating items

Attributes

raw[RW]

Public Class Methods

new(raw, parent, record) click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 19
def initialize(raw, parent, record)
  self.raw = raw
  @parent = parent
  @record = record
end

Public Instance Methods

compact() click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 39
def compact
  dup.tap do |collection|
    collection.compact! if collection.raw.present?
  end.as_json # do not return an internal collection!
end
compact!() click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 45
def compact!
  self.raw = raw.map do |item|
    if item.is_a?(LHS::Data) && item._request && !item._request.response.success?
      nil
    else
      cast_item(item)
    end
  end.compact
end
each() { |cast_item(item)| ... } click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 29
def each(&_block)
  raw.each do |item|
    if item.is_a? Hash
      yield cast_item(item)
    else
      yield item
    end
  end
end
to_ary() click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 25
def to_ary
  map { |item| item }
end

Private Instance Methods

cast_item(item) click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 57
def cast_item(item)
  data = LHS::Data.new(item, @parent, @record)
  (record_by_href(item) || @record)&.new(data) || data
end
plain_value?(item) click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 68
def plain_value?(item)
  item.is_a?(String) || item.is_a?(Numeric) || item.is_a?(TrueClass) || item.is_a?(FalseClass)
end
record_by_href(item) click to toggle source
# File lib/lhs/concerns/collection/internal_collection.rb, line 62
def record_by_href(item)
  return if plain_value?(item) || item[:href].blank?

  LHS::Record.for_url(item[:href])
end