Module: Arango::Database::AQLFunctions

Included in:
Arango::Database
Defined in:
lib/arango/database/aql_functions.rb

Overview

Arango Database AQLFunctions

Instance Method Summary collapse

Instance Method Details

#create_aql_function(name, code: nil, is_deterministic: nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arango/database/aql_functions.rb', line 14

def create_aql_function(name, code: nil, is_deterministic: nil, &block)
  if block_given?
    source_block = Parser::CurrentRuby.parse(block.source).children.last
    source_block = source_block.children.last if source_block.type == :block
    source_code = Unparser.unparse(source_block)
    ruby_header = "    args = `original_arguments`\n    RUBY\n    compiled_ruby= Opal.compile(ruby_header + source_code, parse_comments: false)\n    if compiled_ruby.start_with?('/*')\n      start_of_code = compiled_ruby.index('*/') + 3\n      compiled_ruby = compiled_ruby[start_of_code..-1]\n    end\n    code = <<~JAVASCRIPT\n    function() {\n      \"use strict\";\n      require('opal');\n      var original_arguments = Array.prototype.slice.call(arguments);\n      for (var i=0; i<original_arguments.length; i++) {\n        if (typeof original_arguments[i] === \"object\" && !(original_arguments[i] instanceof Array)) {\n          original_arguments[i] = Opal.Hash.$new(original_arguments[i]);\n        }\n      }\n      var result = \#{compiled_ruby}\n      if (typeof result['$to_n'] === \"function\") { result = result['$to_n'](); }\n      return result;\n    }\n    JAVASCRIPT\n  end\n  body = { name: name, code: code, isDeterministic: is_deterministic }\n  Arango::Requests::AQL::CreateFunction.execute(server: @server, body: body)\n  true\nend\n"

#drop_aql_function(name, namespace: nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/arango/database/aql_functions.rb', line 48

def drop_aql_function(name, namespace: nil)
  params = nil
  params = { namespace: namespace } unless group.nil?
  args = { name: name }
  Arango::Requests::AQL::CreateFunction.execute(server: @server, args: args, params: params)
  true
end

#list_aql_functions(namespace: nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/arango/database/aql_functions.rb', line 7

def list_aql_functions(namespace: nil)
  params = nil
  params = { namespace: namespace } unless namespace.nil?
  result = Arango::Requests::AQL::ListFunctions(server: @server, params: params)
  result.map { |r| Arango::Result.new(r) }
end