From d8e5751a1d7ad027519463134fe1a861d02494fc Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 31 Dec 2016 19:22:49 +0900 Subject: Extract `casted_true`/`casted_false` for `Type::ImmutableString` The only difference between `Type::ImmutableString` and its subclasses is the representation of the casted booleans. Prefer extracting `casted_true`/`casted_false` and override these by subclasses. --- activemodel/lib/active_model/type/immutable_string.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/type/immutable_string.rb b/activemodel/lib/active_model/type/immutable_string.rb index 58268540e5..c967d428ac 100644 --- a/activemodel/lib/active_model/type/immutable_string.rb +++ b/activemodel/lib/active_model/type/immutable_string.rb @@ -8,8 +8,8 @@ module ActiveModel def serialize(value) case value when ::Numeric, ActiveSupport::Duration then value.to_s - when true then "t" - when false then "f" + when true then casted_true + when false then casted_false else super end end @@ -19,12 +19,20 @@ module ActiveModel def cast_value(value) result = \ case value - when true then "t" - when false then "f" + when true then casted_true + when false then casted_false else value.to_s end result.freeze end + + def casted_true + "t".freeze + end + + def casted_false + "f".freeze + end end end end -- cgit v1.2.3