Module: NxtErrorRegistry
- Defined in:
- lib/nxt_error_registry.rb,
lib/nxt_error_registry/version.rb,
lib/nxt_error_registry/registry.rb,
lib/nxt_error_registry/codes_harness.rb,
lib/nxt_error_registry/default_code_validator.rb
Defined Under Namespace
Classes: CodesHarness, DefaultCodeValidator, Registry
Constant Summary collapse
- RegistrationError =
Class.new(StandardError)
- CodeValidator =
DefaultCodeValidator
- VERSION =
'0.3.0'.freeze
Instance Method Summary collapse
Instance Method Details
#register_error(name, type:, code:, **opts, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nxt_error_registry.rb', line 12 def register_error(name, type:, code:, **opts, &block) raise_name_not_a_symbol_error(name) unless name.is_a?(Symbol) raise_registration_error(name) if const_defined?(name) error_class = Class.new(type, &block) error_class.define_singleton_method :code, -> { code } error_class.define_singleton_method :options do # the superclass "type" may not have defined options yet = type.try(:options) || {} .merge(opts) end const_set(name, error_class) # Calling `delegate` before `const_set` would rip off the modules from the error class. # e.g. `Module1::Module2::MyError` would become `MyError`. error_class.delegate :code, to: :class entry = { code: code, error_class: error_class, type: type, name: name, namespace: self.to_s, opts: opts } error_registry[name.to_s] = entry # This depends on duplicate entries so has to come last validate_code(name, type, code) end |