Sha256: 2bd5c19babed3fa1b9096823fa9c1296e7cfe1c18dc220ae46252611687ccef1

Contents?: true

Size: 1.13 KB

Versions: 60

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'active_support'

class LHS::Collection < LHS::Proxy

  module InternalCollection
    extend ActiveSupport::Concern

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

      attr_accessor :raw
      delegate :length, :size, :first, :last, :sample, :[], :present?, :blank?, :empty?,
               :<<, :push, :compact, to: :raw

      def initialize(raw, parent, record)
        self.raw = raw
        @parent = parent
        @record = record
      end

      def to_ary
        map { |item| item }
      end

      def each(&_block)
        raw.each do |item|
          if item.is_a? Hash
            yield cast_item(item)
          else
            yield item
          end
        end
      end

      private

      def cast_item(item)
        record_by_href = LHS::Record.for_url(item[:href]) if item[:href]
        data = LHS::Data.new(item, @parent, @record)
        return (record_by_href || @record).new(data) if record_by_href || @record
        data
      end
    end
  end
end

Version data entries

60 entries across 60 versions & 1 rubygems

Version Path
lhs-21.1.2.pre.keyfix.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-21.1.2.pre.fix.pre.cant.pre.add.pre.new.pre.key.pre.during.pre.iteration.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-21.1.1.pre.pre.fix.pre.cant.pre.add.pre.new.pre.key.pre.during.pre.iteration.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-21.1.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-21.0.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-20.1.4 lib/lhs/concerns/collection/internal_collection.rb
lhs-20.1.3.pre.paginationfix.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-20.1.3.pre.paginationfix.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-20.1.3.pre.paginationfix.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-20.1.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-20.1.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-20.0.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.10.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.9.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.8.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.8.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.8.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.7.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.6.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-19.5.0 lib/lhs/concerns/collection/internal_collection.rb