lib/data_model/builtin/symbol.rb in data_model-0.4.0 vs lib/data_model/builtin/symbol.rb in data_model-0.5.0

- old
+ new

@@ -1,50 +1,51 @@ -# typed: strict - module DataModel + # Symbol type class Builtin::Symbol < Type include Errors - class Arguments < T::Struct - prop :optional, T::Boolean, default: false - prop :included, T::Array[Symbol], default: [] - prop :excluded, T::Array[Symbol], default: [] + # Arguments for this type + class Arguments < Struct + prop :optional, :boolean, default: false + prop :included, [:array, :symbol], default: [] + prop :excluded, [:array, :symbol], default: [] end - sig { override.params(val: Object, coerce: T::Boolean).returns(TTypeResult) } + # 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 # optional & missing if args.optional && val.nil? return [val, err] end if !args.optional && val.nil? - err.add(missing_error(Symbol)) + err.add(missing_error(type_name)) return [val, err] end # type error if !val.is_a?(Symbol) && !coerce - err.add(type_error(Symbol, val)) + err.add(type_error(type_name, val)) return [val, err] end # attempt coercion if !val.is_a?(Symbol) && coerce if val.is_a?(String) val = val.intern elsif val.respond_to?(:to_sym) - val = T.unsafe(val).to_sym + val = val.to_sym else - err.add(coerce_error(Symbol, val)) + err.add(coerce_error(type_name, val)) return [val, err] end end - - val = T.cast(val, Symbol) # inclusion if args.included.any? && !args.included.include?(val) err.add(inclusion_error(args.included)) return [val, err]