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)


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

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.



9
10
11
# File 'lib/janeway/ast/function.rb', line 9

def body
  @body
end

#parametersObject (readonly)

Returns the value of attribute parameters.



9
10
11
# File 'lib/janeway/ast/function.rb', line 9

def parameters
  @parameters
end

Instance Method Details

#literal?Boolean

True if the function’s return value is a literal

Returns:



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

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

#logical?Boolean

True if the function’s return value is a boolean

Returns:



50
51
52
53
54
55
56
57
# File 'lib/janeway/ast/function.rb', line 50

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

#singular_query?Boolean

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



34
35
36
# File 'lib/janeway/ast/function.rb', line 34

def singular_query?
  true
end

#to_sObject



20
21
22
# File 'lib/janeway/ast/function.rb', line 20

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

#tree(level) ⇒ Array

Parameters:

  • level (Integer)

Returns:

  • (Array)


26
27
28
# File 'lib/janeway/ast/function.rb', line 26

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