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.



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

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

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



53
54
55
# File 'lib/cyrel.rb', line 53

def elements
  @elements
end

Instance Method Details

#-(_other) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cyrel.rb', line 129

def -(_other)
  # Same logic as > but for bidirectional
  if @elements.last.is_a?(Cyrel::Pattern::Relationship)
    last_rel = @elements.pop
    new_rel = Cyrel::Pattern::Relationship.new(
      alias_name: last_rel.alias_name,
      types: last_rel.types,
      properties: last_rel.properties,
      length: last_rel.length,
      direction: :both
    )
    @elements << new_rel
  else
    @pending_direction = :both
  end
  self
end

#<(_other) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cyrel.rb', line 111

def <(_other)
  # Same logic as > but for incoming direction
  if @elements.last.is_a?(Cyrel::Pattern::Relationship)
    last_rel = @elements.pop
    new_rel = Cyrel::Pattern::Relationship.new(
      alias_name: last_rel.alias_name,
      types: last_rel.types,
      properties: last_rel.properties,
      length: last_rel.length,
      direction: :incoming
    )
    @elements << new_rel
  else
    @pending_direction = :incoming
  end
  self
end

#>(_other) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cyrel.rb', line 90

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
  if @elements.last.is_a?(Cyrel::Pattern::Relationship)
    # Replace the last relationship with one that has the correct direction
    last_rel = @elements.pop
    new_rel = Cyrel::Pattern::Relationship.new(
      alias_name: last_rel.alias_name,
      types: last_rel.types,
      properties: last_rel.properties,
      length: last_rel.length,
      direction: :outgoing
    )
    @elements << new_rel
  else
    @pending_direction = :outgoing
  end
  self
end

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



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

def node(alias_name = nil, *labels, **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, properties: properties)
  @elements << n
  self
end

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



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

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