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)
= " 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"
|