Class: ActiveCypher::Fixtures::NodeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cypher/fixtures/node_builder.rb

Class Method Summary collapse

Class Method Details

.build(ref, model_class, props) ⇒ Object

Builds a node and registers it like a bouncer at a VIP graph party.

Parameters:

  • ref (Symbol, String)

    Logical ref name (e.g., :john, :spaceship_42)

  • model_class (Class)

    The model class (must know how to connect and label itself)

  • props (Hash)

    Properties to assign to the node

Returns:

  • (Object)

    Instantiated model object with DB-assigned internal_id



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
37
38
# File 'lib/active_cypher/fixtures/node_builder.rb', line 12

def self.build(ref, model_class, props)
  conn = model_class.connection
  labels = model_class.labels

  # Because even Cypher likes a well-dressed node.
  label_clause = labels.map { |label| "`#{label}`" }.join(':')

  # Build and fire the CREATE query.
  # Ask the adapter how it likes its IDs served - string or integer, sir?
  adapter = conn.id_handler
  cypher = "    CREATE (n:\#{label_clause} $props)\n    RETURN n, \#{adapter.return_node_id('n')}, properties(n) AS props\n  CYPHER\n\n  result = conn.execute_cypher(cypher, props: props)\n  record = result.first\n\n  # Extract properties returned by the DB\n  node_props = record[:props] || record['props'] || {}\n  node_props['internal_id'] = record[:internal_id] || record['internal_id']\n\n  # Instantiate and tag it like we own it\n  instance = model_class.instantiate(node_props)\n  Registry.add(ref, instance)\n  instance\nend\n"

.bulk_build(nodes, batch_size: 200) ⇒ Object

Bulk create nodes. Still uses single ‘CREATE` per node, just slices the list to avoid melting your graph engine.

Parameters:

  • nodes (Array<Hash>)

    List of { ref:, model_class:, props: }

  • batch_size (Integer) (defaults to: 200)

    How many to process per slice



45
46
47
48
49
50
51
# File 'lib/active_cypher/fixtures/node_builder.rb', line 45

def self.bulk_build(nodes, batch_size: 200)
  nodes.each_slice(batch_size) do |batch|
    batch.each do |node|
      build(node[:ref], node[:model_class], node[:props])
    end
  end
end