Module: EventHub::Configuration
- Extended by:
- Configuration, Helper
- Included in:
- Configuration
- Defined in:
- lib/eventhub/configuration.rb
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
- #data ⇒ Object
-
#deep_merge!(target, data) ⇒ Object
Deep merging of hashes deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809.
- #default_configuration ⇒ Object
- #instance ⇒ 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
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Methods included from Helper
create_bunny_connection, get_name_from_class, now_stamp, stringify_keys
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *_args, &_block) ⇒ Object
92 93 94 95 |
# File 'lib/eventhub/configuration.rb', line 92 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
13 14 15 |
# File 'lib/eventhub/configuration.rb', line 13 def config_data @config_data end |
#config_file ⇒ Object (readonly)
name of configuration file
12 13 14 |
# File 'lib/eventhub/configuration.rb', line 12 def config_file @config_file end |
#detached ⇒ Object (readonly)
run processor run as a daemon
11 12 13 |
# File 'lib/eventhub/configuration.rb', line 11 def detached @detached end |
#environment ⇒ Object (readonly)
environment the processor is running
10 11 12 |
# File 'lib/eventhub/configuration.rb', line 10 def environment @environment end |
#name ⇒ Object
name of processor
9 10 11 |
# File 'lib/eventhub/configuration.rb', line 9 def name @name end |
Instance Method Details
#data ⇒ Object
132 133 134 135 136 |
# File 'lib/eventhub/configuration.rb', line 132 def data warn "[DEPRECATION] `data` is deprecated. Please use new configuration" \ " access method." stringify_keys(@config_data) end |
#deep_merge!(target, data) ⇒ Object
Deep merging of hashes deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809
84 85 86 87 88 89 90 |
# File 'lib/eventhub/configuration.rb', line 84 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/eventhub/configuration.rb', line 102 def default_configuration { server: { user: "guest", password: "guest", host: "localhost", vhost: "event_hub", port: 5672, tls: false, tls_cert: nil, tls_key: nil, tls_ca_certificates: [], verify_peer: false, show_bunny_logs: false }, processor: { heartbeat_cycle_in_s: 300, watchdog_cycle_in_s: 60, restart_in_s: 15, listener_queues: [@name] } } end |
#instance ⇒ Object
126 127 128 129 130 |
# File 'lib/eventhub/configuration.rb', line 126 def instance warn "[DEPRECATION] `instance` is deprecated. Please use new" \ " configuration access method." self end |
#load!(args = {}) ⇒ Object
load configuration from file
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/eventhub/configuration.rb', line 64 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
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 61 |
# File 'lib/eventhub/configuration.rb', line 35 def (argv = ARGV) @config_file = File.join(Dir.getwd, "config", "#{@name}.json") OptionParser.new { |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 }.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
26 27 28 29 30 31 32 |
# File 'lib/eventhub/configuration.rb', line 26 def reset @name = "undefined" @environment = "development" @detached = false @config_file = File.join(Dir.getwd, "config", "#{@name}.json") @config_data = {} end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
97 98 99 100 |
# File 'lib/eventhub/configuration.rb', line 97 def respond_to_missing?(name, include_private = false) return true if @config_data[name.to_sym] false end |