module Scar::Config

Overview

This module aims to be a simple interface for storing basic key-value configuration like keybindings, window settings, ..

You can store everything that can be represented by YAML::Any.

Example usage:

# At toplevel
Scar::Config.set_defaults({
  int: 7331,
  str: "hello world",
  arr: [1, 3, 3, 7],
})

puts Config[:int] # => 7331

Scar::Config[:str] = "lol"
puts Config[:str] # => lol

Scar::Util.dir = "config-examle" # don't forget to specify a data directory for your files!
Scar::Config.save                # using default filename "config.yml"

Scar::Config.reset(:str)
puts Config[:str] # => hello world

# By loading the saved config from before, the `:str` entry is "lol" again
Scar::Config.load "config.yml"
puts Scar::Config[:str] # => lol

Extended Modules

Defined in:

scar/config.cr

Instance Method Summary

Macro Summary

Instance Method Detail

def [](key) : YAML::Type #

Returns the value for the given configuration entry

You can pass strings or symbols to this method. Returns the standard value if the configuration entry is not set.


[View source]
def []=(key, value) #

Sets a given configuration entry to a new value


[View source]
def dump #

Returns the yaml dump of the configuration data


[View source]
def load(fname : String = "config.yml") #

Loads configuration values from the given file (using Util#read_file)


[View source]
def load_defaults #

Resets all configuration entries to their standard value


[View source]
def reset(key) #

Resets the value of a configuration entry to it's standard (see Config#define_defaults)


[View source]
def save(fname : String = "config.yml") #

Saves the configuration to a file with given file name (using Util#write_file)


[View source]

Macro Detail

macro set_defaults(defaults) #

Defines standard values for configuration entries to fall back to.

Specify the defaults as a NamedTuple of format {entry name}: {value}.

Example:

Scar::Config.define_defaults({
  str: "hello world",
  arr: [1, 3, 3, 7],
})

[View source]