Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ruby2_keywords to delegating methods #62

Merged
merged 2 commits into from Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/docile.rb
Expand Up @@ -44,6 +44,8 @@ def dsl_eval(dsl, *args, &block)
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
Expand Down Expand Up @@ -83,6 +85,8 @@ def dsl_eval(dsl, *args, &block)
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,
Expand Down Expand Up @@ -120,5 +124,7 @@ def dsl_eval_with_block_return(dsl, *args, &block)
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
2 changes: 2 additions & 0 deletions lib/docile/chaining_fallback_context_proxy.rb
Expand Up @@ -16,5 +16,7 @@ class ChainingFallbackContextProxy < FallbackContextProxy
def method_missing(method, *args, &block)
@__receiver__ = super(method, *args, &block)
end

ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
end
end
2 changes: 2 additions & 0 deletions lib/docile/execution.rb
Expand Up @@ -36,6 +36,8 @@ def exec_in_proxy_context(dsl, proxy_type, *args, &block)
end
end
end

ruby2_keywords :exec_in_proxy_context if respond_to?(:ruby2_keywords, true)
module_function :exec_in_proxy_context
end
end
3 changes: 3 additions & 0 deletions lib/docile/fallback_context_proxy.rb
Expand Up @@ -61,6 +61,8 @@ def initialize(receiver, fallback)
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
Expand Down Expand Up @@ -94,6 +96,7 @@ def method_missing(method, *args, &block)
end
end
end

ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
end
end
41 changes: 41 additions & 0 deletions spec/docile_spec.rb
Expand Up @@ -465,6 +465,47 @@ def set(v0, v1:, v2:)
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
Expand Down