Class: CMDx::Deprecators
- Inherits:
-
Object
- Object
- CMDx::Deprecators
- Defined in:
- lib/cmdx/deprecators.rb,
lib/cmdx/deprecators/log.rb,
lib/cmdx/deprecators/warn.rb,
lib/cmdx/deprecators/error.rb
Overview
Registry of named deprecation actions consulted by ‘Deprecation#execute` to dispatch a task class’s deprecation. Ships with built-ins for ‘:log`, `:warn`, and `:error`. A deprecator is any callable accepting `call(task)`; the return value is discarded.
Defined Under Namespace
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#deregister(name) ⇒ Deprecators
Self for chaining.
- #empty? ⇒ Boolean
-
#initialize ⇒ Deprecators
constructor
A new instance of Deprecators.
- #initialize_copy(source) ⇒ void
-
#key?(name) ⇒ Boolean
Whether a deprecator is registered under ‘name`.
-
#lookup(name) ⇒ #call
The registered deprecator.
-
#register(name, callable = nil, &block) { ... } ⇒ Deprecators
Registers a named deprecator, overwriting any existing entry.
-
#resolve(spec) ⇒ #call?
Resolves a ‘deprecation` declaration’s value to a concrete callable.
- #size ⇒ Integer
Constructor Details
#initialize ⇒ Deprecators
Returns a new instance of Deprecators.
12 13 14 15 16 17 18 |
# File 'lib/cmdx/deprecators.rb', line 12 def initialize @registry = { log: Deprecators::Log, warn: Deprecators::Warn, error: Deprecators::Error } end |
Instance Attribute Details
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
10 11 12 |
# File 'lib/cmdx/deprecators.rb', line 10 def registry @registry end |
Instance Method Details
#deregister(name) ⇒ Deprecators
Returns self for chaining.
50 51 52 53 |
# File 'lib/cmdx/deprecators.rb', line 50 def deregister(name) registry.delete(name.to_sym) self end |
#empty? ⇒ Boolean
91 92 93 |
# File 'lib/cmdx/deprecators.rb', line 91 def empty? registry.empty? end |
#initialize_copy(source) ⇒ void
This method returns an undefined value.
22 23 24 |
# File 'lib/cmdx/deprecators.rb', line 22 def initialize_copy(source) @registry = source.registry.dup end |
#key?(name) ⇒ Boolean
Returns whether a deprecator is registered under ‘name`.
57 58 59 |
# File 'lib/cmdx/deprecators.rb', line 57 def key?(name) registry.key?(name.to_sym) end |
#lookup(name) ⇒ #call
Returns the registered deprecator.
64 65 66 67 68 |
# File 'lib/cmdx/deprecators.rb', line 64 def lookup(name) registry[name] || begin raise ArgumentError, "unknown deprecator: #{name.inspect}" end end |
#register(name, callable = nil, &block) { ... } ⇒ Deprecators
Registers a named deprecator, overwriting any existing entry.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cmdx/deprecators.rb', line 35 def register(name, callable = nil, &block) deprecator = callable || block if callable && block raise ArgumentError, "provide either a callable or a block, not both" elsif !deprecator.respond_to?(:call) raise ArgumentError, "deprecator must respond to #call" end registry[name.to_sym] = deprecator self end |
#resolve(spec) ⇒ #call?
Resolves a ‘deprecation` declaration’s value to a concrete callable. Accepts a Symbol (registry lookup) or any object responding to ‘#call`. `nil` resolves to `nil` so callers can short-circuit.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cmdx/deprecators.rb', line 77 def resolve(spec) case spec when NilClass nil when Symbol lookup(spec) else return spec if spec.respond_to?(:call) raise ArgumentError, "unknown deprecator: #{spec.inspect}" end end |
#size ⇒ Integer
96 97 98 |
# File 'lib/cmdx/deprecators.rb', line 96 def size registry.size end |