Class: Janeway::AST::Function

Inherits:
Expression show all
Defined in:
lib/janeway/ast/function.rb

Overview

Represents a JSONPath built-in function.

Instance Attribute Summary collapse

Attributes inherited from Expression

#next, #value

Instance Method Summary collapse

Methods inherited from Expression

#indented, #type

Constructor Details

#initialize(name, parameters, &body) ⇒ Function

Returns a new instance of Function.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/janeway/ast/function.rb', line 13

def initialize(name, parameters, &body)
  raise ArgumentError, "expect string, got #{name.inspect}" unless name.is_a?(String)

  super(name)
  @parameters = parameters
  @body = body
  raise "expect body to be a Proc, got #{body.class}" unless body.is_a?(Proc)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



11
12
13
# File 'lib/janeway/ast/function.rb', line 11

def body
  @body
end

#parametersObject (readonly)

Returns the value of attribute parameters.



11
12
13
# File 'lib/janeway/ast/function.rb', line 11

def parameters
  @parameters
end

Instance Method Details

#literal?Boolean

True if the function’s return value is a literal

Returns:



41
42
43
44
45
46
47
48
# File 'lib/janeway/ast/function.rb', line 41

def literal?
  case name
  when 'length', 'count', 'value' then true
  when 'search', 'match' then false
  else
    raise "Unknown jsonpath function #{name}"
  end
end

#singular_query?Boolean

True if this is the root of a singular-query.



36
37
38
# File 'lib/janeway/ast/function.rb', line 36

def singular_query?
  true
end

#to_sObject



22
23
24
# File 'lib/janeway/ast/function.rb', line 22

def to_s
  "#{name}(#{@parameters.join(',')})"
end

#tree(level) ⇒ Array

Parameters:

  • level (Integer)

Returns:

  • (Array)


28
29
30
# File 'lib/janeway/ast/function.rb', line 28

def tree(level)
  [indented(level, to_s)]
end