Sha256: 4511c3c0bb958852a62162790e278a9b288f10003df442c5cd75e3c357576727

Contents?: true

Size: 1.04 KB

Versions: 38

Compression:

Stored size: 1.04 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?, 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

38 entries across 38 versions & 1 rubygems

Version Path
lhs-7.1.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-7.0.3 lib/lhs/concerns/collection/internal_collection.rb
lhs-7.0.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-7.0.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-7.0.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.8.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.8.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.8.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.7.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.7.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.7.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.6.2 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.6.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.6.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.5.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.4.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.3.1 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.3.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.2.0 lib/lhs/concerns/collection/internal_collection.rb
lhs-6.1.0 lib/lhs/concerns/collection/internal_collection.rb