Class: ApacheAge::Path

Inherits:
Object
  • Object
show all
Includes:
Entities::Path
Defined in:
lib/apache_age/path.rb

Defined Under Namespace

Classes: PathResult

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Entities::Path

#age_type, #all, #ensure_query_builder!, #execute, #first, #limit, #match, #match_clause, #order, #return, #to_sql, #where

Instance Attribute Details

#end_node_filterObject (readonly)

Returns the value of attribute end_node_filter.



86
87
88
# File 'lib/apache_age/path.rb', line 86

def end_node_filter
  @end_node_filter
end

#path_edgeObject (readonly)

Returns the value of attribute path_edge.



86
87
88
# File 'lib/apache_age/path.rb', line 86

def path_edge
  @path_edge
end

#path_lengthObject (readonly)

Returns the value of attribute path_length.



86
87
88
# File 'lib/apache_age/path.rb', line 86

def path_length
  @path_length
end

#path_propertiesObject (readonly)

Returns the value of attribute path_properties.



86
87
88
# File 'lib/apache_age/path.rb', line 86

def path_properties
  @path_properties
end

#query_builderObject (readonly)

Returns the value of attribute query_builder.



86
87
88
# File 'lib/apache_age/path.rb', line 86

def query_builder
  @query_builder
end

#start_node_filterObject (readonly)

Returns the value of attribute start_node_filter.



86
87
88
# File 'lib/apache_age/path.rb', line 86

def start_node_filter
  @start_node_filter
end

Class Method Details

.age_typeObject



89
# File 'lib/apache_age/path.rb', line 89

def age_type = 'path'

.allObject



205
206
207
208
# File 'lib/apache_age/path.rb', line 205

def all
  ensure_query_builder!
  @query_builder.all
end

.cypher(path_edge: nil, path_length: nil, path_properties: {}, start_node_filter: nil, end_node_filter: nil, graph_name: nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/apache_age/path.rb', line 119

def cypher(path_edge: nil, path_length: nil, path_properties: {}, start_node_filter: nil, end_node_filter: nil, graph_name: nil)
  unless path_edge.nil? || path_edge.ancestors.include?(ApacheAge::Entities::Edge)
    raise ArgumentError, 'Path edge must be a valid edge class'
  end

  @path_edge = path_edge ? path_edge.name.gsub('::', '__') : nil
  @path_length = path_length ? "*#{path_length}" : "*"
  @path_properties = path_properties.blank? ? nil : " {#{path_properties.map { |k, v| "#{k}: '#{v}'" }.join(', ')}}"

  # Format node filters for the match clause if provided
  start_filter = format_node_filter(start_node_filter)
  end_filter = format_node_filter(end_node_filter)

  @has_edge_ordering = false
  @match_clause = "path = (start_node#{start_filter})-[#{@path_edge}#{@path_length}#{@path_properties}]->(end_node#{end_filter})"
  @query_builder =
    ApacheAge::Entities::QueryBuilder.new(
      self,
      graph_name:,
      return_clause: 'path',
      match_clause: @match_clause
    )
  self
end

.ensure_query_builder!Object



115
116
117
# File 'lib/apache_age/path.rb', line 115

def ensure_query_builder!
  @query_builder ||= ApacheAge::Entities::QueryBuilder.new(self)
end

.executeObject

Executes the query and returns results



195
196
197
198
# File 'lib/apache_age/path.rb', line 195

def execute
  ensure_query_builder!
  @query_builder.execute
end

.firstObject



200
201
202
203
# File 'lib/apache_age/path.rb', line 200

def first
  ensure_query_builder!
  @query_builder.first
end

.limit(limit_value) ⇒ Object



182
183
184
185
186
# File 'lib/apache_age/path.rb', line 182

def limit(limit_value)
  ensure_query_builder!
  @query_builder.limit(limit_value)
  self
end

.match(match_string) ⇒ Object



153
154
155
156
157
# File 'lib/apache_age/path.rb', line 153

def match(match_string)
  ensure_query_builder!
  @query_builder.match(match_string)
  self
end

.match_clauseObject



144
145
146
147
148
149
150
151
# File 'lib/apache_age/path.rb', line 144

def match_clause
  # Use the edge variable if we're ordering by edge properties
  if @has_edge_ordering
    "path = (start_node)-[edge:#{@path_edge}#{@path_length}#{@path_properties}]->(end_node)"
  else
    "path = (start_node)-[#{@path_edge}#{@path_length}#{@path_properties}]->(end_node)"
  end
end

.order(ordering) ⇒ Object

order is important to put last (but before limit) Path.cypher(…).where(…).order(“start_node.first_name ASC”).limit(5)



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/apache_age/path.rb', line 168

def order(ordering)
  ensure_query_builder!

  # Check if we're ordering by edge properties
  if ordering.is_a?(Hash) && ordering.key?(:edge)
    # Update match clause to include edge variable
    @match_clause = "path = (start_node)-[edge:#{@path_edge}#{@path_length}#{@path_properties}]->(end_node)"
    @query_builder.match_clause = @match_clause
  end

  @query_builder.order(ordering)
  self
end

.path_to_h(path_result) ⇒ Object

Convert a path result or collection of path results to hashes This handles both a single path (array of nodes/edges) and multiple paths



93
94
95
96
97
98
99
100
101
# File 'lib/apache_age/path.rb', line 93

def path_to_h(path_result)
  if path_result.first.is_a?(Array)
    # It's a collection of paths
    path_result.map { |path| path.map(&:to_h) }
  else
    # It's a single path
    path_result.map(&:to_h)
  end
end

.path_to_rich_h(path_result) ⇒ Object

Convert a path result to rich hashes with additional context information This handles both a single path (array of nodes/edges) and multiple paths



105
106
107
108
109
110
111
112
113
# File 'lib/apache_age/path.rb', line 105

def path_to_rich_h(path_result)
  if path_result.first.is_a?(Array)
    # It's a collection of paths
    path_result.map { |path| path.map(&:to_rich_h) }
  else
    # It's a single path
    path_result.map(&:to_rich_h)
  end
end

.return(*variables) ⇒ Object



188
189
190
191
192
# File 'lib/apache_age/path.rb', line 188

def return(*variables)
  ensure_query_builder!
  @query_builder.return(*variables)
  self
end

.to_sqlObject

Build the final SQL query



211
212
213
214
# File 'lib/apache_age/path.rb', line 211

def to_sql
  ensure_query_builder!
  @query_builder.to_sql
end

.where(*args) ⇒ Object

Delegate additional methods like ‘where`, `limit`, etc., to `QueryBuilder`



160
161
162
163
164
# File 'lib/apache_age/path.rb', line 160

def where(*args)
  ensure_query_builder!
  @query_builder.where(*args)
  self
end