lib/data_model.rb in data_model-0.0.1 vs lib/data_model.rb in data_model-0.1.0

- old
+ new

@@ -1,28 +1,44 @@ +# typed: strict + +require "logger" +require "bigdecimal" +require "date" +require "time" + +require "bundler/setup" require "zeitwerk" +require "sorbet-runtime" -loader = Zeitwerk::Loader.for_gem +loader = T.let(Zeitwerk::Loader.for_gem, Zeitwerk::Loader) loader.setup module DataModel + extend T::Sig extend self - def validate(schema, data, registry: Registry.instance) - _, err = read(schema, data, registry:) - err.empty? || err.values.all?(&:empty?) - end + TSchema = T.type_alias { T::Array[Object] } + TData = T.type_alias { Object } - def read(schema, data, registry: Registry.instance) - Model.read(compiled(schema, registry:), data) - end + # an error is a tuple of [error_type, error_context], where context + # provides additional information about the error + TError = T.type_alias { [Symbol, Object] } - def write(schema, data, registry: Registry.instance) - Model.write(compiled(schema, registry:), data) - end + # a map of symbol => type, suitable for sending to a TypeRegistry + TTypeMap = T.type_alias { T::Hash[Symbol, T.class_of(Type)] } - def compiled(model, registry: Registry.instance) - if model in Array - model = Scanner.scan(model, registry) - end - model + # Scan a schema and create a data model, which is a configured type. + sig { params(schema: TSchema, registry: TypeRegistry).returns(Model) } + def define(schema, registry: TypeRegistry.instance) + scanned = Scanner.scan(schema, registry) + + type = registry.type( + scanned.type, + args: scanned.args, + params: scanned.params, + ) + + model = Model.new(schema, type) + + return model end end