Class: FunctionsFramework::Function::Callable
- Inherits:
-
Object
- Object
- FunctionsFramework::Function::Callable
- Defined in:
- lib/functions_framework/function.rb
Overview
A base class for a callable object that provides calling context.
An object of this class is self
while a function block is running.
Instance Method Summary collapse
-
#global(key) ⇒ Object
Get the given named global.
-
#initialize(globals: nil, logger: nil) ⇒ Callable
constructor
Create a callable object with the given context.
-
#logger ⇒ Logger
A logger for use by this call.
-
#set_global(key, value = nil, &block) ⇒ Object
Set a global.
Constructor Details
#initialize(globals: nil, logger: nil) ⇒ Callable
Create a callable object with the given context.
201 202 203 204 |
# File 'lib/functions_framework/function.rb', line 201 def initialize globals: nil, logger: nil @__globals = globals || {} @__logger = logger || FunctionsFramework.logger end |
Instance Method Details
#global(key) ⇒ Object
Get the given named global.
For most function calls, the following globals will be defined:
- :function_name (
String
) The name of the running function. - :function_type (
Symbol
) The type of the running function, either:http
or:cloud_event
.
You can also set additional globals from a startup task.
220 221 222 223 224 |
# File 'lib/functions_framework/function.rb', line 220 def global key value = @__globals[key] value = value.value if LazyGlobal === value value end |
#logger ⇒ Logger
A logger for use by this call.
267 268 269 |
# File 'lib/functions_framework/function.rb', line 267 def logger @__logger end |
#set_global(key, value) ⇒ self #set_global(key, &block) ⇒ self
Set a global. This can be called from startup tasks, but the globals are frozen when the server starts, so this call will raise an exception if called from a normal function.
You can set a global to a final value, or you can provide a block that lazily computes the global the first time it is requested.
257 258 259 260 |
# File 'lib/functions_framework/function.rb', line 257 def set_global key, value = nil, &block @__globals[key] = block ? LazyGlobal.new(block) : value self end |