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
The configuration is stored within a constant.
The current version of the shard. This is read from the VERSION file by a precompilation macro.
Constructors
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.
Class methods
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.
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.
Create a new Config instance from a String specifing the path of the file to read.
Instance methods
Return the raw config data hash.
config.data # => {"verbose" => true}
pp tyoeof(config.data) # => Hash(String, Bool | Int32 | String)
Serialize the data from this Config instance into the target file specified by the Path.
Serialize the data from this Config instance into the target file specified by the String.
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.
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.
Macros
This macro writes the code for writing to or accessing the configuration hash.