Sha256: ec1af8a0bbe08c325c52abd9fef078ae2d52686157891917db236930168ccb44

Contents?: true

Size: 957 Bytes

Versions: 7

Compression:

Stored size: 957 Bytes

Contents

require 'active_support'
require File.dirname(__FILE__) + '/../../proxy'

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?, 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

7 entries across 7 versions & 1 rubygems

Version Path
lhs-5.0.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-5.0.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-5.0.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-4.2.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-4.2.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-4.1.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-4.0.0 lib/lhs/concerns/collection/internal_collection.rb