Class: CemAcpt::Config

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cem_acpt/shared_objects.rb

Overview

Provides a thread-safe Config object. Once the config is loaded by calling the `load` method, the object is immutable and thread-safe as long as the internal hash is only accessed by the `#get` and `#has?` methods. If `#load` is called more than once, the object will raise an error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

current_log_config, #current_log_config, current_log_format, current_log_level, #current_log_level, included, #logger, new_log_config, #new_log_config, new_log_formatter, new_log_level, #new_log_level

Constructor Details

#initializeConfig

Returns a new instance of Config.



51
52
53
# File 'lib/cem_acpt/shared_objects.rb', line 51

def initialize
  @opts = {}
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



49
50
51
# File 'lib/cem_acpt/shared_objects.rb', line 49

def config_file
  @config_file
end

Instance Method Details

#get(dot_key) ⇒ Object

Returns the value of the dot-separated key. If the key is not found, returns nil.

Parameters:

  • dot_key (String)

    Dot-separated key

Returns:

  • (Object)

    Value of the key



59
60
61
# File 'lib/cem_acpt/shared_objects.rb', line 59

def get(dot_key)
  @opts.dot_dig(dot_key)
end

#has?(dot_key) ⇒ Boolean

Checks if the dot-separated key exists in the config.

Parameters:

  • dot_key (String)

    Dot-separated key

Returns:

  • (Boolean)

    True if the key exists, false otherwise



66
67
68
# File 'lib/cem_acpt/shared_objects.rb', line 66

def has?(dot_key)
  !!get(dot_key)
end

#load(opts: {}, config_file: '~/.cem_acpt.yaml') ⇒ Object

Loads the config from specified file path. Config files must be in YAML format. If 'opts' is specified, it will be merged with the config file. Values in 'opts' will override values in the config file.

Parameters:

  • opts (Hash) (defaults to: {})

    Options to be merged with the config file

  • config_file (String) (defaults to: '~/.cem_acpt.yaml')

    Path to the config file

Raises:



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cem_acpt/shared_objects.rb', line 75

def load(opts: {}, config_file: '~/.cem_acpt.yaml')
  raise ConfigImmutableError, 'Config is immutable, cannot load more than once' if frozen?
  raise ArgumentError, 'opts must be a Hash' unless opts.is_a?(Hash)
  raise ArgumentError, 'config_file must be a String' unless config_file.is_a?(String)

  @config_file = File.expand_path(config_file)
  @opts = load_opts_from_file(File.expand_path(config_file)).deep_merge(opts)
  @opts.format!
  @opts.freeze
  freeze
end

#to_hHash

Returns the config as a Hash.

Returns:

  • (Hash)

    Config as a Hash



89
90
91
# File 'lib/cem_acpt/shared_objects.rb', line 89

def to_h
  ::Marshal.load(::Marshal.dump(@opts))
end

#to_json(*args) ⇒ String

Returns the config as a JSON string.

Returns:

  • (String)

    Config as a JSON string



101
102
103
# File 'lib/cem_acpt/shared_objects.rb', line 101

def to_json(*args)
  to_h.to_json(*args)
end

#to_yamlString

Returns the config as a YAML string.

Returns:

  • (String)

    Config as a YAML string



95
96
97
# File 'lib/cem_acpt/shared_objects.rb', line 95

def to_yaml
  to_h.to_yaml
end