Totem::Config
Inherits Totem::Utils::HashHelper < Totem::Utils::FileHelper < Totem::Utils::EnvHelper < Reference < Object
Totem::Config is the core configuration reader, parser and writer.
The config type are avaiable in:
- yaml/yml
- json
- env (depend on poncho)
Constants
Constructors
Class methods
Instance methods
Alias to set method.
totem["id"] = 123
totem["user.name"] = "foobar"
Alias to fetch method but return Nil if not exists.
totem["id"]?
totem["user.name"]?
Add a remote provider
Two arguments must passed:
endpoint: the url of endporint.provider: The name of provider, ignore it if endpoint's scheme is same as provider.
Redis
You can get value access the key:
totem.add_remote(provider: "redis", endpoint: "redis://user:pass@localhost:6379/1")
# Or make it shorter
totem.add_remote(endpoint: "redis://user:pass@localhost:6379/1")
totem.get("user:id") # => "123"
You can get value from raw json access the path
totem.add_remote(endpoint: "redis://user:pass@localhost:6379/1", path: "config:production.json")
totem.get("user:id") # => "123"
Register an aliase
totem.set("food", "apple")
totem.alias("f", "food")
totem.set("user.name", "foobar")
totem.alias("username", "user.name")
Enable and load ENV variables to Totem to search.
It provide an argument to quick define the env prefix(env_prefix=)
ENV["TOTEM_ENV"] = "development"
totem.automatic_env("totem")
totem.get("env") # => "development"
Bind a key to a ENV vairable
If only a key is provided, it will use the ENV key matching the key, upcased.
It will append env prefix when env_prefix is seted and the key is not provided.
Case-sensitive for a key.
totem.bind_env("HOME")
totem.bind_env("root_path", "HOME")
totem.get("home")
totem.get("root_path")
Defines a ENV prefix
If defined with "totem", Totem will look for env variables that start with "TOTEM_"
It always upcase the prefix.
ENV["TOTEM_ENV"] = "development"
totem.env_prefix = "totem"
totem.bind_env("env")
totem.get("env") # => "development"
Similar to get method but returns given value if key not exists.
Case-insensitive for a key.
totem.fetch("env", "development") => "development"
Similar to get method but returns Nil if key not exists.
Case-insensitive for a key.
totem.fetch("env") => "development" or nil.
Returns all keys holding a value, regardless of where they are set.
Nested keys are returns with a #key_delimiter (= ".") separator.
Gets any value by given key
The behavior of returning the value associated with the first place from where it is set. following order:
- override
- env
- config file
- key/value store
- default
Case-insensitive for a key.
totem.get("id")
totem.get("user.name")
Checks to see if the key has been set in any of the data locations.
Case-insensitive for a key.
Load configuration file from disk, searching in the defined paths.
totem = Totem.new("config")
totem.config_paths << "/etc/totem" << "~/.totem"
begin
totem.load!
rescue e
puts "Fatal error config file: #{e.message}"
end
Load configuration file by given file name.
It will ignore the values of config_name, config_type and config_paths.
totem = Totem.new("config", "json")
totem.config_paths << "/etc/totem" << "~/.totem"
begin
totem.load_file!("~/config/development.yaml")
rescue e
puts "Fatal error config file: #{e.message}"
end
Mapping JSON/YAML Serializable to Struct with key
struct Clothes
include JSON::Serializable
# or
# include YAML::Serializable
property jacket : String
property trousers : String
property pants : Hash(String, String)
end
clothes = totem.mapping(Clothes, "clothing")
clothes.jacket # => "leather"
Parse raw string with given config type to configuration
The config type are avaiable in:
- yaml/yml
- json
- env
Sets the value for the key in the override regiser.
Will be used instead of values obtained via config file, env, default.
Case-insensitive for a key.
totem.set("id", 123)
totem.set("user.name", "foobar")
Sets the default value for given key
totem.set_default("id", 123)
totem.set_default("user.name", "foobar")
Sets the default values with Hash data
totem.set_defaults({
"id" => 123,
"user" => {
"name" => "foobar",
},
})
Store the current configuration to a file.
totem = Totem.new("config", "json")
totem.config_paths << "/etc/totem" << "~/.totem"
begin
totem.store!
rescue e
puts "Fatal error config file: #{e.message}"
end
Store current configuration to a given file.
It will ignore the values of config_name, config_type and config_paths.
totem = Totem.new("config", "json")
begin
totem.store_file!("~/config.yaml", force: true)
rescue e
puts "Fatal error config file: #{e.message}"
end