12
13
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
|
# File 'lib/arango/database/aql_functions.rb', line 12
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
args = `original_arguments`
RUBY
compiled_ruby= Opal.compile( + source_code, parse_comments: false)
if compiled_ruby.start_with?('/*')
start_of_code = compiled_ruby.index('*/') + 3
compiled_ruby = compiled_ruby[start_of_code..-1]
end
code = <<~JAVASCRIPT
function() {
"use strict";
require('opal');
var original_arguments = Array.prototype.slice.call(arguments);
for (var i=0; i<original_arguments.length; i++) {
if (typeof original_arguments[i] === "object" && !(original_arguments[i] instanceof Array)) {
original_arguments[i] = Opal.Hash.$new(original_arguments[i]);
}
}
var result = #{compiled_ruby}
if (typeof result['$to_n'] === "function") { result = result['$to_n'](); }
return result;
}
JAVASCRIPT
end
body = { name: name, code: code, isDeterministic: is_deterministic }
Arango::Requests::AQL::CreateFunction.execute(server: @server, body: body)
true
end
|