class

Config

Inherits Reference < Object

An instance of Config provides a Hash(String, String | Int | Bool) that is accessed via method calls. The method names are the keys of the hash.

For example:

config = Config.new

config.verbose = true
pp config.verbose # => true
if config.quiet?  # => false
  puts "be quiet"
else
  puts "don't be quiet" # => "don't be quiet"
end

The #data method will return the raw data hash.

pp config.data # => {"verbose" => true}

The Config class can be initialized from an IO, or a Path/String that points to a file. The file can be in either JSON or YAML format. The default format is JSON, but if the file cannot be read as JSON, but can be read as YAML, then the format will change to YAML.

config = Config.from("config.json") # {"verbose" : "true"}
pp config.verbose                   # => "true"

config = Config.from("config.yaml") # {verbose : true}
pp config.verbose                   # => true

config = Config.from("config.txt") # {also_verbose : true}
pp config.also_verbose             # => true

Constants

DATA = Hash(String, ConfigTypes).new

The configuration is stored within a constant.

VERSION = {{ (read_file("/tmp/tmp.PMcHaP/src/src/../VERSION")).chomp }}

The current version of the shard. This is read from the VERSION file by a precompilation macro.

Constructors

new(source : Hash(_, _))

Instantiate a new Config instance from a Hash. Keys will be turned into String, and values other than Bool and Int32 will be turned into String, as well.

Source

Class methods

from(source : Hash(_, _))

Create a new Config instance from a hash. Keys will be turned into String, and values, if they are a type other than Bool, Int32, or String, will be turned into String as well.

Source
from(source : IO, format : Format = Format::JSON)

Create a new Config instance from an IO object. The format of the data is assumed to be JSON unless otherwise specified. However, if the file can not be parsed as JSON data, the IO will be rewound, and the data will be parsed as YAML.

Source
from(source : Path)

Create a new Config instance from a Path specifing the file to read.

Source
from(source : String)

Create a new Config instance from a String specifing the path of the file to read.

Source

Instance methods

data

Return the raw config data hash.

config.data            # => {"verbose" => true}
pp tyoeof(config.data) # => Hash(String, Bool | Int32 | String)
Source
into(target : Hash(_, _))

Insert the data from the this Configs hash into the target hash.

Source
into(target : IO)

Serialize the data from this Config instance into the target IO object.

Source
into(target : Path)

Serialize the data from this Config instance into the target file specified by the Path.

Source
into(target : String)

Serialize the data from this Config instance into the target file specified by the String.

Source
serialization_format

The default serialization format is JSON. If the config file reads from a file which cannot be read as JSON, but can be read as YAML, then the default format will change to YAML. This property can also be set manually if one wants to specify the format to use to serialize the config data.

Source
serialization_format=(serialization_format : Format)

The default serialization format is JSON. If the config file reads from a file which cannot be read as JSON, but can be read as YAML, then the default format will change to YAML. This property can also be set manually if one wants to specify the format to use to serialize the config data.

Source

Macros

method_missing(call)

This macro writes the code for writing to or accessing the configuration hash.

Source

Nested types