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 1 commit
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
44 changes: 44 additions & 0 deletions spec/docile_spec.rb
Expand Up @@ -467,6 +467,50 @@ def set(v0, v1:, v2:)
end
end

context "when a DSL method has a double splat" do
class DSLMethodWithDoubleSplat
attr_reader :arguments, :options

def configure(*arguments, **options)
@arguments = arguments.dup
@options = options.dup
end
end

let(:dsl) do
DSLMethodWithDoubleSplat.new
end

it "correctly passes keyword arguments" do
Docile.dsl_eval(dsl) do
configure(1, a: 1)
end

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 2" do
Docile.dsl_eval(dsl) do
configure(1, { a: 1 })
end

expect(dsl.arguments).to eq [1]
expect(dsl.options).to eq({ a: 1 })
end
else
it "correctly passes hash arguments on Ruby 3+" do
Docile.dsl_eval(dsl) do
configure(1, { a: 1 })
end

expect(dsl.arguments).to eq [1, { a: 1 }]
expect(dsl.options).to eq({})
end
end
end

describe ".dsl_eval_with_block_return" do
let(:array) { [] }
let!(:result) { execute_dsl_against_array }
Expand Down