Class: DataModel::Registry
- Inherits:
-
Object
- Object
- DataModel::Registry
- 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 ⇒ Hash
Default error messages that will be used if alternative error messages are not given.
-
.default_types ⇒ Hash
Default types that will be used if alternative type map is not given.
-
.instance(types: default_types, errors: default_error_messages) ⇒ Registry
Singleton instance that will be used globally unless instances given.
-
.register(name, type) ⇒ void
Register a type on the global instance.
Instance Method Summary collapse
-
#error_message(error) ⇒ String
Build the error message for a given error.
-
#error_message_builders ⇒ Hash
Get the error message builders.
-
#error_messages(error) ⇒ Hash
Build error messages from error object.
-
#initialize(types: self.class.default_types, errors: self.class.default_error_messages) ⇒ Registry
constructor
Instanciate a new type registry.
-
#register(name, type) ⇒ void
Register a type on this instance.
-
#register_error_message(type, &block) ⇒ void
Register a custom error message for use with custom errors.
-
#type(name, args: {}, params: nil) ⇒ Type
Access and configure registered type.
-
#type?(name) ⇒ Boolean
Check if a type is registered.
Constructor Details
#initialize(types: self.class.default_types, errors: self.class.default_error_messages) ⇒ Registry
Instanciate a new type registry. Default errors will always be used, but additional errors can be registered.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/data_model/registry.rb', line 38 def initialize(types: self.class.default_types, errors: self.class.) @error_messages = nil if errors errors.each { |type, builder| (type, &builder) } end @types = {} types.each { |(name, type)| register(name, type) } end |
Class Method Details
.default_error_messages ⇒ Hash
Default error messages that will be used if alternative error messages are not given
13 14 15 |
# File 'lib/data_model/registry.rb', line 13 def self. Errors. end |
.default_types ⇒ Hash
Default types that will be used if alternative type map is not given
7 8 9 |
# File 'lib/data_model/registry.rb', line 7 def self.default_types Builtin.types end |
.instance(types: default_types, errors: default_error_messages) ⇒ Registry
Singleton instance that will be used globally unless instances given
21 22 23 |
# File 'lib/data_model/registry.rb', line 21 def self.instance(types: default_types, errors: ) @instance ||= new(types:, errors:) end |
.register(name, type) ⇒ void
This method returns an undefined value.
Register a type on the global instance
29 30 31 |
# File 'lib/data_model/registry.rb', line 29 def self.register(name, type) instance.register(name, type) end |
Instance Method Details
#error_message(error) ⇒ String
Build the error message for a given error
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/data_model/registry.rb', line 107 def (error) type = error[0] ctx = error[1] builder = [type] if builder.nil? raise "no error message builder for #{type}" end builder.call(ctx) end |
#error_message_builders ⇒ Hash
Get the error message builders
95 96 97 98 99 100 101 |
# File 'lib/data_model/registry.rb', line 95 def if @error_messages.nil? @error_messages ||= {} end @error_messages end |
#error_messages(error) ⇒ Hash
Build error messages from error object
123 124 125 126 127 |
# File 'lib/data_model/registry.rb', line 123 def (error) error.to_h.transform_values do |error_list| error_list.map { |e| (e) } end end |
#register(name, type) ⇒ void
This method returns an undefined value.
Register a type on this instance
53 54 55 |
# File 'lib/data_model/registry.rb', line 53 def register(name, type) @types[name] = type end |
#register_error_message(type, &block) ⇒ void
This method returns an undefined value.
Register a custom error message for use with custom errors
89 90 91 |
# File 'lib/data_model/registry.rb', line 89 def (type, &block) [type] = block end |
#type(name, args: {}, params: nil) ⇒ Type
Access and configure registered type
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/data_model/registry.rb', line 69 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
Check if a type is registered
60 61 62 |
# File 'lib/data_model/registry.rb', line 60 def type?(name) @types.key?(name) end |