class Totem::Config

Overview

Totem::Config is the core configuration reader, parser and writer.

The config type are avaiable in:

Included Modules

Defined in:

totem/config.cr

Constant Summary

CONFIG_ENVS = ["development", "test", "production"] of ::String
CONFIG_NAME = "config"
KEY_DELIMITER = "."

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Totem::Utils::HashHelper

deep_search(source : Hash(String, String | Any), paths : Array(String)) : Any | Hash(String, Any) deep_search, has_value?(source : Hash(String, String | Any), paths : Array(String)) : Any | Nil has_value?

Instance methods inherited from module Totem::Utils::FileHelper

config_type(file : String) config_type

Instance methods inherited from module Totem::Utils::EnvHelper

env_key(key : String, env_prefix : String | Nil = nil) env_key

Instance methods inherited from class Reference

==(other : Totem::Any) ==

Instance methods inherited from class Object

===(other : Totem::Any) ===

Constructor Detail

def self.new(config_name : String = CONFIG_NAME, config_type : String | Nil = nil, config_paths : Array(String) = [] of String, config_env : String | Nil = nil, config_envs : Array(String) = CONFIG_ENVS, key_delimiter : String = KEY_DELIMITER) #

[View source]

Class Method Detail

def self.from_file(file : String, paths : Array(String) | Nil = nil, environment : String | Nil = nil, key_delimiter : String | Nil = KEY_DELIMITER) #

Load configuration from a file

Totem::Config.from_file("config.yaml", ["/etc/totem", "~/.totem", "./"])

[View source]
def self.parse(raw : String, type : String, key_delimiter : String | Nil = KEY_DELIMITER) #

Parse configuration from a raw string.


[View source]

Instance Method Detail

def [](key : String) : Any #

Alias to #get method.

totem["id"]
totem["user.name"]

[View source]
def []=(key : String, value : T) forall T #

Alias to #set method.

totem["id"] = 123
totem["user.name"] = "foobar"

[View source]
def []?(key : String) : Any | Nil #

Alias to #fetch method but return Nil if not exists.

totem["id"]?
totem["user.name"]?

[View source]
def add_remote(*, provider : String | Nil = nil, **options) #

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"

[View source]
def alias(alias_key : String, key : String) #

Register an aliase

totem.set("food", "apple")
totem.alias("f", "food")

totem.set("user.name", "foobar")
totem.alias("username", "user.name")

[View source]
def automatic_env(prefix : String | Nil = nil) #

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"

[View source]
def bind_env(key : String, real_key : String | Nil = nil) #

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")

[View source]
def clear! #

[View source]
def config_env : String? #

[View source]
def config_env=(config_env : Nil | String) #

[View source]
def config_envs : Array(String) #

[View source]
def config_envs=(config_envs : Array(String)) #

[View source]
def config_file : String | Nil #

[View source]
def config_name : String #

[View source]
def config_name=(config_name : String) #

[View source]
def config_paths : Array(String) #

[View source]
def config_paths=(config_paths : Array(String)) #

[View source]
def config_type : String? #

[View source]
def config_type=(config_type : Nil | String) #

[View source]
def debugging=(value : Bool) #

Debugging switch


[View source]
def each(&) #

Returns an iterator over the #settings entries.


[View source]
def env_prefix : String | Nil #

[View source]
def env_prefix=(prefix : String) #

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"

[View source]
def fetch(key : String, default_value : Any | Any::Type) : Any #

Similar to #get method but returns given value if key not exists.

Case-insensitive for a key.

totem.fetch("env", "development") => "development"

[View source]
def fetch(key : String) : Any | Nil #

Similar to #get method but returns Nil if key not exists.

Case-insensitive for a key.

totem.fetch("env")  => "development" or nil.

[View source]
def flat_keys : Array(String) #

Returns all keys holding a value, regardless of where they are set.

Nested keys are returns with a #key_delimiter (= ".") separator.


[View source]
def get(key : String) : Any #

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")

[View source]
def has_key?(key : String) : Bool #

Checks to see if the key has been set in any of the data locations.

Case-insensitive for a key.


[View source]
def key_delimiter : String #

[View source]
def key_delimiter=(key_delimiter : String) #

[View source]
def keys #

Returns an iterator over the key of #settings entries.


[View source]
def load! #

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

[View source]
def load_file!(file : String) #

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

[View source]
def mapping(converter : T.class, key : String | Nil = nil) forall T #

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"

[View source]
def parse(raw : String | IO, config_type = @config_type) #

Parse raw string with given config type to configuration

The config type are avaiable in:

  • yaml/yml
  • json
  • env

[View source]
def set(key : String, value : T) forall T #

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")

[View source]
def set_default(key : String, value : T) forall T #

Sets the default value for given key

totem.set_default("id", 123)
totem.set_default("user.name", "foobar")

[View source]
def set_defaults(defaults : Hash(String, _)) #

Sets the default values with Hash data

totem.set_defaults({
  "id"   => 123,
  "user" => {
    "name" => "foobar",
  },
})

[View source]
def set_value_from(source : Hash(String, Totem::Any), key : String, value : T) forall T #

[View source]
def settings : Hash(String, Any) #

Returns all settings of configuration.


[View source]
def store! #

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

[View source]
def store_file!(file : String, force : Bool = false) #

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

[View source]
def to_h #

Alias to #settings


[View source]