Sha256: 34516469d1341be9860c8f05f7ec9f9e5a53acb83a6aba9527045f571d59e615

Contents?: true

Size: 1.41 KB

Versions: 10

Compression:

Stored size: 1.41 KB

Contents

module Neo4j
  module Core
    module Cypher
      # Wraps the Cypher query result.
      # Loads the node and relationships wrapper if possible and use symbol as column keys.
      # @notice The result is a once forward read only Enumerable, work if you need to read the result twice - use #to_a
      #
      # @example
      #   result = Neo4j.query(@a, @b){|a,b| node(a,b).as(:n)}
      #   r = @query_result.to_a # can only loop once
      #   r.size.should == 2
      #   r.first.should include(:n)
      #   r[0][:n].neo_id.should == @a.neo_id
      #   r[1][:n].neo_id.should == @b.neo_id
      class ResultWrapper
        include Enumerable

        # @return the original result from the Neo4j Cypher Engine, once forward read only !
        attr_reader :source

        def initialize(source)
          @source = source
        end

        # @return [Array<Symbol>] the columns in the query result
        def columns
          @source.columns.map{|x| x.to_sym}
        end

        # for the Enumerable contract
        def each
          @source.each { |row| yield map(row) }
        end

        # Maps each row so that we can use symbols for column names.
        # @private
        def map(row)
          out = {} # move to a real hash!
          row.each do |key, value|
            out[key.to_sym] = value.respond_to?(:wrapper) ? value.wrapper : value
          end
          out
        end

      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
neo4j-core-2.1.0-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-2.0.1-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-2.0.0-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-2.0.0.rc2-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-2.0.0.rc1-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-0.0.15-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-0.0.14-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-0.0.13-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-0.0.12-java lib/neo4j-core/cypher/result_wrapper.rb
neo4j-core-0.0.11-java lib/neo4j-core/cypher/result_wrapper.rb