Sha256: 64c086f3f99ff0d723649f352d7556620885dac2e5f70a314983553f9419582b

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module Rails
  module GraphQL
    module Helpers
      # The base class for inherited collections. It helps softly dealing with
      # values defined in multiple class instance variables and look up the tree
      # to make it possible to iterate over combined result
      class InheritedCollection::Base
        include Enumerable

        delegate :eager, to: :each

        # Just a little helper to initialize the iterator form a given +source+
        def self.handle(source, ivar, type)
          klass = (type == :array || type == :set) ? :Array : :Hash
          InheritedCollection.const_get(klass, false).new(source, ivar, type)
        end

        def initialize(source, ivar, type)
          @source = source
          @ivar = ivar
          @type = type
        end

        # Overrides the lazy enumerator because each instance variable defined
        # and found by the +each_definition+ will be iterated over lazily
        def lazy
          Enumerator::Lazy.new(each_definition) { |y, d| d.each(&y) }
        end

        protected

          def each_definition
            Enumerator.new do |yielder|
              current = @source
              until current === Object
                yielder << current.instance_variable_get(@ivar) \
                  if current.instance_variable_defined?(@ivar)
                current = current.superclass
              end
            end
          end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails-graphql-1.0.2 lib/rails/graphql/helpers/inherited_collection/base.rb
rails-graphql-1.0.1 lib/rails/graphql/helpers/inherited_collection/base.rb
rails-graphql-1.0.0 lib/rails/graphql/helpers/inherited_collection/base.rb
rails-graphql-1.0.0.rc2 lib/rails/graphql/helpers/inherited_collection/base.rb