Module: Remi::Dsl Private

Defined in:
lib/remi/dsl.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

A namespace for functions relating to the execution of a block against a proxy object.

Much of this code was borrowed from Docile and was modified to support different fallback contexts.

See Also:

  • [Docile](https://github.com/ms-ati/docile)

Class Method Summary collapse

Class Method Details

.dsl_eval(dsl, fallback_dsl, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Use with an imperative DSL (commands modify the context object)

Execute a block in the context of an object whose methods represent the commands in a DSL.

Use this method to execute an imperative DSL, which means that:

  1. Each command mutates the state of the DSL context object
  2. The return value of each command is ignored
  3. The final return value is the original context object

Parameters:

  • dsl (Object)

    context object whose methods make up the DSL

  • fallback_dsl (Object)

    context object that the DSL should fallback to

  • args (Array)

    arguments to be passed to the block

  • block (Proc)

    the block of DSL commands to be executed against the dsl context object

Returns:

  • (Object)

    the dsl context object after executing the block



63
64
65
66
# File 'lib/remi/dsl.rb', line 63

def dsl_eval(dsl, fallback_dsl, *args, &block)
  exec_in_proxy_context(dsl, fallback_dsl, Docile::FallbackContextProxy, *args, &block)
  dsl
end

.dsl_return(dsl, fallback_dsl, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
# File 'lib/remi/dsl.rb', line 69

def dsl_return(dsl, fallback_dsl, *args, &block)
  exec_in_proxy_context(dsl, fallback_dsl, Docile::FallbackContextProxy, *args, &block)
end

.exec_in_proxy_context(dsl, fallback_dsl, proxy_type, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute a block in the context of an object whose methods represent the commands in a DSL, using a specific proxy class.

Parameters:

  • dsl (Object)

    context object whose methods make up the (initial) DSL

  • fallback_dsl (Object)

    context object that the DSL should fall back to if the primary context fails to resolve

  • proxy_type (FallbackContextProxy, ChainingFallbackContextProxy)

    which class to instantiate as proxy context

  • args (Array)

    arguments to be passed to the block

  • block (Proc)

    the block of DSL commands to be executed

Returns:

  • (Object)

    the return value of the block



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/remi/dsl.rb', line 26

def exec_in_proxy_context(dsl, fallback_dsl, proxy_type, *args, &block)
  block_context = fallback_dsl
  proxy_context = proxy_type.new(dsl, block_context)
  begin
    block_context.instance_variables.each do |ivar|
      value_from_block = block_context.instance_variable_get(ivar)
      proxy_context.instance_variable_set(ivar, value_from_block)
    end
    proxy_context.instance_exec(*args, &block)
  ensure
    block_context.instance_variables.each do |ivar|
      value_from_dsl_proxy = proxy_context.instance_variable_get(ivar)
      block_context.instance_variable_set(ivar, value_from_dsl_proxy)
    end
  end
end