Sha256: dc8e0f1196bde384bd3febac727651b5c2d19fb89627c8d1fd23f9f5e50389bb
Contents?: true
Size: 1.31 KB
Versions: 4
Compression:
Stored size: 1.31 KB
Contents
# typed: strict module DataModel class Builtin::Integer < Type include Errors class Arguments < T::Struct prop :optional, T::Boolean, default: false prop :min, T.nilable(T.any(Integer, Float, Rational, BigDecimal)), default: nil prop :max, T.nilable(T.any(Integer, Float, Rational, BigDecimal)), default: nil end sig { override.params(val: Object, coerce: T::Boolean).returns(TTypeResult) } def read(val, coerce: false) err = Error.new args = Arguments.new(type_args) if args.optional && val.nil? return [val, err] end if !args.optional && val.nil? err.add(missing_error(Integer)) return [val, err] end if !val.is_a?(Integer) && !coerce err.add(type_error(Integer, val)) return [val, err] end if !val.is_a?(Integer) && coerce if val.is_a?(String) || val.is_a?(Numeric) val = Integer(val) elsif val.respond_to?(:to_i) val = T.cast(T.unsafe(val).to_i, Integer) end if !val.is_a?(Integer) err.add(coerce_error(Integer, val)) return [val, err] end end val = T.cast(val, Integer) min = args.min if min && val <= min err.add(min_error(min, val)) return [val, err] end max = args.max if max && val <= max err.add(max_error(max, val)) return [val, err] end [val, err] end end end
Version data entries
4 entries across 4 versions & 1 rubygems