mirror of
https://github.com/ms-ati/docile
synced 2023-03-27 23:21:52 -04:00
Refactor to share common code between #dsl_eval and #dsl_eval_immutable
This commit is contained in:
parent
448a1155f6
commit
cb26f1f69a
1 changed files with 10 additions and 16 deletions
|
@ -37,27 +37,21 @@ module Docile
|
|||
# `dsl` context object
|
||||
# @return [Object] the `dsl` context object after executing the block
|
||||
def dsl_eval(dsl, *args, &block)
|
||||
block_context = eval("self", block.binding)
|
||||
proxy_context = FallbackContextProxy.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
|
||||
exec_in_proxy_context(dsl, FallbackContextProxy, *args, &block)
|
||||
dsl
|
||||
end
|
||||
module_function :dsl_eval
|
||||
|
||||
def dsl_eval_immutable(dsl, *args, &block)
|
||||
exec_in_proxy_context(dsl, ChainingFallbackContextProxy, *args, &block)
|
||||
end
|
||||
module_function :dsl_eval_immutable
|
||||
|
||||
private
|
||||
|
||||
def exec_in_proxy_context(dsl, proxy_type, *args, &block)
|
||||
block_context = eval("self", block.binding)
|
||||
proxy_context = ChainingFallbackContextProxy.new(dsl, block_context)
|
||||
proxy_context = proxy_type.new(dsl, proxy_type.new(dsl, block_context))
|
||||
begin
|
||||
block_context.instance_variables.each do |ivar|
|
||||
value_from_block = block_context.instance_variable_get(ivar)
|
||||
|
@ -71,5 +65,5 @@ module Docile
|
|||
end
|
||||
end
|
||||
end
|
||||
module_function :dsl_eval_immutable
|
||||
module_function :exec_in_proxy_context
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue