class Totem::Config
- Totem::Config
- Reference
- Object
Overview
Totem::Config is the core configuration reader, parser and writer.
The config type are avaiable in:
- yaml/yml
- json
- env (depend on poncho)
Included Modules
Defined in:
totem/config.crConstant Summary
- 
        CONFIG_ENVS = ["development", "test", "production"] of ::String
- 
        CONFIG_NAME = "config"
- 
        KEY_DELIMITER = "."
Constructors
Class Method Summary
- 
        .from_file(file : String, paths : Array(String) | Nil = nil, environment : String | Nil = nil, key_delimiter : String | Nil = KEY_DELIMITER)
        
          Load configuration from a file 
- 
        .parse(raw : String, type : String, key_delimiter : String | Nil = KEY_DELIMITER)
        
          Parse configuration from a raw string. 
Instance Method Summary
- 
        #[](key : String) : Any
        
          Alias to #getmethod.
- 
        #[]=(key : String, value : T) forall T
        
          Alias to #setmethod.
- 
        #[]?(key : String) : Any | Nil
        
          Alias to #fetchmethod but returnNilif not exists.
- 
        #add_remote(*, provider : String | Nil = nil, **options)
        
          Add a remote provider 
- 
        #alias(alias_key : String, key : String)
        
          Register an aliase 
- 
        #automatic_env(prefix : String | Nil = nil)
        
          Enable and load ENV variables to Totem to search. 
- 
        #bind_env(key : String, real_key : String | Nil = nil)
        
          Bind a key to a ENV vairable 
- #clear!
- #config_env : String?
- #config_env=(config_env : Nil | String)
- #config_envs : Array(String)
- #config_envs=(config_envs : Array(String))
- #config_file : String | Nil
- #config_name : String
- #config_name=(config_name : String)
- #config_paths : Array(String)
- #config_paths=(config_paths : Array(String))
- #config_type : String?
- #config_type=(config_type : Nil | String)
- 
        #debugging=(value : Bool)
        
          Debugging switch 
- 
        #each(&)
        
          Returns an iterator over the #settingsentries.
- #env_prefix : String | Nil
- 
        #env_prefix=(prefix : String)
        
          Defines a ENVprefix
- 
        #fetch(key : String, default_value : Any | Any::Type) : Any
        
          Similar to #getmethod but returns given value if key not exists.
- 
        #fetch(key : String) : Any | Nil
        
          Similar to #getmethod but returnsNilif key not exists.
- 
        #flat_keys : Array(String)
        
          Returns all keys holding a value, regardless of where they are set. 
- 
        #get(key : String) : Any
        
          Gets any value by given key 
- 
        #has_key?(key : String) : Bool
        
          Checks to see if the key has been set in any of the data locations. 
- #key_delimiter : String
- #key_delimiter=(key_delimiter : String)
- 
        #keys
        
          Returns an iterator over the key of #settingsentries.
- 
        #load!
        
          Load configuration file from disk, searching in the defined paths. 
- 
        #load_file!(file : String)
        
          Load configuration file by given file name. 
- 
        #mapping(converter : T.class, key : String | Nil = nil) forall T
        
          Mapping JSON/YAML Serializable to Struct with key 
- 
        #parse(raw : String | IO, config_type = @config_type)
        
          Parse raw string with given config type to configuration 
- 
        #set(key : String, value : T) forall T
        
          Sets the value for the key in the override regiser. 
- 
        #set_default(key : String, value : T) forall T
        
          Sets the default value for given key 
- 
        #set_defaults(defaults : Hash(String, _))
        
          Sets the default values with Hashdata
- #set_value_from(source : Hash(String, Totem::Any), key : String, value : T) forall T
- 
        #settings : Hash(String, Any)
        
          Returns all settings of configuration. 
- 
        #store!
        
          Store the current configuration to a file. 
- 
        #store_file!(file : String, force : Bool = false)
        
          Store current configuration to a given file. 
- 
        #to_h
        
          Alias to #settings
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
Class Method Detail
Load configuration from a file
Totem::Config.from_file("config.yaml", ["/etc/totem", "~/.totem", "./"])Parse configuration from a raw string.
Instance Method Detail
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}"
endLoad 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}"
endMapping 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}"
endStore 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