Sha256: 3c4c882157dfadfcf1fcdeb13f96a51f1d894d23dc45ba5b9caf2729cdeeb8e2

Contents?: true

Size: 1.87 KB

Versions: 38

Compression:

Stored size: 1.87 KB

Contents

module Neo4j
  module Embedded
    # Wraps the Cypher query result.
    # Loads the node and relationships wrapper if possible and use symbol as column keys.
    # This is typically used in the native neo4j bindings since result does is not a Ruby enumerable with symbols as keys.
    # @note The result is a once forward read only Enumerable, work if you need to read the result twice - use #to_a
    #
    class ResultWrapper
      class ResultsAlreadyConsumedException < Exception
      end

      include Enumerable

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

      def initialize(source, query, unwrapped = nil)
        @source = source
        @struct = Struct.new(*source.columns.to_a.map!(&:to_sym)) unless source.columns.empty?
        @unread = true
        @query = query
        @unwrapped = unwrapped
      end

      def to_s
        @query
      end

      def unwrapped?
        !!unwrapped
      end

      def inspect
        "Enumerable query: '#{@query}'"
      end

      # @return [Array<Symbol>] the columns in the query result
      def columns
        @source.columns.map!(&:to_sym)
      end

      def error?
        false
      end

      def each
        fail ResultsAlreadyConsumedException unless @unread

        if block_given?
          @source.each do |row|
            yield(row.each_with_object(@struct.new) do |(column, value), result|
              result[column.to_sym] = unwrap(value)
            end)
          end
        else
          Enumerator.new(self)
        end
      end

      private

      def unwrap(value)
        if !value.nil? && value.respond_to?(:to_a)
          value.respond_to?(:to_hash) ? value.to_hash : value.map { |v| unwrap(v) }
        else
          (!value.respond_to?(:wrapper) || unwrapped?) ? value : value.wrapper
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
neo4j-core-7.0.7 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.6 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.5 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.4 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.3 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.2 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.1 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.rc.2 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.rc.1 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.8 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.7 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.6 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.5 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.4 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.3 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.2 lib/neo4j-embedded/cypher_response.rb
neo4j-core-7.0.0.alpha.1 lib/neo4j-embedded/cypher_response.rb