Class: ConvenientService::Utils::Proc::ExecConfig

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/utils/proc/exec_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(proc, object) ⇒ void

Parameters:

  • proc (Proc)
  • object (Object)

    Can be any type.



24
25
26
27
# File 'lib/convenient_service/utils/proc/exec_config.rb', line 24

def initialize(proc, object)
  @proc = proc
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



11
12
13
# File 'lib/convenient_service/utils/proc/exec_config.rb', line 11

def object
  @object
end

#procObject (readonly)

Returns the value of attribute proc.



17
18
19
# File 'lib/convenient_service/utils/proc/exec_config.rb', line 17

def proc
  @proc
end

Instance Method Details

#callObject

Note:

Second form allows to access methods from the enclosing context (like ‘test` in examples).

Returns Can be any type.

Examples:

Preparation.

class Foo
  class << self
    def stack
      @stack ||= SomeMiddlewareStack.new
    end

    def configure(&block)
      ConvenientService::Utils::Proc.exec_config(block, stack)
    end

    def test
    end
  end
end

First form - no arguments. Block is executed inside the object context (‘stack` in this particular case, see preparation example).

class Foo
  configure do
    use SomeMiddleware

    test # Raises Exception since `test` is NOT defined in `stack`.
  end
end

Second form - one argument. Block is executed in the enclosing context.

class Foo
  configure do |stack|
    stack.use SomeMiddleware

    test # Works.
  end
end

Returns:

  • (Object)

    Can be any type.



71
72
73
# File 'lib/convenient_service/utils/proc/exec_config.rb', line 71

def call
  proc_has_one_positional_argument? ? proc.call(object) : object.instance_exec(&proc)
end