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.



29
30
31
32
# File 'lib/convenient_service/utils/proc/exec_config.rb', line 29

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

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#procObject (readonly)

Returns the value of attribute proc.



22
23
24
# File 'lib/convenient_service/utils/proc/exec_config.rb', line 22

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.



76
77
78
# File 'lib/convenient_service/utils/proc/exec_config.rb', line 76

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