Hash based application configuration helpers, because snowflake configurations are evil.
Use the Bogo::Config
directly to store configuration values:
require 'bogo-config'
config = Bogo::Config.new(
:bind => {
:address => '0.0.0.0',
:port => 8080
},
:run_as => 'www-data'
)
puts config.get(:bind, :address)
puts config[:run_as]
A path to a configuration file can also be provided. Lets define the file:
# /tmp/bogo-config.json
{
"bind": {
"address": "0.0.0.0",
"port": 8080
},
"run_as": "www-data"
}
and now we can load it:
require 'bogo-config'
config = Bogo::Config.new('/tmp/bogo-config.json')
puts config.get(:bind, :address)
puts config[:run_as]
Subclassing Bogo::Config
allows adding some structure
to the configuration file. The Bogo::Lazy
module is
used to provide the #attribute
method to describe
the configuration:
require 'bogo-config'
class MyConfig < Bogo::Config
attribute :bind, Smash, :coerce => proc{|v| v.to_smash}
attribute :run_as, String, :default => 'www-data'
end
config = MyConfig.new('/tmp/bogo-config.json')
puts config.get(:bind, :address)
puts config[:run_as]
Support for AttributeStruct
configuration files is builtin.
A helper class is provided as a visual nicety. The above
JSON example could also be written as:
# /tmp/bogo-config.rb
Configuration do
bind do
address '0.0.0.0'
port 8080
end
run_as 'www-data'
end
Currently the following serialization types are supported:
- JSON
- YAML
- XML
- AttributeStruct
Note on XML configuration files: The configuration must
be contained within a <configuration>
tag. The above
example would then look like this:
<configuration>
<bind>
<address>
0.0.0.0
</address>
<port>
8080
</port>
</bind>
<run_as>
www-data
</run_as>
</configuration>
The path provided on initialization can also be a directory. The contents of the directory will be read in string sorted order and deep merged. Files can be a mix of supported types.
- Repository: https://github.com/spox/bogo-config