2013-07-29 04:06:43 +00:00
|
|
|
module Docile
|
|
|
|
# @api private
|
|
|
|
#
|
2013-08-02 19:45:12 +00:00
|
|
|
# A namespace for functions relating to the execution of a block against a
|
2013-07-29 04:06:43 +00:00
|
|
|
# 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
|
2013-08-02 19:45:12 +00:00
|
|
|
# @param proxy_type [FallbackContextProxy, ChainingFallbackContextProxy]
|
|
|
|
# which class to instantiate as proxy context
|
2013-07-29 04:06:43 +00:00
|
|
|
# @param args [Array] arguments to be passed to the block
|
2013-08-02 19:45:12 +00:00
|
|
|
# @param block [Proc] the block of DSL commands to be executed
|
2013-07-29 04:06:43 +00:00
|
|
|
# @return [Object] the return value of the block
|
|
|
|
def exec_in_proxy_context(dsl, proxy_type, *args, &block)
|
2018-01-14 23:55:16 +00:00
|
|
|
block_context = eval("self", block.binding)
|
2014-01-10 03:46:26 +00:00
|
|
|
proxy_context = proxy_type.new(dsl, block_context)
|
2013-07-29 04:06:43 +00:00
|
|
|
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
|
2018-02-02 19:05:36 +00:00
|
|
|
|
2013-07-29 04:06:43 +00:00
|
|
|
proxy_context.instance_exec(*args, &block)
|
|
|
|
ensure
|
2018-02-02 19:05:36 +00:00
|
|
|
if block_context.respond_to?(:__docile_undo_fallback__)
|
|
|
|
block_context.send(:__docile_undo_fallback__)
|
|
|
|
end
|
|
|
|
|
2013-07-29 04:06:43 +00:00
|
|
|
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
|
|
|
|
module_function :exec_in_proxy_context
|
|
|
|
end
|
2018-01-14 23:55:16 +00:00
|
|
|
end
|