class Config

Overview

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

Defined in:

config.cr
format.cr
version.cr

Constant Summary

DATA = Hash(String, ConfigTypes).new

The configuration is stored within a constant.

VERSION = {{ (read_file("/home/runner/work/config.cr/config.cr/src/../VERSION")).chomp }}

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

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Constructor Detail

def self.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.


[View source]

Class Method Detail

def self.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.


[View source]
def self.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.


[View source]
def self.from(source : Path) #

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


[View source]
def self.from(source : String) #

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


[View source]

Instance Method Detail

def data #

Return the raw config data hash.

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

[View source]
def into(target : Hash(_, _)) #

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


[View source]
def into(target : IO) #

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


[View source]
def into(target : Path) #

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


[View source]
def into(target : String) #

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


[View source]
def 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.


[View source]
def 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.


[View source]

Macro Detail

macro method_missing(call) #

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


[View source]