Class: RocketPants::Errors
- Inherits:
-
Object
- Object
- RocketPants::Errors
- Defined in:
- lib/rocket_pants/errors.rb
Overview
A simple map of data about errors that the rocket pants system can handle.
Constant Summary collapse
- @@errors =
{}
Class Method Summary collapse
-
.[](name) ⇒ Error?
Looks up a specific error from the given name, returning nil if none are found.
-
.add(error) ⇒ Object
Adds a given Error class in the list of all errors, making it suitable for lookup via [].
-
.all ⇒ Hash{Symbol => RocketPants::Error}
Returns a hash of all known errors, keyed by their error name.
-
.register!(name, options = {}) ⇒ Object
Creates an error class to represent a given error state.
Class Method Details
.[](name) ⇒ Error?
Looks up a specific error from the given name, returning nil if none are found.
19 20 21 |
# File 'lib/rocket_pants/errors.rb', line 19 def self.[](name) @@errors[name.to_sym] end |
.add(error) ⇒ Object
Adds a given Error class in the list of all errors, making it suitable for lookup via [].
27 28 29 |
# File 'lib/rocket_pants/errors.rb', line 27 def self.add(error) @@errors[error.error_name] = error end |
.all ⇒ Hash{Symbol => RocketPants::Error}
Returns a hash of all known errors, keyed by their error name.
12 13 14 |
# File 'lib/rocket_pants/errors.rb', line 12 def self.all @@errors.dup end |
.register!(name, options = {}) ⇒ Object
Creates an error class to represent a given error state.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rocket_pants/errors.rb', line 39 def self.register!(name, = {}) klass_name = ([:class_name] || name.to_s.classify).to_sym base_klass = [:base] || Error raise ArgumentError, ":base must be a subclass of RocketPants::Error" unless base_klass <= Error klass = Class.new(base_klass) klass.error_name([:error_name] || name.to_s.underscore) klass.http_status([:http_status]) if [:http_status].present? ([:under] || RocketPants).const_set klass_name, klass add klass klass end |