Skip to content

Commit

Permalink
Add ruby2_keywords to delegating methods (#62)
Browse files Browse the repository at this point in the history
* Add ruby2_keywords to delegating methods
* Fix unit tests for Ruby 1.9
  • Loading branch information
jochenseeber committed Jan 11, 2021
1 parent 5f0e1df commit 5001479
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
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

0 comments on commit 5001479

Please sign in to comment.