Sha256: 89a1039b96e174638af5825dfbcbdede0026616cc9934b8dfb422a7cff8e78a5

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
data_model-0.6.1 lib/data_model/builtin/time.rb
data_model-0.6.0 lib/data_model/builtin/time.rb
data_model-0.5.0 lib/data_model/builtin/time.rb