Class: Neo4j::Embedded::ResultWrapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/neo4j-embedded/cypher_response.rb

Overview

Note:

The result is a once forward read only Enumerable, work if you need to read the result twice - use #to_a

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.

Defined Under Namespace

Classes: ResultsAlreadyConsumedException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, query) ⇒ ResultWrapper

Returns a new instance of ResultWrapper.



17
18
19
20
21
22
# File 'lib/neo4j-embedded/cypher_response.rb', line 17

def initialize(source, query)
  @source = source
  @struct = Struct.new(*source.columns.to_a.map(&:to_sym))
  @unread = true
  @query = query
end

Instance Attribute Details

#sourceObject (readonly)

Returns the original result from the Neo4j Cypher Engine, once forward read only !.

Returns:

  • the original result from the Neo4j Cypher Engine, once forward read only !



15
16
17
# File 'lib/neo4j-embedded/cypher_response.rb', line 15

def source
  @source
end

Instance Method Details

#columnsArray<Symbol>

Returns the columns in the query result.

Returns:

  • (Array<Symbol>)

    the columns in the query result



33
34
35
# File 'lib/neo4j-embedded/cypher_response.rb', line 33

def columns
  @source.columns.map(&:to_sym)
end

#eachObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/neo4j-embedded/cypher_response.rb', line 37

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] = (value.respond_to?(:wrapper) ? value.wrapper : value)
      end)
    end
  else
    Enumerator.new(self)
  end
end

#inspectObject



28
29
30
# File 'lib/neo4j-embedded/cypher_response.rb', line 28

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

#to_sObject



24
25
26
# File 'lib/neo4j-embedded/cypher_response.rb', line 24

def to_s
  @query
end