Class: Loquacious::Undefined
- Inherits:
-
Object
- Object
- Loquacious::Undefined
- Defined in:
- lib/loquacious/undefined.rb
Overview
Represents an undefined configuration value. An undefined value is assigned to each configuration propery by default. Any method can be invoked on an undefined value, and a warning message will be printed to the IO stream (defaulting to $stderr).
The purpose of this class is to provide the user with a helpful message that the configuration values they are trying to use have not been setup correctly.
Class Attribute Summary collapse
-
.io ⇒ Object
Returns the value of attribute io.
Class Method Summary collapse
-
.warn(key) ⇒ Object
Write a warning message to the Undefined class IO stream.
Instance Method Summary collapse
-
#initialize(key) ⇒ Undefined
constructor
Creates a new undefined value returned from the lookup key in some configuration object.
-
#method_missing(method, *args, &block) ⇒ Object
For every method invoked on an undefined object, generate a warning message describing the undefined value and the method that was called.
-
#nil? ⇒ Boolean
An undefined value acts like a
nil
in that it has no value of its own. -
#respond_to_missing?(id, priv = false) ⇒ Boolean
We can respond to any method except :call.
Constructor Details
#initialize(key) ⇒ Undefined
Creates a new undefined value returned from the lookup key in some configuration object. The key is used to alert the user where the undefined value came from.
63 64 65 |
# File 'lib/loquacious/undefined.rb', line 63 def initialize( key ) @key = Kernel.Array(key) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
For every method invoked on an undefined object, generate a warning message describing the undefined value and the method that was called.
Returns a new undefined object with the most recent method included in the key name.
84 85 86 87 88 |
# File 'lib/loquacious/undefined.rb', line 84 def method_missing( method, *args, &block ) key = @key.dup << method.to_s Undefined.warn key return Undefined.new(key) end |
Class Attribute Details
.io ⇒ Object
Returns the value of attribute io.
37 38 39 |
# File 'lib/loquacious/undefined.rb', line 37 def io @io end |
Class Method Details
.warn(key) ⇒ Object
Write a warning message to the Undefined class IO stream. By default, this IO stream is set to the Ruby $stderr output.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/loquacious/undefined.rb', line 42 def warn( key ) if @first_time @io.puts <<-__ --------------------------------------------------------------------------- The Loquacious configuration system has detected that one or moe settings have an undefined value. An attempt is being made to reference sub-properties of these undefined settings. Messages will follow containing information about the undefined properties. --------------------------------------------------------------------------- __ @first_time = false end @io.puts "Access to undefined value #{key.first.inspect}: #{key.join('.')}" end |
Instance Method Details
#nil? ⇒ Boolean
An undefined value acts like a nil
in that it has no value of its own. This method always returns true
.
70 |
# File 'lib/loquacious/undefined.rb', line 70 def nil?() true; end |
#respond_to_missing?(id, priv = false) ⇒ Boolean
We can respond to any method except :call. The call method is reserved for Procs and lambdas, and it is used internally by loquacious for lazy evaluation of configuration parameters.
76 |
# File 'lib/loquacious/undefined.rb', line 76 def respond_to_missing?( id, priv = false ) id != :call; end |