Class: Gloo::Verbs::Invoke

Inherits:
Core::Verb show all
Defined in:
lib/gloo/verbs/invoke.rb

Constant Summary collapse

KEYWORD =
'invoke'.freeze
KEYWORD_SHORT =
'~>'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Verb

#params, #tokens

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Verb

#display_value, help, inherited, #initialize, #type_display

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Verb

Class Method Details

.doc_dataObject

Get the verb's documentation data.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gloo/verbs/invoke.rb', line 49

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'Invoke a function. Set it to the result of the function.',
    :syntax => [
      'invoke {path.to.function} {params}',
      'invoke( {path.to.function} {params} )  — inline, usable inside any expression',
      '~>( {path.to.function} {params} )      — inline, shortcut spelling'
    ],
    :parameters => [
      '{path.to.function} — The function we want to invoke.',
      '{params} — The list of parameters to the function.'
    ],
    :result => 'The result of the function is put into it. The ' \
      'inline forms (invoke(...), ~>(...)) can be used anywhere ' \
      'an expression is evaluated - show, put ... into, if, ' \
      'unless, eval, and more - and evaluate to the result ' \
      'directly rather than going through it.',
    :errors => [
      "#{Gloo::Core::Invoker::NO_TARGET_ERR} — No function reference was given.",
      "#{Gloo::Core::Invoker::NOT_FOUND_ERR}{path.to.function} — The path doesn't resolve to any object.",
      "#{Gloo::Core::Invoker::NOT_FUNCTION_ERR}{path.to.function} — The path resolves to an object that isn't a function.",
      "#{Gloo::Core::Invoker::PARAM_COUNT_ERR}{path.to.function} (expected N, got N) — The number of params given doesn't match what the function declares."
    ],
    :notes => 'If the function itself fails during invocation ' \
      '(its on_invoke script hits an error), it is left ' \
      'unchanged rather than being set to an unreliable result. ' \
      'The underlying error is whatever was reported by the ' \
      'failure inside the function. ' \
      'Inline call params are space-separated, same as the ' \
      'standalone verb - each one is evaluated as a single ' \
      'token (a literal or object reference), not a multi-token ' \
      "sub-expression, so invoke( f 3+4 ) won't parse 3+4 as " \
      'one arg.',
    :examples => <<~EXAMPLES.strip
      #
      # Function examples
      #

      functions [can] :

        name [string] : Jasper

        on_load [script] :
          show 'running function examples' (white)

          invoke functions.greet ^.name
          show it

          invoke functions.add 3 4
          show it

          # Inline, anywhere an expression is evaluated:
          show invoke( functions.add 3 4 )
          put ~>( functions.add 3 4 ) into total
          show "Total: " + invoke( functions.add 3 4 )

          show 'done' (white)

        #
        # Say hi to person (name)
        #
        greet [ƒ] :
          on_invoke [script] :
            put 'Hi, ' + ^.params.name into ^.result
          params [container] :
            name [string] : none
          result [string] :

        #
        # Add 2 numbers
        #
        add [ƒ] :
          on_invoke [script] :
            put ^.params.x and ^.params.y into ^.result
          params [container] :
            x [int] :
            y [int] :
          result [string] :
    EXAMPLES
  }
end

.keywordObject

Get the Verb's keyword.



31
32
33
# File 'lib/gloo/verbs/invoke.rb', line 31

def self.keyword
  return KEYWORD
end

.keyword_shortcutObject

Get the Verb's keyword shortcut.



38
39
40
# File 'lib/gloo/verbs/invoke.rb', line 38

def self.keyword_shortcut
  return KEYWORD_SHORT
end

Instance Method Details

#runObject

Run the verb.



21
22
23
24
25
26
# File 'lib/gloo/verbs/invoke.rb', line 21

def run
  target = @tokens.token_count > 1 ? @tokens.second : nil
  arg_tokens = @tokens.token_count > 1 ? @tokens.params[ 1..-1 ] : []

  return Gloo::Core::Invoker.invoke( @engine, target, arg_tokens )
end