Module: FinchAPI::Converter Private
- Defined in:
- lib/finch-api/base_model.rb
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Class Method Summary collapse
-
.coerce(target, value) ⇒ Object
private
Based on ‘target`, transform `value` into `target`, to the extent possible:.
- .dump(target, value) ⇒ Object private
-
.try_strict_coerce(target, value) ⇒ Object
private
The underlying algorithm for computing maximal compatibility is subject to future improvements.
- .type_info(spec) ⇒ Proc private
Instance Method Summary collapse
- #coerce(value) ⇒ Object private
- #dump(value) ⇒ Object private
- #try_strict_coerce(value) ⇒ Array(true, Object, nil), Array(false, Boolean, Integer) private
Class Method Details
.coerce(target, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Based on ‘target`, transform `value` into `target`, to the extent possible:
1. if the given `value` conforms to `target` already, return the given `value`
2. if it's possible and safe to convert the given `value` to `target`, then the
converted value
3. otherwise, the given `value` unaltered
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/finch-api/base_model.rb', line 73 def coerce(target, value) case target in FinchAPI::Converter target.coerce(value) in Symbol case value in Symbol | String if (val = value.to_sym) == target val else value end in Module case target in -> { _1 <= NilClass } nil in -> { _1 <= Integer } value.is_a?(Numeric) ? Integer(value) : value in -> { _1 <= Float } value.is_a?(Numeric) ? Float(value) : value in -> { _1 <= Symbol } value.is_a?(String) ? value.to_sym : value in -> { _1 <= String } value.is_a?(Symbol) ? value.to_s : value in -> { _1 <= Date || _1 <= Time } value.is_a?(String) ? target.parse(value) : value in -> { _1 <= IO } value.is_a?(String) ? StringIO.new(value) : value else value end end end |
.dump(target, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
112 113 114 115 116 117 118 119 |
# File 'lib/finch-api/base_model.rb', line 112 def dump(target, value) case target in FinchAPI::Converter target.dump(value) else value end end |
.try_strict_coerce(target, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The underlying algorithm for computing maximal compatibility is subject to
future improvements.
Similar to `#.coerce`, used to determine the best union variant to decode into.
1. determine if strict-ish coercion is possible
2. return either result of successful coercion or if loose coercion is possible
3. return a score for recursively tallied count for fields that can be coerced
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/finch-api/base_model.rb', line 136 def try_strict_coerce(target, value) case target in FinchAPI::Converter target.try_strict_coerce(value) in Symbol case value in Symbol | String if (val = value.to_sym) == target [true, val, 1] else [false, false, 0] end in Module case [target, value] in [-> { _1 <= NilClass }, _] [true, nil, value.nil? ? 1 : 0] in [-> { _1 <= Integer }, Numeric] [true, Integer(value), 1] in [-> { _1 <= Float }, Numeric] [true, Float(value), 1] in [-> { _1 <= Symbol }, String] [true, value.to_sym, 1] in [-> { _1 <= String }, Symbol] [true, value.to_s, 1] in [-> { _1 <= Date || _1 <= Time }, String] Kernel.then do [true, target.parse(value), 1] rescue ArgumentError [false, false, 0] end in [_, ^target] [true, value, 1] else [false, false, 0] end end end |
.type_info(spec) ⇒ Proc
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/finch-api/base_model.rb', line 45 def type_info(spec) case spec in Hash type_info(spec.slice(:const, :enum, :union).first&.last) in Proc spec in FinchAPI::Converter | Module | Symbol -> { spec } in true | false -> { FinchAPI::BooleanModel } in NilClass | Integer | Float -> { spec.class } end end |
Instance Method Details
#coerce(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 |
# File 'lib/finch-api/base_model.rb', line 13 def coerce(value) = value |
#dump(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
20 |
# File 'lib/finch-api/base_model.rb', line 20 def dump(value) = value |
#try_strict_coerce(value) ⇒ Array(true, Object, nil), Array(false, Boolean, Integer)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
27 |
# File 'lib/finch-api/base_model.rb', line 27 def try_strict_coerce(value) = (raise NotImplementedError) |