lib/dry/behaviour/cerberus.rb in dry-behaviour-0.5.2 vs lib/dry/behaviour/cerberus.rb in dry-behaviour-0.6.0
- old
+ new
@@ -80,10 +80,14 @@
guarded.guarded_methods.each do |name, clauses|
H.umbrella(guarded, name, clauses)
end
end
+ # rubocop:disable Metrics/MethodLength
+ # rubocop:disable Metrics/CyclomaticComplexity
+ # rubocop:disable Metrics/PerceivedComplexity
+ # rubocop:disable Style/AsciiComments
def umbrella(guarded, name, clauses)
guarded.prepend(Module.new do
define_method name do |*args, **params, &cb|
found = clauses.each_with_index.detect do |(hash, m), idx|
next if m.arity >= 0 && m.arity != args.size
@@ -91,25 +95,31 @@
when NilClass then m.arity < 0
when Integer then hash == args.size
end
next if hash.nil?
hash.all? do |param, condition|
- type_var, idx = m.parameters.each_with_index.detect { |(_type, var), idx| var == param }
+ type_var, idx = m.parameters.each_with_index.detect { |(_type, var), _i| var == param }
# rubocop:disable Style/CaseEquality
- # rubocop:disable Style/RescueModifier
- idx && (case type_var.first
- when :req, :opt then condition === args[idx] rescue false
- when :block then condition === cb
- end)
- # rubocop:enable Style/RescueModifier
+ idx && begin
+ case type_var.first
+ when :req, :opt then condition === args[idx]
+ when :block then condition === cb
+ end
+ rescue => _e
+ false # we don’t care
+ end
# rubocop:enable Style/CaseEquality
end
end
raise ::Dry::Guards::NotMatched.new(*args, **params, &cb) unless found
::Dry::DEFINE_METHOD.(H.alias_name(found.first.last, found.last), self, *args, **params, &cb)
end
end)
end
+ # rubocop:enable Style/AsciiComments
+ # rubocop:enable Metrics/PerceivedComplexity
+ # rubocop:enable Metrics/CyclomaticComplexity
+ # rubocop:enable Metrics/MethodLength
end
private_constant :H
end
end