Class: Cyrel::Clause::Match

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

Overview

Represents a MATCH or OPTIONAL MATCH clause in a Cypher query.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, optional: false, path_variable: nil) ⇒ Match

Returns a new instance of Match.

Parameters:

  • pattern (Cyrel::Pattern::Path, Cyrel::Pattern::Node, Cyrel::Pattern::Relationship)

    The pattern to match. Typically a Path, but could be a single Node for simple matches.

  • optional (Boolean) (defaults to: false)

    Whether this is an OPTIONAL MATCH.

  • path_variable (Symbol, String, nil) (defaults to: nil)

    An optional variable to assign to the matched path.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cyrel/clause/match.rb', line 13

def initialize(pattern, optional: false, path_variable: nil)
  super() # Call super for Base initialization
  # Ensure pattern is a valid type
  unless pattern.is_a?(Cyrel::Pattern::Path) ||
         pattern.is_a?(Cyrel::Pattern::Node) ||
         pattern.is_a?(Cyrel::Pattern::Relationship)
    raise ArgumentError,
          "Match pattern must be a Cyrel::Pattern::Path, Node, or Relationship, got #{pattern.class}"
  end

  @pattern = pattern
  @optional = optional
  @path_variable = path_variable&.to_sym
end

Instance Attribute Details

#optionalObject (readonly)

Returns the value of attribute optional.



7
8
9
# File 'lib/cyrel/clause/match.rb', line 7

def optional
  @optional
end

#path_variableObject (readonly)

Returns the value of attribute path_variable.



7
8
9
# File 'lib/cyrel/clause/match.rb', line 7

def path_variable
  @path_variable
end

#patternObject (readonly)

Returns the value of attribute pattern.



7
8
9
# File 'lib/cyrel/clause/match.rb', line 7

def pattern
  @pattern
end

Instance Method Details

#render(query) ⇒ String

Renders the MATCH or OPTIONAL MATCH clause.

Parameters:

  • query (Cyrel::Query)

    The query object for rendering the pattern.

Returns:

  • (String)

    The Cypher string fragment for the clause.



31
32
33
34
35
36
37
# File 'lib/cyrel/clause/match.rb', line 31

def render(query)
  keyword = @optional ? 'OPTIONAL MATCH' : 'MATCH'
  path_assignment = @path_variable ? "#{@path_variable} = " : ''
  pattern_string = @pattern.render(query)

  "#{keyword} #{path_assignment}#{pattern_string}"
end