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.

Constant Summary

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#initializeConfig

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_fileObject (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)

Returns:

  • (TrueClass)

    If cem_acpt is running in debug mode

  • (FalseClass)

    If cem_acpt is not runnint in debug mode



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.

Parameters:

  • dot_key (String)

    Dot-separated key

Returns:

  • (Object)

    Value of the key



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.

Parameters:

  • dot_key (String)

    Dot-separated key

Returns:

  • (Boolean)

    True if the key exists, false otherwise



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.

Parameters:

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

    Options to be merged with the config file

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

    Path to the config file

Raises:



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.expand_path(config_file)
  @opts.deep_merge!(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



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.

Returns:

  • (String)

    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_yamlString

Returns the config as a YAML string.

Returns:

  • (String)

    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