module DataModel # A time type class Builtin::Time < Type include Errors # Arguments for this type class Arguments < Struct prop :optional, :boolean, default: false prop :earliest, [:time, { optional: true }], default: nil prop :latest, [:time, { optional: true }], default: nil end # read a value, and validate it # @param val [Object] the value to read # @param coerce [Boolean] whether to coerce the value # @return [Array(Object, Error)] the result of reading the value def read(val, coerce: false) args = Arguments.new(type_args) err = Error.new # missing, but allowed, don't do any more checks if val.nil? && args.optional return [val, err] end # missing, but not allowed, don't do any more checks if val.nil? err.add(missing_error(type_name)) return [val, err] end # coercion is enabled, and the value is a string, try to parse it if val.is_a?(String) && coerce begin val = Time.parse(val) rescue ArgumentError err.add(type_error(type_name, val)) return [val, err] end end # not a date, don't do any more checks if !val.is_a?(Time) err.add(type_error(type_name, val)) return [val, err] end # date is before the earliest point allowed if args.earliest && (val < args.earliest) error = earliest_error(args.earliest, val) err.add(error) end # date is after the latest point allowed if args.latest && (val > args.latest) error = latest_error(args.latest, val) err.add(error) end return [val, err] end end end