Class: Nucleon::Codes
- Inherits:
-
Object
- Object
- Nucleon::Codes
- Defined in:
- lib/core/codes.rb
Constant Summary collapse
- @@registry =
Code index
{}
- @@status_index =
{}
- @@next_code =
0
Class Method Summary collapse
-
.code(name) ⇒ Object
—————————————————————————– Code construction.
-
.codes(*codes) ⇒ Object
—.
-
.index(status_code = nil) ⇒ Object
—.
-
.registry ⇒ Object
—.
-
.render_index(status_code = nil) ⇒ Object
—.
-
.render_status(status_code = nil) ⇒ Object
—.
-
.reset ⇒ Object
–.
-
.status_index ⇒ Object
—.
Instance Method Summary collapse
-
#[](name) ⇒ Object
—————————————————————————– Return status codes on demand.
-
#method_missing(method, *args, &block) ⇒ Object
—.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
125 126 127 |
# File 'lib/core/codes.rb', line 125 def method_missing(method, *args, &block) self[method] end |
Class Method Details
.code(name) ⇒ Object
Code construction
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/core/codes.rb', line 71 def self.code(name) name = name.to_sym unless registry.has_key?(name) status_code = @@next_code @@next_code = @@next_code + 1 # TODO: Add more information to the index (like a help message) @@registry[name] = status_code @@status_index[status_code] = name.to_s end end |
.codes(*codes) ⇒ Object
86 87 88 89 90 |
# File 'lib/core/codes.rb', line 86 def self.codes(*codes) codes.each do |name| code(name) end end |
.index(status_code = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/core/codes.rb', line 26 def self.index(status_code = nil) if status_code.nil? || ! status_code.integer? @@status_index else status_code = status_code.to_i if @@status_index.has_key?(status_code) @@status_index[status_code] else @@status_index[registry[:unknown_status]] end end end |
.registry ⇒ Object
14 15 16 |
# File 'lib/core/codes.rb', line 14 def self.registry @@registry end |
.render_index(status_code = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/core/codes.rb', line 53 def self.render_index(status_code = nil) output = "Status index:\n" @@status_index.each do |code, name| name = name.gsub(/_/, ' ').capitalize if ! status_code.nil? && status_code == code output << sprintf(" [ %3i ] - %s\n", code, name) else output << sprintf(" %3i - %s\n", code, name) end end output << "\n" output end |
.render_status(status_code = nil) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/core/codes.rb', line 42 def self.render_status(status_code = nil) if ! status_code.nil? && @@status_index.has_key?(status_code) status_name = @@status_index[status_code] else status_name = @@status_index[registry[:unknown_status]] end sprintf(" [ %3i ] - %s\n", status_code, status_name.gsub(/_/, ' ').capitalize) end |
.reset ⇒ Object
–
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/core/codes.rb', line 94 def self.reset @@registry = {} @@status_index = {} @@next_code = 0 code(:success) # This must be added first (needs to be 0) code(:help_wanted) code(:unknown_status) code(:action_unprocessed) code(:batch_error) code(:validation_failed) code(:access_denied) end |
.status_index ⇒ Object
20 21 22 |
# File 'lib/core/codes.rb', line 20 def self.status_index @@status_index end |
Instance Method Details
#[](name) ⇒ Object
Return status codes on demand
113 114 115 116 117 118 119 120 121 |
# File 'lib/core/codes.rb', line 113 def [](name) name = name.to_sym if @@registry.has_key?(name) @@registry[name] else @@registry[:unknown_status] end end |