Skip to content

Commit

Permalink
fix warning related to keywoard argument (#45)
Browse files Browse the repository at this point in the history
* add workaround to avoid warnings related to keyword arguments (refs: #44)
  • Loading branch information
taichi-ishitani authored Dec 17, 2020
1 parent eec6ac6 commit be7a988
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/docile/fallback_context_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,25 @@ def instance_variables

# Proxy all methods, excluding {NON_PROXIED_METHODS}, first to `receiver`
# and then to `fallback` if not found.
def method_missing(method, *args, &block)
if @__receiver__.respond_to?(method.to_sym)
@__receiver__.__send__(method.to_sym, *args, &block)
args_string =
if RUBY_VERSION >= "2.7.0"
"*args, **kwargs, &block"
else
begin
@__fallback__.__send__(method.to_sym, *args, &block)
rescue NoMethodError => e
e.extend(BacktraceFilter)
raise e
"*args, &block"
end
class_eval(<<-METHOD)
def method_missing(method, #{args_string})
if @__receiver__.respond_to?(method.to_sym)
@__receiver__.__send__(method.to_sym, #{args_string})
else
begin
@__fallback__.__send__(method.to_sym, #{args_string})
rescue NoMethodError => e
e.extend(BacktraceFilter)
raise e
end
end
end
end
METHOD
end
end
30 changes: 30 additions & 0 deletions spec/docile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,36 @@ def dsl_eval_string(string)
end
end
end

if RUBY_VERSION >= "2.0.0"
context "when a DSL method has a keyword argument" do
class DSLMethodWithKeywordArgument
attr_reader :v0, :v1, :v2
class_eval(<<-METHOD)
def set(v0, v1:, v2:)
@v0 = v0
@v1 = v1
@v2 = v2
end
METHOD
end

let(:dsl) do
DSLMethodWithKeywordArgument.new
end

it "calls such DSL methods with no stderr output" do
# This is to check warnings related to keyword argument is not output.
# See: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
expect { Docile.dsl_eval(dsl) { set(0, v2: 2, v1: 1) } }.
not_to output.to_stderr

expect(dsl.v0).to eq 0
expect(dsl.v1).to eq 1
expect(dsl.v2).to eq 2
end
end
end
end

describe ".dsl_eval_with_block_return" do
Expand Down

0 comments on commit be7a988

Please sign in to comment.