class FunctionsFramework::Function::Callable

A base class for a callable object that provides calling context.

An object of this class is `self` while a function block is running.

Public Class Methods

new(globals: nil, logger: nil) click to toggle source

Create a callable object with the given context.

@param globals [Hash] A set of globals available to the call. @param logger [Logger] A logger for use by the function call.

# File lib/functions_framework/function.rb, line 201
def initialize globals: nil, logger: nil
  @__globals = globals || {}
  @__logger = logger || FunctionsFramework.logger
end

Public Instance Methods

global(key) click to toggle source

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.

@param key [Symbol,String] The name of the global to get. @return [Object]

# File lib/functions_framework/function.rb, line 220
def global key
  value = @__globals[key]
  value = value.value if LazyGlobal === value
  value
end
logger() click to toggle source

A logger for use by this call.

@return [Logger]

# File lib/functions_framework/function.rb, line 267
def logger
  @__logger
end
set_global(key, value = nil, &block) click to toggle source

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.

@overload set_global(key, value)

Set the given global to the given value. For example:

    set_global(:project_id, "my-project-id")

@param key [Symbol,String]
@param value [Object]
@return [self]

@overload set_global(key, &block)

Call the given block to compute the global's value only when the
value is actually requested. This block will be called at most once,
and its result reused for subsequent calls. For example:

    set_global(:connection_pool) do
      ExpensiveConnectionPool.new
    end

@param key [Symbol,String]
@param block [Proc] A block that lazily computes a value
@yieldreturn [Object] The value
@return [self]
# File lib/functions_framework/function.rb, line 257
def set_global key, value = nil, &block
  @__globals[key] = block ? LazyGlobal.new(block) : value
  self
end