lib/dry/behaviour/black_tie.rb in dry-behaviour-0.11.2 vs lib/dry/behaviour/black_tie.rb in dry-behaviour-0.12.0
- old
+ new
@@ -52,19 +52,26 @@
receiver.class.ancestors.lazy.map do |c|
BlackTie.implementations[self].fetch(c, nil)
end.reject(&:nil?).first
end
- BlackTie.protocols[self].each do |method, *_| # FIXME: CHECK ARITY HERE
- singleton_class.send :define_method, method do |receiver = nil, *args|
+ BlackTie.protocols[self].each do |method, *_m_args, **_m_kwargs| # FIXME: CHECK ARITY HERE
+ singleton_class.send :define_method, method do |receiver, *args, **kwargs|
impl = implementation_for(receiver)
+
raise Dry::Protocol::NotImplemented.new(
:protocol, inspect,
method: method, receiver: receiver, args: args, self: self
) unless impl
+
begin
- impl[method].(*args.unshift(receiver))
+ # [AM] [v1] [FIXME] for modern rubies `if` is redundant
+ if kwargs.empty?
+ impl[method].(*args.unshift(receiver))
+ else
+ impl[method].(*args.unshift(receiver), **kwargs)
+ end
rescue => e
raise Dry::Protocol::NotImplemented.new(
:nested, inspect,
cause: e,
method: method, receiver: receiver, args: args, impl: impl, self: self
@@ -87,11 +94,11 @@
params.map do |p, type|
if type && !PARAM_TYPES.include?(type)
BlackTie.Logger.warn(UNKNOWN_TYPE_DECLARATION % [Dry::BlackTie.proto_caller, type, self.inspect, name])
type = nil
end
- [type || PARAM_TYPES.include?(p) ? p : :req, p]
+ [type || (PARAM_TYPES.include?(p) ? p : :req), p]
end
BlackTie.protocols[self][name] = params
end
def defimpl(protocol = nil, target: nil, delegate: [], map: {}, &λ)
@@ -107,12 +114,17 @@
mod.methods(false).tap do |meths|
(NORMALIZE_KEYS.(protocol) - meths).each_with_object(meths) do |m, acc|
if BlackTie.protocols[protocol][:__implicit_inheritance__]
mod.singleton_class.class_eval do
- define_method m do |this, *♿_args, &♿_λ|
- super(this, *♿_args, &♿_λ)
+ define_method m do |this, *♿_args, **♿_kwargs, &♿_λ|
+ # [AM] [v1] [FIXME] for modern rubies `if` is redundant
+ if ♿_kwargs.empty?
+ super(this, *♿_args, &♿_λ)
+ else
+ super(this, *♿_args, **♿_kwargs, &♿_λ)
+ end
end
end
else
BlackTie.Logger.warn(
IMPLICIT_DELEGATE_DEPRECATION % [Dry::BlackTie.proto_caller, protocol.inspect, m, target]
@@ -141,10 +153,10 @@
end
end
end
module_function :defimpl
- PARAM_TYPES = %i[req opt rest keyrest block]
+ PARAM_TYPES = %i[req opt rest key keyrest keyreq block]
DELEGATE_METHOD = lambda do |klazz, (source, target)|
klazz.class_eval do
define_method(source, &Dry::DEFINE_METHOD.curry[target])
end