Sha256: bad7f5e411600180c4d753b7cd805db082d2db571d384e50d749185d48749a10

Contents?: true

Size: 1.08 KB

Versions: 32

Compression:

Stored size: 1.08 KB

Contents

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, :last, :sample, :[], :present?, :blank?, :empty?,
               :<<, :push, 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

32 entries across 32 versions & 1 rubygems

Version Path
lhs-14.6.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.6.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.6.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.5.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.4.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.3.4 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.3.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.3.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.3.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.3.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.2.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.1.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.1.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.0.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.0.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.0.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-14.0.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-13.2.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-13.2.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-13.2.1 lib/lhs/concerns/collection/internal_collection.rb