Module: EventHub::Configuration
Overview
Configuraiton module
Instance Attribute Summary collapse
-
#config_data ⇒ Object
readonly
data from configuration file.
-
#config_file ⇒ Object
readonly
name of configuration file.
-
#detached ⇒ Object
readonly
run processor run as a daemon.
-
#environment ⇒ Object
readonly
environment the processor is running.
-
#name ⇒ Object
name of processor.
Instance Method Summary collapse
-
#deep_merge!(target, data) ⇒ Object
Deep merging of hashes deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809.
- #default_configuration ⇒ Object
-
#load!(args = {}) ⇒ Object
load configuration from file.
- #method_missing(name, *_args, &_block) ⇒ Object
-
#parse_options(argv = ARGV) ⇒ Object
parse options from argument list.
- #reset ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *_args, &_block) ⇒ Object
91 92 93 94 |
# File 'lib/eventhub/configuration.rb', line 91 def method_missing(name, *_args, &_block) @config_data[name.to_sym] || fail(NoMethodError, "unknown configuration [#{name}]", caller) end |
Instance Attribute Details
#config_data ⇒ Object (readonly)
data from configuration file
12 13 14 |
# File 'lib/eventhub/configuration.rb', line 12 def config_data @config_data end |
#config_file ⇒ Object (readonly)
name of configuration file
11 12 13 |
# File 'lib/eventhub/configuration.rb', line 11 def config_file @config_file end |
#detached ⇒ Object (readonly)
run processor run as a daemon
10 11 12 |
# File 'lib/eventhub/configuration.rb', line 10 def detached @detached end |
#environment ⇒ Object (readonly)
environment the processor is running
9 10 11 |
# File 'lib/eventhub/configuration.rb', line 9 def environment @environment end |
#name ⇒ Object
name of processor
8 9 10 |
# File 'lib/eventhub/configuration.rb', line 8 def name @name end |
Instance Method Details
#deep_merge!(target, data) ⇒ Object
Deep merging of hashes deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809
83 84 85 86 87 88 89 |
# File 'lib/eventhub/configuration.rb', line 83 def deep_merge!(target, data) return if data.nil? merger = proc do |_, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2, &merger) : v2 end target.merge! data, &merger end |
#default_configuration ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/eventhub/configuration.rb', line 96 def default_configuration { server: { user: 'guest', password: 'guest', host: 'localhost', vhost: 'event_hub', port: 5672, tls: false }, processor: { heartbeat_cycle_in_s: 300, watchdog_cycle_in_s: 15, listener_queues: [@name] } } end |
#load!(args = {}) ⇒ Object
load configuration from file
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/eventhub/configuration.rb', line 63 def load!(args = {}) # for better rspec testing @config_file = args[:config_file] if args[:config_file] @environment = args[:environment] if args[:environment] new_data = {} begin new_data = JSON.parse(File.read(@config_file), symbolize_names: true) rescue => e EventHub.logger.warn("Exception while loading configuration file: #{e}") EventHub.logger.info('Using default configuration values') end deep_merge!(@config_data, default_configuration) new_data = new_data[@environment.to_sym] deep_merge!(@config_data, new_data) end |
#parse_options(argv = ARGV) ⇒ Object
parse options from argument list
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/eventhub/configuration.rb', line 34 def (argv = ARGV) @config_file = File.join(Dir.getwd, 'config', "#{@name}.json") OptionParser.new do |opts| note = 'Define environment' opts.on('-e', '--environment ENVIRONMENT', note) do |environment| @environment = environment end opts.on('-d', '--detached', 'Run processor detached as a daemon') do @detached = true end note = 'Define configuration file' opts.on('-c', '--config CONFIG', note) do |config| @config_file = config end end.parse!(argv) true rescue OptionParser::InvalidOption => e EventHub.logger.warn("Argument Parsing: #{e}") false rescue OptionParser::MissingArgument => e EventHub.logger.warn("Argument Parsing: #{e}") false end |
#reset ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/eventhub/configuration.rb', line 25 def reset @name = 'undefined' @environment = 'development' @detached = false @config_file = File.join(Dir.getwd, 'config', "#{@name}.json") @config_data = {} end |