toml-config 
handy use of crystal-toml
- https://github.com/manastech/crystal-toml
Usage
config (ex. config.toml
)
verbose = false
[redis]
host = "127.0.0.1"
port = 6379
cmds = ["GET", "SET"]
code
config = TOML::Config.parse_file("config.toml")
config["verbose"] # => false
config["xxx"]? # => nil
config["xxx"] # TOML::Config::NotFound
config["redis/host"] # => "127.0.0.1"
config["redis/host"].size # undefined method 'size'
# syntax sugars to fix type
config.bool("verbose") # => false
config.str("redis/host") # => "127.0.0.1"
config.str("redis/host").size # => 9
config.int("redis/port") # => 6379
config.int("redis/port").class # => Int32
config.["redis/port"].class # => Int64 (TOML default)
config.strs("redis/cmds") # => ["GET, "SET"]
config.str("xxx") # => TOML::Config::NotFound
config.str("xxx")? # => nil
config.as_hash("redis").keys # => ["host", "port", "cmds"]
custom class
class RedisConfig < TOML::Config
bool verbose
str "redis/host", host
as_hash "redis"
end
config = RedisConfig.parse_file("config.toml")
config.verbose? # => false
config.host # => "127.0.0.1"
config.redis.keys # => ["host", "port", "cmds"]
Examples
- https://github.com/maiha/dstat-redis.cr/blob/master/src/bin/main.cr
Installation
Add this to your application's shard.yml
:
dependencies:
toml-config:
github: maiha/toml-config.cr
version: 0.5.1
require "toml-config"
Breaking Changes
v0.5.0
#hash
renamed to#as_hash
to respectObject#hash
for old crystal
- v0.1.0 for crystal-0.18.x
- v0.2.0 for crystal-0.19.x, 0.20.4
- v0.3.1 for crystal-0.23.x, 0.24.x
- v0.3.2 for crystal-0.25.x, or higher
Contributing
- Fork it ( https://github.com/maiha/toml-config/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- maiha maiha - creator, maintainer