Sha256: f68dacb1f855eaf17413fa8800ddc5cde8b77f736046a1c31c57197941115366

Contents?: true

Size: 1.07 KB

Versions: 31

Compression:

Stored size: 1.07 KB

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?,
               :<<, :push, 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)
        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

31 entries across 31 versions & 1 rubygems

Version Path
lhs-11.3.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.3.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.3.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.2.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.2.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.2.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.1.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.0.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.0.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.0.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-11.0.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-10.1.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-10.1.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-10.0.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-9.1.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-9.1.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-9.0.4 lib/lhs/concerns/collection/internal_collection.rb
lhs-9.0.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-9.0.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-9.0.1 lib/lhs/concerns/collection/internal_collection.rb