Class: FunctionsFramework::Function::Callable

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(globals: nil, logger: nil) ⇒ Callable

Create a callable object with the given context.

Parameters:

  • globals (Hash) (defaults to: nil)

    A set of globals available to the call.

  • logger (Logger) (defaults to: nil)

    A logger for use by the function call.



224
225
226
227
# File 'lib/functions_framework/function.rb', line 224

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.

Parameters:

  • key (Symbol, String)

    The name of the global to get.

Returns:

  • (Object)


243
244
245
246
247
# File 'lib/functions_framework/function.rb', line 243

def global key
  value = @__globals[key]
  value = value.value if LazyGlobal === value
  value
end

#loggerLogger

A logger for use by this call.

Returns:

  • (Logger)


290
291
292
# File 'lib/functions_framework/function.rb', line 290

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.

Overloads:

  • #set_global(key, value) ⇒ self

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

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

    Parameters:

    • key (Symbol, String)
    • value (Object)

    Returns:

    • (self)
  • #set_global(key, &block) ⇒ self

    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
    

    Parameters:

    • key (Symbol, String)
    • block (Proc)

      A block that lazily computes a value

    Yield Returns:

    • (Object)

      The value

    Returns:

    • (self)


280
281
282
283
# File 'lib/functions_framework/function.rb', line 280

def set_global key, value = nil, &block
  @__globals[key] = block ? LazyGlobal.new(block) : value
  self
end