Sha256: 4930cc471b564dc28a656197b3fde75350250d9b78712a82ece07afcf33205ad

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

require File.join(__dir__, 'proxy.rb')
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 :last, :sample, :[], :present?, :blank?, :empty?, to: :raw

      def initialize(raw, parent, record)
        self.raw = raw
        @parent = parent
        @record = record
      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)
        data = LHS::Data.new(item, @parent, @record)
        return @record.new(data) if @record
        data
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lhs-3.3.2 lib/lhs/concerns/collection/internal_collection.rb