Class: DataModel::Registry
- Inherits:
-
Object
- Object
- DataModel::Registry
- Extended by:
- T::Sig
- Defined in:
- lib/data_model/registry.rb
Overview
Registry allows for different type implementations to be used by the scanner. It also acts as an error message registry, mostly for pragmatic reasons.
Class Method Summary collapse
- .default_error_messages ⇒ Object
- .default_types ⇒ Object
- .instance(types: default_types, errors: default_error_messages) ⇒ Object
- .register(name, type) ⇒ Object
Instance Method Summary collapse
- #error_message(error) ⇒ Object
- #error_message_builders ⇒ Object
- #error_messages(error) ⇒ Object
-
#initialize(types: self.class.default_types, errors: self.class.default_error_messages) ⇒ Registry
constructor
A new instance of Registry.
- #register(name, type) ⇒ Object
- #register_error_message(type, &block) ⇒ Object
- #type(name, args: {}, params: nil) ⇒ Object
- #type?(name) ⇒ Boolean
Constructor Details
#initialize(types: self.class.default_types, errors: self.class.default_error_messages) ⇒ Registry
Returns a new instance of Registry.
35 36 37 38 39 40 41 42 43 |
# File 'lib/data_model/registry.rb', line 35 def initialize(types: self.class.default_types, errors: self.class.) @error_messages = T.let(nil, T.nilable(Errors::TErrorMessages)) if errors errors.each { |type, builder| (type, &builder) } end @types = T.let({}, TTypeMap) types.each { |(name, type)| register(name, type) } end |
Class Method Details
.default_error_messages ⇒ Object
16 17 18 |
# File 'lib/data_model/registry.rb', line 16 def self. Errors. end |
.default_types ⇒ Object
11 12 13 |
# File 'lib/data_model/registry.rb', line 11 def self.default_types Builtin.types end |
.instance(types: default_types, errors: default_error_messages) ⇒ Object
22 23 24 |
# File 'lib/data_model/registry.rb', line 22 def self.instance(types: default_types, errors: ) @instance ||= T.let(new(types:, errors:), T.nilable(Registry)) end |
.register(name, type) ⇒ Object
28 29 30 |
# File 'lib/data_model/registry.rb', line 28 def self.register(name, type) instance.register(name, type) end |
Instance Method Details
#error_message(error) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/data_model/registry.rb', line 93 def (error) type = T.let(error[0], Symbol) ctx = T.let(error[1], T.untyped) builder = [type] if builder.nil? raise "no error message builder for #{type}" end builder.call(ctx) end |
#error_message_builders ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/data_model/registry.rb', line 83 def if @error_messages.nil? @error_messages ||= T.let({}, T.nilable(Errors::TErrorMessages)) end @error_messages end |
#error_messages(error) ⇒ Object
108 109 110 111 112 |
# File 'lib/data_model/registry.rb', line 108 def (error) error.to_h.transform_values do |error_list| error_list.map { |e| (e) } end end |
#register(name, type) ⇒ Object
47 48 49 |
# File 'lib/data_model/registry.rb', line 47 def register(name, type) @types[name] = type end |
#register_error_message(type, &block) ⇒ Object
77 78 79 |
# File 'lib/data_model/registry.rb', line 77 def (type, &block) [type] = block end |
#type(name, args: {}, params: nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/data_model/registry.rb', line 59 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 |
#type?(name) ⇒ Boolean
53 54 55 |
# File 'lib/data_model/registry.rb', line 53 def type?(name) @types.key?(name) end |