class Config
- Config
- Reference
- Object
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.crformat.cr
version.cr
Constant Summary
-
DATA =
Hash(String, ConfigTypes).new -
The configuration is stored within a constant.
-
VERSION =
{{ (read_file("/srv/crystaldoc.info/github-wyhaines-config.cr-v0.1.0/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
Configinstance from aHash.
Class Method Summary
-
.from(source : Hash(_, _))
Create a new
Configinstance from a hash. -
.from(source : IO, format : Format = Format::JSON)
Create a new
Configinstance from anIOobject. -
.from(source : Path)
Create a new
Configinstance from aPathspecifing the file to read. -
.from(source : String)
Create a new
Configinstance from aStringspecifing the path of the file to read.
Instance Method Summary
-
#data
Return the raw config data hash.
-
#into(target : Hash(_, _))
Insert the data from the this
Configs hash into thetargethash. -
#into(target : IO)
Serialize the data from this
Configinstance into thetargetIO object. -
#into(target : Path)
Serialize the data from this
Configinstance into thetargetfile specified by thePath. -
#into(target : String)
Serialize the data from this
Configinstance into thetargetfile specified by theString. -
#serialization_format : Format
The default serialization format is
JSON. -
#serialization_format=(serialization_format : Format)
The default serialization format is
JSON.
Macro Summary
-
method_missing(call)
This macro writes the code for writing to or accessing the configuration hash.
Constructor Detail
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 Method Detail
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 Method Detail
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.
Macro Detail
This macro writes the code for writing to or accessing the configuration hash.