Class: Cyrel::Expression::Literal

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

Overview

Represents a literal value (String, Number, Boolean, Nil, Array, Map) in a Cypher query. Literals are typically converted into parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#!=, #%, #&, #*, #+, #-, #/, #<, #<=, #==, #=~, #>, #>=, #^, #|

Constructor Details

#initialize(value) ⇒ Literal

Returns a new instance of Literal.



10
11
12
13
14
15
# File 'lib/cyrel/expression/literal.rb', line 10

def initialize(value)
  # We don't validate the type here extensively, assuming Neo4j driver
  # or the database itself will handle type compatibility.
  # We could add checks for common unsupported types if needed.
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/cyrel/expression/literal.rb', line 8

def value
  @value
end

Instance Method Details

#render(query) ⇒ String

Renders the literal by registering it as a parameter.

Parameters:

  • query (Cyrel::Query)

    The query object for parameter registration.

Returns:

  • (String)

    The parameter placeholder string (e.g., “$p1”).



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cyrel/expression/literal.rb', line 20

def render(query)
  # nil is special - render as NULL literal, not a parameter
  return 'NULL' if @value.nil?

  param_key = query.register_parameter(@value)

  # If the param_key is the same as the value (for loop variables),
  # don't add the $ prefix - just render as identifier
  if param_key == @value && @value.is_a?(Symbol)
    param_key.to_s
  else
    "$#{param_key}"
  end
end