From 8f108b278a7f5a9a17b7a63655bdfce9c976d82b Mon Sep 17 00:00:00 2001 From: Szymon Fiedler Date: Thu, 14 Nov 2024 12:24:28 +0100 Subject: [PATCH] Refactor with pattern matching It's here since Ruby 2.7, 3.0 is EOL already, so it's aligned with our maintenance policy: https://railseventstore.org/docs/v2/maintenance_policy/ --- .../browser/json_api_event.rb | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/ruby_event_store-browser/lib/ruby_event_store/browser/json_api_event.rb b/ruby_event_store-browser/lib/ruby_event_store/browser/json_api_event.rb index 78943bda6c..555e567161 100644 --- a/ruby_event_store-browser/lib/ruby_event_store/browser/json_api_event.rb +++ b/ruby_event_store-browser/lib/ruby_event_store/browser/json_api_event.rb @@ -30,18 +30,14 @@ def to_h def sanitize_infinity_values(value) case value - when Hash - value.transform_values { |v| sanitize_infinity_values(v) } - when Array - value.map { |v| sanitize_infinity_values(v) } - when Float - if value.infinite? - value.positive? ? "Infinity" : "-Infinity" - elsif value.nan? - "NaN" - else - value - end + in Hash => hash + hash.transform_values { |v| sanitize_infinity_values(v) } + in Array => array + array.map { |v| sanitize_infinity_values(v) } + in Float => f if f.infinite? + f.positive? ? "Infinity" : "-Infinity" + in Float => f if f.nan? + "NaN" else value end