Sha256: e7c75033918a3636f85db4e6fc77ff480c838b5738b59b39d3d412a61279dc35

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

module Docile
  # @api private
  #
  # A namespace for functions relating to the execution of a block against a
  # proxy object.
  module Execution
    # Execute a block in the context of an object whose methods represent the
    # commands in a DSL, using a specific proxy class.
    #
    # @param dsl        [Object] context object whose methods make up the
    #                            (initial) DSL
    # @param proxy_type [FallbackContextProxy, ChainingFallbackContextProxy]
    #                            which class to instantiate as proxy context
    # @param args       [Array]  arguments to be passed to the block
    # @param block      [Proc]   the block of DSL commands to be executed
    # @return           [Object] the return value of the block
    def exec_in_proxy_context(dsl, proxy_type, *args, &block)
      block_context = eval("self", block.binding)
      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
        if block_context.respond_to?(:__docile_undo_fallback__)
          block_context.send(:__docile_undo_fallback__)
        end

        block_context.instance_variables.each do |ivar|
          next unless proxy_context.instance_variables.include?(ivar)
          value_from_dsl_proxy = proxy_context.instance_variable_get(ivar)
          block_context.instance_variable_set(ivar, value_from_dsl_proxy)
        end
      end
    end

    ruby2_keywords :exec_in_proxy_context if respond_to?(:ruby2_keywords, true)
    module_function :exec_in_proxy_context
  end
end

Version data entries

4 entries across 3 versions & 2 rubygems

Version Path
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/docile-1.3.5/lib/docile/execution.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/docile-1.3.5/lib/docile/execution.rb
tdiary-5.1.5 vendor/bundle/ruby/2.7.0/gems/docile-1.3.5/lib/docile/execution.rb
docile-1.3.5 lib/docile/execution.rb