class Movie::Config

Overview

Config provides path-based access to configuration values.

Example: config = Movie::Config.builder .set("name", "my-system") .set("remoting.host", "127.0.0.1") .set("remoting.port", 9000) .build

config.get_string("name") # => "my-system" config.get_int("remoting.port") # => 9000 config.get_config("remoting") # => Config subsection

Defined in:

movie/config.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.empty : Config #

Creates an empty config.


[View source]
def self.from_json(json_string : String) : Config #

Parses a JSON string into a Config.

Example: config = Movie::Config.from_json(%({ "name": "my-system", "remoting": { "host": "127.0.0.1", "port": 9000 } }))


[View source]
def self.from_yaml(yaml_string : String) : Config #

Parses a YAML string into a Config.

Example: config = Movie::Config.from_yaml(<<-YAML) name: my-system remoting: host: 127.0.0.1 port: 9000 YAML


[View source]
def self.load(path : String, default : Config) : Config #

Loads config from file with fallback for missing values. If file exists, loads it and uses default as fallback. If file doesn't exist, returns default.


[View source]
def self.load(path : String) : Config #

Loads config from file, detecting format by extension (.yml, .yaml, .json).


[View source]
def self.load_json(path : String, default : Config) : Config #

Loads a Config from a JSON file with fallback for missing values. If file exists, loads it and uses default as fallback. If file doesn't exist, returns default.


[View source]
def self.load_json(path : String) : Config #

Loads a Config from a JSON file.


[View source]
def self.load_yaml(path : String, default : Config) : Config #

Loads a Config from a YAML file with fallback for missing values. If file exists, loads it and uses default as fallback. If file doesn't exist, returns default.


[View source]
def self.load_yaml(path : String) : Config #

Loads a Config from a YAML file.


[View source]
def self.new(root : Hash(String, ConfigValue) = {} of String => ConfigValue) #

[View source]

Class Method Detail

def self.builder : ConfigBuilder #

Creates a new ConfigBuilder for programmatic config construction.


[View source]

Instance Method Detail

def [](path : String) : ConfigValue #

Subscript access - returns raw ConfigValue


[View source]
def []?(path : String) : ConfigValue #

Subscript access with nil for missing paths


[View source]
def empty? : Bool #

Returns true if config is empty.


[View source]
def get_array(path : String) : Array(ConfigValue) #

Returns the array value at the given path.


[View source]
def get_bool(path : String, default : Bool) : Bool #

Returns the boolean value at the given path, or default if not found.


[View source]
def get_bool(path : String) : Bool #

Returns the boolean value at the given path.


[View source]
def get_config(path : String, default : Config) : Config #

Returns a Config subsection at the given path, or empty Config if not found.


[View source]
def get_config(path : String) : Config #

Returns a Config subsection at the given path. Raises MissingConfigError if path doesn't exist. Raises WrongTypeConfigError if value is not a hash.


[View source]
def get_duration(path : String, default : Time::Span) : Time::Span #

Returns the duration value at the given path, or default if not found.


[View source]
def get_duration(path : String) : Time::Span #

Returns the duration value at the given path. Supports formats: "100ms", "5s", "2m", "1h", "1d" Also accepts numeric values as milliseconds.


[View source]
def get_float(path : String, default : Float64) : Float64 #

Returns the float value at the given path, or default if not found.


[View source]
def get_float(path : String) : Float64 #

Returns the float value at the given path.


[View source]
def get_int(path : String, default : Int32) : Int32 #

Returns the integer value at the given path, or default if not found.


[View source]
def get_int(path : String) : Int32 #

Returns the integer value at the given path.


[View source]
def get_int_array(path : String) : Array(Int32) #

Returns an array of integers at the given path.


[View source]
def get_long(path : String, default : Int64) : Int64 #

Returns the Int64 value at the given path, or default if not found.


[View source]
def get_long(path : String) : Int64 #

Returns the Int64 value at the given path.


[View source]
def get_string(path : String, default : String) : String #

Returns the string value at the given path, or default if not found.


[View source]
def get_string(path : String) : String #

Returns the string value at the given path. Raises MissingConfigError if path doesn't exist. Raises WrongTypeConfigError if value is not a string.


[View source]
def get_string_array(path : String, default : Array(String)) : Array(String) #

Returns an array of strings at the given path, or default if not found.


[View source]
def get_string_array(path : String) : Array(String) #

Returns an array of strings at the given path.


[View source]
def get_value(path : String) : ConfigValue #

Returns the raw value at the given path, or nil if not found.


[View source]
def get_value!(path : String) : ConfigValue #

Returns the raw value at the given path. Raises MissingConfigError if path doesn't exist.


[View source]
def has_path?(path : String) : Bool #

Returns true if the given path exists in the config.


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

Returns all top-level keys.


[View source]
def to_h : Hash(String, ConfigValue) #

Returns the root hash (for serialization).


[View source]
def with_env_overrides(prefix : String = "MOVIE") : Config #

Returns a new Config with environment variable overrides applied. Environment variables are mapped from MOVIE_* pattern: MOVIE_NAME -> name MOVIE_REMOTING_PORT -> remoting.port MOVIE_CLUSTER_SEED_NODES -> cluster.seed_nodes

Values are auto-converted:

  • "true"/"false" -> Bool
  • Numeric strings -> Int64 or Float64
  • Comma-separated -> Array(String)
  • Other -> String

Example:

With MOVIE_REMOTING_PORT=9000 MOVIE_DEBUG=true

config = base_config.with_env_overrides config.get_int("remoting.port") # => 9000 config.get_bool("debug") # => true


[View source]
def with_fallback(other : Config) : Config #

Returns a new Config with values from other merged in. Values from other override values in self.


[View source]
def with_override(other : Config) : Config #

Returns a new Config with values from other overriding self.


[View source]