Class: Gloo::Core::Invoker
- Inherits:
-
Object
- Object
- Gloo::Core::Invoker
- Defined in:
- lib/gloo/core/invoker.rb
Constant Summary collapse
- NO_TARGET_ERR =
'Missing function reference!'.freeze
- NOT_FOUND_ERR =
'Object was not found: '.freeze
- NOT_FUNCTION_ERR =
'Not a function: '.freeze
- PARAM_COUNT_ERR =
'Wrong number of parameters for function: '.freeze
Class Method Summary collapse
-
.evaluate_arg_tokens(engine, tokens) ⇒ Object
Evaluate a list of raw tokens, each as its own single-token expression (a literal or object reference) - the standalone invoke verb's long-standing per-token semantics, now shared with the inline-call form too.
-
.invoke(engine, target, arg_tokens) ⇒ Object
Resolve, validate and invoke the function at the given target path, with the given raw (unevaluated) arg tokens.
-
.invoke_function(engine, func, args) ⇒ Object
Invoke the function and return its result.
-
.params_count_ok?(engine, func, args) ⇒ Boolean
Confirm the number of args given matches the number the function declares.
-
.resolve_function(engine, target) ⇒ Object
Resolve the function object at the target path.
Class Method Details
.evaluate_arg_tokens(engine, tokens) ⇒ Object
Evaluate a list of raw tokens, each as its own single-token expression (a literal or object reference) - the standalone invoke verb's long-standing per-token semantics, now shared with the inline-call form too.
70 71 72 73 74 |
# File 'lib/gloo/core/invoker.rb', line 70 def self.evaluate_arg_tokens( engine, tokens ) return ( tokens || [] ).map do |t| Gloo::Expr::Expression.new( engine, [ t ] ).evaluate end end |
.invoke(engine, target, arg_tokens) ⇒ Object
Resolve, validate and invoke the function at the given
target path, with the given raw (unevaluated) arg tokens.
Reports an error and returns nil for any failure - including
a failure inside the function itself (see Function#invoke,
which leaves it alone rather than setting it to an
unreliable result when that happens).
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gloo/core/invoker.rb', line 27 def self.invoke( engine, target, arg_tokens ) if target.nil? || target.to_s.strip.empty? engine.err NO_TARGET_ERR return nil end func = resolve_function( engine, target ) return nil unless func args = evaluate_arg_tokens( engine, arg_tokens ) return nil unless params_count_ok?( engine, func, args ) return invoke_function( engine, func, args ) end |
.invoke_function(engine, func, args) ⇒ Object
Invoke the function and return its result.
93 94 95 96 97 98 |
# File 'lib/gloo/core/invoker.rb', line 93 def self.invoke_function( engine, func, args ) engine.log.debug "invoking function: #{func.pn}" result = func.invoke( args ) engine.log.debug "function returned: #{result}" return result end |
.params_count_ok?(engine, func, args) ⇒ Boolean
Confirm the number of args given matches the number the function declares. Reports an error and returns false if they don't match.
81 82 83 84 85 86 87 88 |
# File 'lib/gloo/core/invoker.rb', line 81 def self.params_count_ok?( engine, func, args ) expected = func.params_hash&.keys&.length || 0 return true if args.count == expected engine.err "#{PARAM_COUNT_ERR}#{func.pn} " \ "(expected #{expected}, got #{args.count})" return false end |
.resolve_function(engine, target) ⇒ Object
Resolve the function object at the target path. Reports an error and returns nil if the path doesn't resolve to anything, or resolves to an object that isn't a function.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gloo/core/invoker.rb', line 47 def self.resolve_function( engine, target ) pn = Gloo::Core::Pn.new( engine, target ) func = pn.resolve unless func engine.err "#{NOT_FOUND_ERR}#{target}" return nil end unless func.is_function? engine.err "#{NOT_FUNCTION_ERR}#{target}" return nil end return func end |