Module: ZTK::Config
- Defined in:
- lib/ztk/config.rb
Overview
Configuration Module
Extend an existing class with this module to turn it into a singleton configuration class.
Given we have some code like:
class C
extend(ZTK::Config)
end
We can then do things like:
C.thing = "something"
or we can reference keys this way:
C[:thing] = "something"
Accessing the value is just as simple:
puts C.thing
We can also load configurations from disk. Assuming we have a file (i.e. config.rb) like:
"Hello World"
thing (1+1)
We can load it like so:
C.from_file("config.rb")
Then we can reference the configuration defined in the file as easily as:
puts C.
Class Method Summary (collapse)
-
+ (Object) extended(base)
Extend base class with this module.
Instance Method Summary (collapse)
-
- (Object) [](key)
Get the value of a configuration key.
-
- (Object) []=(key, value)
Set the value of a configuration key.
-
- (Object) config {|configuration| ... }
Yields the configuration OpenStruct object to a block.
-
- (Object) from_file(filename)
Loads a configuration from a file.
- - (Boolean) has_key?(key)
- - (Object) keys
- - (Object) merge(hash)
- - (Object) merge!(hash)
-
- (Object) method_missing(method_symbol, *method_args)
Handles method calls for our configuration keys.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method_symbol, *method_args)
Handles method calls for our configuration keys.
127 128 129 130 131 132 133 |
# File 'lib/ztk/config.rb', line 127 def method_missing(method_symbol, *method_args) if method_args.length > 0 _set(method_symbol, method_args.first) end _get(method_symbol) end |
Class Method Details
+ (Object) extended(base)
Extend base class with this module.
This will add the configuration attribute to the base class and create a new OpenStruct object assigning it to the configuration attribute.
54 55 56 57 58 59 60 |
# File 'lib/ztk/config.rb', line 54 def self.extended(base) class << base attr_accessor :configuration end base.configuration = OpenStruct.new end |
Instance Method Details
- (Object) [](key)
Get the value of a configuration key.
84 85 86 |
# File 'lib/ztk/config.rb', line 84 def [](key) _get(key) end |
- (Object) []=(key, value)
Set the value of a configuration key.
97 98 99 |
# File 'lib/ztk/config.rb', line 97 def []=(key, value) _set(key, value) end |
- (Object) config {|configuration| ... }
Yields the configuration OpenStruct object to a block.
73 74 75 |
# File 'lib/ztk/config.rb', line 73 def config(&block) block and block.call(self.configuration) end |
- (Object) from_file(filename)
Loads a configuration from a file.
64 65 66 |
# File 'lib/ztk/config.rb', line 64 def from_file(filename) self.instance_eval(IO.read(filename), filename) end |
- (Boolean) has_key?(key)
109 110 111 |
# File 'lib/ztk/config.rb', line 109 def has_key?(key) self.configuration.send(:table).has_key?(key) end |
- (Object) keys
103 104 105 |
# File 'lib/ztk/config.rb', line 103 def keys self.configuration.send(:table).keys end |
- (Object) merge(hash)
115 116 117 |
# File 'lib/ztk/config.rb', line 115 def merge(hash) self.configuration.send(:table).merge(hash) end |
- (Object) merge!(hash)
121 122 123 |
# File 'lib/ztk/config.rb', line 121 def merge!(hash) self.configuration.send(:table).merge!(hash) end |