1
0
Fork 0
mirror of https://github.com/ms-ati/docile synced 2023-03-27 23:21:52 -04:00

First light on Docile#dsl_eval_immutable

This commit is contained in:
Marc Siegel 2013-07-28 19:22:45 -04:00
parent cb6e5bbe65
commit 448a1155f6
3 changed files with 34 additions and 2 deletions

View file

@ -1,5 +1,6 @@
require "docile/version"
require "docile/fallback_context_proxy"
require "docile/chaining_fallback_context_proxy"
# Docile keeps your Ruby DSLs tame and well-behaved
# @see http://ms-ati.github.com/docile/
@ -53,4 +54,22 @@ module Docile
dsl
end
module_function :dsl_eval
def dsl_eval_immutable(dsl, *args, &block)
block_context = eval("self", block.binding)
proxy_context = ChainingFallbackContextProxy.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
module_function :dsl_eval_immutable
end

View file

@ -0,0 +1,13 @@
require "docile/fallback_context_proxy"
module Docile
class ChainingFallbackContextProxy < FallbackContextProxy
def method_missing(method, *args, &block)
@__receiver__ = super(method, *args, &block)
end
end
end

View file

@ -259,11 +259,11 @@ describe Docile do
end
end
pending "doesn't modify the original string" do
it "doesn't modify the original string" do
original.should == "Hello, world!"
end
pending "chains the commands in the block against the DSL context object" do
it "chains the commands in the block against the DSL context object" do
result.should == "!DLROW ,OLLEH"
end
end