Class: ROM::Neo4j::Relation

Inherits:
Relation
  • Object
show all
Defined in:
lib/rom/neo4j/relation.rb

Overview

Relation supporting Cypher graph traversals. Configure the sub-graph for the relation by specifying a ‘match` pattern, and collect a dataset for mapping by specifying nodes, edges and properties in the `returns`.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds data structures to configure the relation DSL.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rom/neo4j/relation.rb', line 35

def self.inherited(klass)
  klass.class_eval do
    class << self
      attr_reader :traversal
    end
  end

  klass.instance_variable_set('@traversal', {
    start: false,
    match: false,
    return: false
  })

  super
end

.matches(*conditions) ⇒ Object

Specify a ‘MATCH` clause for the relation’s graph traversal. If you’re coming from the SQL world, you can think of this as similar to a ‘SELECT FROM`, except that it matches on a topological structure rather than a schema.

Examples:

Reproduce SQL style projections by passing node labels directly.


class Movies < ROM::Relation[:neo4j]
  matches m: :movie
end

Specify topological matches using Cypher’s ASCII-art syntax.


class Actors < ROM::Relation[:neo4j]
  matches '(actor:Person)-[:ACTED_IN]->(movie)'
end

See Also:



81
82
83
# File 'lib/rom/neo4j/relation.rb', line 81

def self.matches(*conditions)
  @traversal[:match] = conditions
end

.returns(*conditions) ⇒ Object

Specify a ‘RETURN` clause for the relation. This will define the structure of objects in the returned dataset.

Any combination of nodes, edges and properties can be selected, as well as custom aliases and distinct objects.



93
94
95
# File 'lib/rom/neo4j/relation.rb', line 93

def self.returns(*conditions)
  @traversal[:return] = conditions
end

.start(*conditions) ⇒ Object

Specify a ‘START` node for the for the relation’s graph traversal. This is only required for legacy indexes. In most cases Cypher can infer the starting points to anchor a graph traversal from the pattern specified in a ‘MATCH` clause.



58
59
60
# File 'lib/rom/neo4j/relation.rb', line 58

def self.start(*conditions)
  @traversal[:start] = conditions
end

Instance Method Details

#each(&iter) ⇒ Object

The row iterator. Calling this kicks off the actual query to the database server.

Before triggering the enumerator, it rebuilds the dataset with the default traversal configured by the relation DSL.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rom/neo4j/relation.rb', line 21

def each(&iter)
  # Configure the default traversal
  unless @configured
    @configured = true
    self.class.traversal.each do |query_method, conditions|
      @dataset = @dataset.send(query_method.to_sym, *conditions) if conditions
    end
  end

  @dataset.each(&iter)
end