# 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