Class: Mixergy::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mixergy/config.rb

Overview

Configuration handler for Mixergy CLI and API clients. Loads and saves config as YAML, provides hash-like access.

Constant Summary collapse

DEFAULT_CONFIG_PATH =

Default path for the config file (~/.mixergy)

File.expand_path("~/.mixergy")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path = DEFAULT_CONFIG_PATH) ⇒ Config

Create a new Config object and load config from disk.

Parameters:

  • config_path (String) (defaults to: DEFAULT_CONFIG_PATH)

    Optional path to config file



22
23
24
25
# File 'lib/mixergy/config.rb', line 22

def initialize(config_path = DEFAULT_CONFIG_PATH)
  @filepath = config_path
  @data = load
end

Instance Attribute Details

#dataHash (readonly)

Returns The loaded config data.

Returns:

  • (Hash)

    The loaded config data



18
19
20
# File 'lib/mixergy/config.rb', line 18

def data
  @data
end

#filepathString (readonly)

Returns Path to the config file.

Returns:

  • (String)

    Path to the config file



16
17
18
# File 'lib/mixergy/config.rb', line 16

def filepath
  @filepath
end

Instance Method Details

#[](key) ⇒ Object?

Get a config value by key.

Parameters:

  • key (String, Symbol)

Returns:

  • (Object, nil)

    Value for the key



46
47
48
# File 'lib/mixergy/config.rb', line 46

def [](key)
  @data[key.to_s]
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Set a config value by key.

Parameters:

  • key (String, Symbol)
  • value (Object)


54
55
56
# File 'lib/mixergy/config.rb', line 54

def []=(key, value)
  @data[key.to_s] = value
end

#loadHash

Load config from disk.

Returns:

  • (Hash)

    The loaded config data



29
30
31
32
33
34
35
# File 'lib/mixergy/config.rb', line 29

def load
  if File.exist?(@filepath)
    YAML.load_file(@filepath) || {}
  else
    {}
  end
end

#savevoid

This method returns an undefined value.

Save current config data to disk.



39
40
41
# File 'lib/mixergy/config.rb', line 39

def save
  File.write(@filepath, YAML.dump(@data))
end