Class: Gloo::Objs::Function

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/ctrl/function.rb

Constant Summary collapse

KEYWORD =
'function'.freeze
KEYWORD_SHORT =
'ƒ'.freeze
ON_INVOKE =

Events

'on_invoke'.freeze
AFTER_INVOKE =
'after_invoke'.freeze
PARAMS =

Parameters to the function invocation.

'params'.freeze
RESULT =

Return Value or container of objects

'result'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_responds_to?, #msg_unload, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

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

Class Method Details

.doc_dataObject

Get the object's documentation data.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/gloo/objs/ctrl/function.rb', line 204

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'Define a function that will be performed ' \
      'somewhere else. When the function is invoked, the ' \
      'parameter values are set, the on_invoke script is run, the ' \
      'result is determined, the after_invoke script is run, and ' \
      'the result is returned.',
    :children => [
      'on_invoke (script) — Script to run when the function is invoked.',
      'after_invoke (optional script) — Script to run after the function has been invoked and before the value is returned. Can be used to clean up and reset the object state.',
      'params (container) — The collection of parameters to the function. Parameter values are used as defaults if no value is passed in during invocation.',
      'result (string) — The result of the function to return to the caller.'
    ],
    :messages => [
      'invoke — Invoke the function, set it and return the result. The function result is put into it as well as being returned to the caller.'
    ],
    :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

          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

.messagesObject

Get a list of message names that this object receives.



122
123
124
# File 'lib/gloo/objs/ctrl/function.rb', line 122

def self.messages
  return super + [ 'invoke' ]
end

.short_typenameObject

The short name of the object type.



36
37
38
# File 'lib/gloo/objs/ctrl/function.rb', line 36

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



29
30
31
# File 'lib/gloo/objs/ctrl/function.rb', line 29

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



97
98
99
# File 'lib/gloo/objs/ctrl/function.rb', line 97

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



106
107
108
109
110
111
112
# File 'lib/gloo/objs/ctrl/function.rb', line 106

def add_default_children
  fac = @engine.factory

  fac.create_can PARAMS, self
  fac.create_script ON_INVOKE, '', self
  fac.create_untyped RESULT, '', self
end

#invoke(args) ⇒ Object

Invoke the function, run the script and return the result.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/gloo/objs/ctrl/function.rb', line 167

def invoke args
  @engine.log.debug "Invoking function: #{name}"

  set_params args if args
  run_on_invoke

  if @engine.running_app&.obj&.embedded_renderer 
    return_value = @engine.running_app.obj.embedded_renderer.render result, params_hash
  else
    return_value = result
  end
  @engine.heap.it.set_to return_value
  run_after_invoke
  
  return return_value
end

#msg_invokeObject

Send the object the invoke message. Invoke the function and return the result.



130
131
132
# File 'lib/gloo/objs/ctrl/function.rb', line 130

def msg_invoke
  return invoke( nil )
end

#multiline_value?Boolean

Does this object support multi-line values? Initially only true for scripts.

Returns:



58
59
60
# File 'lib/gloo/objs/ctrl/function.rb', line 58

def multiline_value?
  return false
end

#params_hashObject

Get the params hash from the child object. Returns nil if there is none.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gloo/objs/ctrl/function.rb', line 75

def params_hash
  params_can = find_child PARAMS
  return nil unless params_can

  h = {}
  params_can.children.each do |o|
    h[ o.name ] = o.value
  end

  return h
end

#resultObject

Get the result, the return value or container of objects.



65
66
67
68
69
# File 'lib/gloo/objs/ctrl/function.rb', line 65

def result
  # TODO: what does it look like to return objects?
  # if return_any is a container with children, return the container
  return find_child_value RESULT
end

#run_after_invokeObject

Run the after invoke script if there is one.



152
153
154
155
156
157
# File 'lib/gloo/objs/ctrl/function.rb', line 152

def run_after_invoke
  o = find_child AFTER_INVOKE
  return unless o

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#run_on_invokeObject

Run the on invoke script if there is one.



142
143
144
145
146
147
# File 'lib/gloo/objs/ctrl/function.rb', line 142

def run_on_invoke
  o = find_child ON_INVOKE
  return unless o

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#set_array_value(arr) ⇒ Object

Set the value as an array.



50
51
52
# File 'lib/gloo/objs/ctrl/function.rb', line 50

def set_array_value( arr )
  self.value = arr
end

#set_params(args) ⇒ Object

Set parameters from the arguments given.



187
188
189
190
191
192
193
194
195
# File 'lib/gloo/objs/ctrl/function.rb', line 187

def set_params args
  params = find_child PARAMS
  return unless params

  args.each_with_index do |arg, i|
    param = params.children[i]
    param.value = arg if param
  end
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



43
44
45
# File 'lib/gloo/objs/ctrl/function.rb', line 43

def set_value( new_value )
  self.value = new_value.to_s
end