toml-config Build Status

handy use of crystal-toml

Usage

config (ex. config.toml )

verbose = true

[redis]
host = "127.0.0.1"
port = 6379
cmds = ["GET", "SET"]

code

config = TOML::Config.parse_file("config.toml")

Basic API

Basic API provides hash like access and returns a value as Union Types like Int64 | String | ....

config["verbose"]          # => true
config["xxx"]?             # => nil
config["xxx"]              # TOML::Config::NotFound
config["redis/host"]       # => "127.0.0.1"
config["redis/host"].size  # undefined method 'size'

Typed API

If you know the type, the Typed API is useful as it will automatically convert the type.

config.bool("verbose")         # => true
config.bool?("verbose")        # => nil
config.str("redis/host")       # => "127.0.0.1"
config.str("redis/host").size  # => 9
config.i32("redis/port")       # => 6379
config.i32("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"]

Macro API

In subclass of TOML::Config, type names are provided as class level macro. We can use type as DSL to define instance methods.

class RedisConfig < TOML::Config
  bool verbose
  str  "redis/host", host
  i32  "redis/port"
  i32  "redis/db", db
  strs "redis/cmds", cmds

  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"]
config.cmds       # => ["GET", "SET"]

Examples

Installation

Add this to your application's shard.yml:

dependencies:
  toml-config:
    github: maiha/toml-config.cr
    version: 0.7.0
require "toml-config"

Breaking Changes

for old crystal

Contributing

  1. Fork it ( https://github.com/maiha/toml-config/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors