Class: Rodbot::Config
- Inherits:
-
Object
- Object
- Rodbot::Config
- Defined in:
- lib/rodbot/config.rb
Overview
Simple yet flexible configuration module
The configuration is defined in Ruby as follows:
name 'Bot'
country 'Sweden'
country nil
log do
level 3
end
plugin :matrix do
version 1
ssl true
end
plugin :slack do
version 2
end
Within Rodbot, you should use the Rodbot.config
shortcut to access the configuration.
source = Pathname('config/rodbot.rb')
rc = Rodbot::Config.new(source)
rc.config(:name) # => 'Bot'
rc.config(:country) # => nil
rc.config(:undefined) # => nil
rc.config(:log) # => { level: 3 }
rc.config(:plugin, :matrix, :version)
# => 1
rc.config(:plugin, :matrix)
# => { version: 1, ssl: true }
rc.config(:plugin)
# => { matrix: { version: 1, ssl: true }, slack: { version: 2 } }
rc.config
# => { name: 'Bot', country: nil, plugin: { matrix: { version: 1, ssl: true }, slack: { version: 2 } } }
There are two types configuration items:
-
Object values without block like name ‘Bot’:
The config key:name
gets the object ‘Bot’ assigned. Subsequent assignments with the same config key overwrite previous assignments. -
Unspecified value with a block like log do:
The config key:log
is assigned a hash defined by the block. Subsequent assignments with the same config key are merged into the hash. -
Object values with a block like plugin :matrix do:
The config key:plugin
is assigned an empty hash which is then populated with the object ‘:matrix` (usually a Symbol) as key and the subtree defined by the block. Subsequent assignments with the same config key add more keys to this hash.
Please note: You can force a config key to always be treated as if it had a block (type 3) by adding it to the KEYS_WITH_IMPLICIT_BLOCK
array.
Defaults set by the DEFAULTS
constant are read first and therefore may be overwritten or extend as mentioned above.
Defined Under Namespace
Classes: Reader
Constant Summary collapse
- KEYS_WITH_IMPLICIT_BLOCK =
Keys which are always treated as if they had a block even if they don’t
%i(plugin).freeze
- DEFAULTS =
Default configuration
<<~END name 'Rodbot' port 7200 time_zone ENV['TZ'] || 'Etc/UTC' db 'hash' app do threads Rodbot.env.development? ? (1..1) : (2..4) end log do to STDOUT level Rodbot.env.development? ? Logger::INFO : Logger::ERROR end END
Instance Method Summary collapse
-
#config(*keys) ⇒ Object
Get config values and subtrees.
-
#initialize(source, defaults: true) ⇒ self
constructor
Read configuration.
Constructor Details
#initialize(source, defaults: true) ⇒ self
Read configuration
85 86 87 88 89 90 |
# File 'lib/rodbot/config.rb', line 85 def initialize(source, defaults: true) @config = Reader.new.eval_strings( (DEFAULTS if defaults), (source.respond_to?(:read) ? (source.read if source.readable?) : source) ).to_h end |
Instance Method Details
#config(*keys) ⇒ Object
Use the Rodbot.config
shortcut to access this method!
Get config values and subtrees
98 99 100 101 102 103 104 105 106 |
# File 'lib/rodbot/config.rb', line 98 def config(*keys) return @config if keys.none? value = @config.dig(*keys) if value.instance_of?(Array) && value.count == 1 value.first else value end end |