Class: Cyrel::PathBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cyrel.rb

Overview

Path builder DSL for constructing path patterns

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePathBuilder

Returns a new instance of PathBuilder.



57
58
59
60
# File 'lib/cyrel.rb', line 57

def initialize
  @elements = []
  @pending_direction = nil
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



55
56
57
# File 'lib/cyrel.rb', line 55

def elements
  @elements
end

Instance Method Details

#-(_other) ⇒ Object



104
105
106
107
# File 'lib/cyrel.rb', line 104

def -(_other)
  # Same logic as > but for bidirectional
  apply_direction(:both)
end

#<(_other) ⇒ Object



99
100
101
102
# File 'lib/cyrel.rb', line 99

def <(_other)
  # Same logic as > but for incoming direction
  apply_direction(:incoming)
end

#>(_other) ⇒ Object



92
93
94
95
96
97
# File 'lib/cyrel.rb', line 92

def >(_other)
  # When called like: node(:a) > rel(:r) > node(:b)
  # The rel(:r) is evaluated first, then > is called
  # So we need to modify the last relationship that was just added
  apply_direction(:outgoing)
end

#node(alias_name = nil, *labels, or_labels: nil, **properties) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cyrel.rb', line 62

def node(alias_name = nil, *labels, or_labels: nil, **properties)
  # If there's a pending direction, we need to add a relationship first
  if @pending_direction && @elements.any? && @elements.last.is_a?(Cyrel::Pattern::Node)
    @elements << Cyrel::Pattern::Relationship.new(types: [], direction: @pending_direction)
    @pending_direction = nil
  end

  n = Cyrel::Pattern::Node.new(alias_name, labels: labels, or_labels: or_labels, properties: properties)
  @elements << n
  self
end

#rel(alias_name = nil, *types, **properties) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cyrel.rb', line 74

def rel(alias_name = nil, *types, **properties)
  length = properties.delete(:length)

  # Check if we need to replace the last element (an anonymous relationship)
  if @elements.last.is_a?(Cyrel::Pattern::Relationship) && @elements.last.types.empty?
    # Replace the anonymous relationship with specified one, keeping direction
    direction = @elements.last.direction
    @elements.pop
  else
    direction = @pending_direction || :both
  end

  r = Cyrel::Pattern::Relationship.new(alias_name: alias_name, types: types, properties: properties, length: length, direction: direction)
  @elements << r
  @pending_direction = nil
  self
end