Class: Workhorse::ScopedEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/workhorse/scoped_env.rb

Overview

Scoped environment for method delegation. Used internally to provide scoped access to daemon configuration methods.

Instance Method Summary collapse

Constructor Details

#initialize(delegation_object, methods, backup_binding = nil) ⇒ ScopedEnv

Creates a new scoped environment.

Parameters:

  • delegation_object (Object)

    Object to delegate method calls to

  • methods (Array<Symbol>)

    Methods that should be delegated

  • backup_binding (Object, nil) (defaults to: nil)

    Fallback object for method resolution



12
13
14
15
16
# File 'lib/workhorse/scoped_env.rb', line 12

def initialize(delegation_object, methods, backup_binding = nil)
  @delegation_object = delegation_object
  @methods = methods
  @backup_binding = backup_binding
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Handles method delegation to the configured objects.

Parameters:

  • symbol (Symbol)

    Method name

  • args (Array)

    Method arguments

  • block (Proc, nil)

    Block to pass to the method

Returns:

  • (Object)

    Result of the delegated method call



24
25
26
27
28
29
30
31
32
# File 'lib/workhorse/scoped_env.rb', line 24

def method_missing(symbol, *args, &block)
  if @methods.include?(symbol)
    @delegation_object.send(symbol, *args, &block)
  elsif @backup_binding.try(:respond_to?, symbol)
    @backup_binding.send(symbol, *args, &block)
  else
    super
  end
end

Instance Method Details

#respond_to_missing?(symbol, include_private = false) ⇒ Boolean

Checks if this object can respond to the given method.

Parameters:

  • symbol (Symbol)

    Method name to check

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods

Returns:

  • (Boolean)

    True if method can be handled



39
40
41
# File 'lib/workhorse/scoped_env.rb', line 39

def respond_to_missing?(symbol, include_private = false)
  @methods.include?(symbol) || super
end