Method: Janeway::Functions#parse_function_count

Defined in:
lib/janeway/functions/count.rb

#parse_function_countObject

The count() function extension provides a way to obtain the number of nodes in a nodelist and make that available for further processing in the filter expression:

Its only argument is a nodelist. The result is a value (an unsigned integer) that gives the number of nodes in the nodelist.

Notes:

* There is no deduplication of the nodelist.
* The number of nodes in the nodelist is counted independent of
  their values or any children they may have, e.g., the count of a
  non-empty singular nodelist such as count(@) is always 1.

Examples:

$[?count(@.*.author) >= 5]


Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/janeway/functions/count.rb', line 20

def parse_function_count
  consume # function
  raise "expect group_start token, found #{current}" unless current.type == :group_start

  consume # (

  # Read parameter
  arg = parse_function_parameter
  parameters = [arg]
  raise Error, "Invalid parameter - count() expects node list, got #{arg.value.inspect}" if arg.literal?
  raise Error, 'Too many parameters for count() function call' unless current.type == :group_end

  # Define function body
  AST::Function.new('count', parameters) do |node_list|
    if node_list.is_a?(Array)
      node_list.size
    else
      1 # the count of a non-empty singular nodelist such as count(@) is always 1.
    end
  end
end