Class: Aspen::Renderers::CypherBatchRenderer

Inherits:
AbstractRenderer show all
Defined in:
lib/aspen/renderers/cypher_batch_renderer.rb

Instance Attribute Summary

Attributes inherited from AbstractRenderer

#environment, #statements

Instance Method Summary collapse

Methods inherited from AbstractRenderer

#initialize, #nodes, #relationships

Constructor Details

This class inherits a constructor from AbstractRenderer

Instance Method Details

#renderObject

Sorts statements by signature, where signature could be

(Person)-[KNOWS]-(Person) or (Company)-[HIRED]->(Person).


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aspen/renderers/cypher_batch_renderer.rb', line 7

def render
  groups = statements.inject(
    Hash.new { |h, k| h[k] = [] }
    ) do |memo, statement|
      memo[statement.signature] << statement
      memo
  end
  groups.inject([]) do |memo, elem|
    signature, statements = elem
    if signature == "custom"
      # Delegate custom statements to the base renderer
      memo << CypherBaseRenderer.new(statements, environment).render
    else
      first = statements.first
      values = statements.map do |statement|
        "{from: #{statement.origin.attributes.values.first.inspect}, to: #{statement.target.attributes.values.first.inspect}}"
      end.join(",\n  ")
      memo << ERB.new(template).result_with_hash(
        signature:    signature,
        values:       values,
        origin_label: first.origin.label,
        target_label: first.target.label,
        origin_attr:  first.origin.attributes.keys.first,
        target_attr:  first.target.attributes.keys.first,
        edge_cypher:  first.edge.to_cypher,
      )
    end
    memo
  end.join("\n\n")
end

#templateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aspen/renderers/cypher_batch_renderer.rb', line 38

def template
  <<~TEMPLATE
    // <%= signature %>

    WITH [
      <%= values %>
    ] as values

    UNWIND values as row
    MERGE (from:<%= origin_label %> {<%= origin_attr %>: row.from})
    MERGE (to:<%= target_label %> {<%= target_attr %>: row.to})
    MERGE (from)<%= edge_cypher %>(to)
  TEMPLATE
end