Add ruby2_keywords to delegating methods (#62)

* Add ruby2_keywords to delegating methods
* Fix unit tests for Ruby 1.9
This commit is contained in:
Jochen Seeber 2021-01-11 23:03:24 +01:00 committed by GitHub
parent 5f0e1df3b9
commit 5001479f54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 0 deletions

View File

@ -44,6 +44,8 @@ module Docile
exec_in_proxy_context(dsl, FallbackContextProxy, *args, &block)
dsl
end
ruby2_keywords :dsl_eval if respond_to?(:ruby2_keywords, true)
module_function :dsl_eval
# Execute a block in the context of an object whose methods represent the
@ -83,6 +85,8 @@ module Docile
def dsl_eval_with_block_return(dsl, *args, &block)
exec_in_proxy_context(dsl, FallbackContextProxy, *args, &block)
end
ruby2_keywords :dsl_eval_with_block_return if respond_to?(:ruby2_keywords, true)
module_function :dsl_eval_with_block_return
# Execute a block in the context of an immutable object whose methods,
@ -120,5 +124,7 @@ module Docile
def dsl_eval_immutable(dsl, *args, &block)
exec_in_proxy_context(dsl, ChainingFallbackContextProxy, *args, &block)
end
ruby2_keywords :dsl_eval_immutable if respond_to?(:ruby2_keywords, true)
module_function :dsl_eval_immutable
end

View File

@ -16,5 +16,7 @@ module Docile
def method_missing(method, *args, &block)
@__receiver__ = super(method, *args, &block)
end
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
end
end

View File

@ -36,6 +36,8 @@ module Docile
end
end
end
ruby2_keywords :exec_in_proxy_context if respond_to?(:ruby2_keywords, true)
module_function :exec_in_proxy_context
end
end

View File

@ -61,6 +61,8 @@ module Docile
end
end
singleton_class.send(:ruby2_keywords, :method_missing) if singleton_class.respond_to?(:ruby2_keywords, true)
# instrument a helper method to remove the above instrumentation
singleton_class.
send(:define_method, :__docile_undo_fallback__) do
@ -94,6 +96,7 @@ module Docile
end
end
end
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
end
end

View File

@ -465,6 +465,47 @@ describe Docile do
end
end
end
if RUBY_VERSION >= "2.0.0"
context "when a DSL method has a double splat" do
class DSLMethodWithDoubleSplat
attr_reader :arguments, :options
# Use class_eval because Ruby 1.x does not support double splat
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def configure(*arguments, **options)
@arguments = arguments.dup
@options = options.dup
end
METHOD
end
let(:dsl) { DSLMethodWithDoubleSplat.new }
it "correctly passes keyword arguments" do
Docile.dsl_eval(dsl) { configure(1, a: 1) }
expect(dsl.arguments).to eq [1]
expect(dsl.options).to eq({ a: 1 })
end
if RUBY_VERSION >= "3.0.0"
it "correctly passes hash arguments on Ruby 3+" do
Docile.dsl_eval(dsl) { configure(1, { a: 1 }) }
expect(dsl.arguments).to eq [1, { a: 1 }]
expect(dsl.options).to eq({})
end
elsif RUBY_VERSION >= "2.0.0"
it "correctly passes hash arguments on Ruby 2" do
Docile.dsl_eval(dsl) { configure(1, { a: 1 }) }
expect(dsl.arguments).to eq [1]
expect(dsl.options).to eq({ a: 1 })
end
end
end
end
end
describe ".dsl_eval_with_block_return" do