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.
Constant Summary
Constants included from Logging
Instance Attribute Summary collapse
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
Instance Method Summary collapse
-
#debug_mode? ⇒ TrueClass, FalseClass
Checks to see if cem_acpt is set to run in debug mode (log level is set to debug).
-
#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_config.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_format, current_log_level, #current_log_level, included, logger, #logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, new_log_level, #new_log_level, new_logger, #new_logger
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cem_acpt/shared_objects.rb', line 50 def initialize @opts = { thread_pool: { min_threads: [2, Concurrent.processor_count - 1].max, max_threads: [2, Concurrent.processor_count - 1].max, max_queue: [2, Concurrent.processor_count - 1].max * 10, fallback_policy: :caller_runs, idletime: 1200, auto_terminate: true, }, } end |
Instance Attribute Details
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
48 49 50 |
# File 'lib/cem_acpt/shared_objects.rb', line 48 def config_file @config_file end |
Instance Method Details
#debug_mode? ⇒ TrueClass, FalseClass
Checks to see if cem_acpt is set to run in debug mode (log level is set to debug)
81 82 83 |
# File 'lib/cem_acpt/shared_objects.rb', line 81 def debug_mode? @opts.dot_dig('log_level') == 'debug' end |
#get(dot_key) ⇒ Object
Returns the value of the dot-separated key. If the key is not found, returns nil.
67 68 69 |
# File 'lib/cem_acpt/shared_objects.rb', line 67 def get(dot_key) @opts.dot_dig(dot_key) end |
#has?(dot_key) ⇒ Boolean
Checks if the dot-separated key exists in the config.
74 75 76 |
# File 'lib/cem_acpt/shared_objects.rb', line 74 def has?(dot_key) !!get(dot_key) end |
#load(opts: {}, config_file: './cem_acpt_config.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.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cem_acpt/shared_objects.rb', line 90 def load(opts: {}, config_file: './cem_acpt_config.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.deep_merge!(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.
104 105 106 |
# File 'lib/cem_acpt/shared_objects.rb', line 104 def to_h ::Marshal.load(::Marshal.dump(@opts)) end |
#to_json(*args) ⇒ String
Returns the config as a JSON string.
116 117 118 |
# File 'lib/cem_acpt/shared_objects.rb', line 116 def to_json(*args) to_h.to_json(*args) end |
#to_yaml ⇒ String
Returns the config as a YAML string.
110 111 112 |
# File 'lib/cem_acpt/shared_objects.rb', line 110 def to_yaml to_h.to_yaml end |