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, unwrapped = nil) ⇒ ResultWrapper

Returns a new instance of ResultWrapper.



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

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

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

#unwrappedObject (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 unwrapped
  @unwrapped
end

Instance Method Details

#columnsArray<Symbol>

Returns the columns in the query result.

Returns:

  • (Array<Symbol>)

    the columns in the query result



38
39
40
# File 'lib/neo4j-embedded/cypher_response.rb', line 38

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

#eachObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/neo4j-embedded/cypher_response.rb', line 42

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

#inspectObject



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

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

#to_sObject



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

def to_s
  @query
end

#unwrapped?Boolean

Returns:

  • (Boolean)


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

def unwrapped?
  !!unwrapped
end