From 5a9d1685f1e8e621e1838f46b32cbbdd5d645025 Mon Sep 17 00:00:00 2001 From: Jake Goulding Date: Tue, 25 Apr 2023 16:07:36 -0400 Subject: [PATCH] Avoid throwing an exception for failed coercions with dry-types 1.x dry-types 0.15 and 1.x changed how failed coercions are reported. Older versions returned the original value while newer versions raise an exception. 0.14.1: ```ruby Types::Params::Integer['cow'] # => "cow" ``` 1.7.1: ```ruby Types::Params::Integer['cow'] # *** Dry::Types::CoercionError Exception: invalid value for Integer(): "cow" ``` Fortunately, the constructor accepts an optional block: > When a block is passed, `{#call}` will never throw an exception on > failed coercion, instead it will call the block. Doubly-fortunately, the block appears to be ignored on older versions of dry-types. To preserve the current behavior, we update to use a block that returns the original value. --- lib/disposable/twin/coercion.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/disposable/twin/coercion.rb b/lib/disposable/twin/coercion.rb index b360962..f850d77 100644 --- a/lib/disposable/twin/coercion.rb +++ b/lib/disposable/twin/coercion.rb @@ -25,7 +25,7 @@ def coercing_setter!(name, type, nilify=false) mod = Module.new do define_method("#{name}=") do |value| - super type.(value) + super type.(value) { value } end end include mod