Sha256: c43bb6d4bc5eaef45b24864f06a81b51766f3933e6013b04fc6e7143185e5304
Contents?: true
Size: 1.94 KB
Versions: 3
Compression:
Stored size: 1.94 KB
Contents
# typed: strict module DataModel # TypeRegistry allows for different type implementations to be used by the scanner. # It also acts as an error message registry, mostly for pragmatic reasons. class TypeRegistry include Errors extend T::Sig # Default types that will be used if alternative type map is not given sig { returns(TTypeMap) } def self.default_types Builtin.types end # Singleton instance that will be used globally unless instances given sig { params(types: TTypeMap, errors: T.nilable(TErrorMessages)).returns(TypeRegistry) } def self.instance(types: default_types, errors: nil) @instance ||= T.let(new(types:, errors:), T.nilable(TypeRegistry)) end # Register a type on the global instance sig { params(name: Symbol, type: T.class_of(Type)).void } def self.register(name, type) instance.register(name, type) end # Instanciate a new type registry. Default errors will always be used, but additional # errors can be registered. sig { params(types: TTypeMap, errors: T.nilable(TErrorMessages)).void } def initialize(types: self.class.default_types, errors: nil) if errors errors.each { |type, builder| register_error_message(type, &builder) } end @types = T.let({}, TTypeMap) types.each { |(name, type)| register(name, type) } end # Register a type on this instance sig { params(name: Symbol, type: T.class_of(Type)).void } def register(name, type) @types[name] = type end # Check if a type is registered sig { params(name: Symbol).returns(T::Boolean) } def type?(name) @types.key?(name) end # Access and configure registered type sig { params(name: Symbol, args: Type::TArguments, params: T.nilable(T::Array[Object])).returns(Type) } def type(name, args: {}, params: nil) if !type?(name) raise "#{name} is not registered as a type" end t = @types.fetch(name).new(args, registry: self) if params t.configure(params) end return t end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
data_model-0.3.0 | lib/data_model/type_registry.rb |
data_model-0.2.0 | lib/data_model/type_registry.rb |
data_model-0.1.0 | lib/data_model/type_registry.rb |