Class: Neo4j::Cypher::ResultWrapper

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

Overview

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.

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ ResultWrapper

Returns a new instance of ResultWrapper.



21
22
23
# File 'lib/neo4j-cypher/result_wrapper.rb', line 21

def initialize(source)
  @source = source
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 !



19
20
21
# File 'lib/neo4j-cypher/result_wrapper.rb', line 19

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



26
27
28
# File 'lib/neo4j-cypher/result_wrapper.rb', line 26

def columns
  @source.columns.map { |x| x.to_sym }
end

#eachObject

for the Enumerable contract



31
32
33
# File 'lib/neo4j-cypher/result_wrapper.rb', line 31

def each
  @source.each { |row| yield map(row) }
end

#map(row) ⇒ Object

Maps each row so that we can use symbols for column names.



37
38
39
40
41
42
43
# File 'lib/neo4j-cypher/result_wrapper.rb', line 37

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