Class: CemAcpt::Config
- Inherits:
-
Object
- Object
- CemAcpt::Config
- 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
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
Instance Method Summary collapse
-
#get(dot_key) ⇒ Object
Returns the value of the dot-separated key.
-
#has?(dot_key) ⇒ Boolean
Checks if the dot-separated key exists in the config.
-
#initialize ⇒ Config
constructor
A new instance of Config.
-
#load(opts: {}, config_file: '~/.cem_acpt.yaml') ⇒ Object
Loads the config from specified file path.
-
#to_h ⇒ Hash
Returns the config as a Hash.
-
#to_json(*args) ⇒ String
Returns the config as a JSON string.
-
#to_yaml ⇒ String
Returns the config as a YAML string.
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
#initialize ⇒ Config
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_file ⇒ Object (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.
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.
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.
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.(config_file) @opts = load_opts_from_file(File.(config_file)).deep_merge(opts) @opts.format! @opts.freeze freeze end |
#to_h ⇒ Hash
Returns the 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.
101 102 103 |
# File 'lib/cem_acpt/shared_objects.rb', line 101 def to_json(*args) to_h.to_json(*args) end |
#to_yaml ⇒ String
Returns the 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 |