Class: Fleck::Configuration
- Inherits:
-
Object
- Object
- Fleck::Configuration
- Defined in:
- lib/fleck/configuration.rb
Overview
‘Fleck::Configuration` implements a set of methods useful for `Fleck` clients and consumers configuration.
Instance Attribute Summary collapse
-
#app_name ⇒ Object
Returns the value of attribute app_name.
-
#default_host ⇒ Object
Returns the value of attribute default_host.
-
#default_pass ⇒ Object
Returns the value of attribute default_pass.
-
#default_port ⇒ Object
Returns the value of attribute default_port.
-
#default_queue ⇒ Object
Returns the value of attribute default_queue.
-
#default_user ⇒ Object
Returns the value of attribute default_user.
-
#default_vhost ⇒ Object
Returns the value of attribute default_vhost.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#hosts ⇒ Object
Returns the value of attribute hosts.
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#loglevel ⇒ Object
Returns the value of attribute loglevel.
-
#progname ⇒ Object
Returns the value of attribute progname.
Instance Method Summary collapse
- #add_host(data) ⇒ Object
- #default_options ⇒ Object
- #formatter ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #logger ⇒ Object
- #logger=(new_logger) ⇒ Object
- #reset_logger ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fleck/configuration.rb', line 11 def initialize @logfile = $stdout @loglevel = ::Logger::INFO @progname = 'Fleck' @app_name = $PROGRAM_NAME @default_host = '127.0.0.1' @default_port = 5672 @default_user = nil @default_pass = nil @default_vhost = '/' @default_queue = 'default' @filters = %w[password secret token] @hosts = [] @credentials = {} end |
Instance Attribute Details
#app_name ⇒ Object
Returns the value of attribute app_name.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def app_name @app_name end |
#default_host ⇒ Object
Returns the value of attribute default_host.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def default_host @default_host end |
#default_pass ⇒ Object
Returns the value of attribute default_pass.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def default_pass @default_pass end |
#default_port ⇒ Object
Returns the value of attribute default_port.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def default_port @default_port end |
#default_queue ⇒ Object
Returns the value of attribute default_queue.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def default_queue @default_queue end |
#default_user ⇒ Object
Returns the value of attribute default_user.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def default_user @default_user end |
#default_vhost ⇒ Object
Returns the value of attribute default_vhost.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def default_vhost @default_vhost end |
#filters ⇒ Object
Returns the value of attribute filters.
8 9 10 |
# File 'lib/fleck/configuration.rb', line 8 def filters @filters end |
#hosts ⇒ Object
Returns the value of attribute hosts.
7 8 9 |
# File 'lib/fleck/configuration.rb', line 7 def hosts @hosts end |
#logfile ⇒ Object
Returns the value of attribute logfile.
7 8 9 |
# File 'lib/fleck/configuration.rb', line 7 def logfile @logfile end |
#loglevel ⇒ Object
Returns the value of attribute loglevel.
7 8 9 |
# File 'lib/fleck/configuration.rb', line 7 def loglevel @loglevel end |
#progname ⇒ Object
Returns the value of attribute progname.
7 8 9 |
# File 'lib/fleck/configuration.rb', line 7 def progname @progname end |
Instance Method Details
#add_host(data) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/fleck/configuration.rb', line 33 def add_host(data) case data when String then add_host_from_string(data) when Hash then add_host_from_hash(data) else raise ArgumentError, "Invalid host type #{data.inspect}: String or Hash expected" end end |
#default_options ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fleck/configuration.rb', line 42 def best = @hosts.min opts = {} host = best ? best.host : @default_host port = best ? best.port : @default_port credentials = @credentials["#{host}:#{port}"] || { user: @default_user, pass: @default_pass } opts[:host] = host opts[:port] = port opts[:user] = credentials[:user] || @default_user opts[:pass] = credentials[:pass] || @default_pass opts[:vhost] = @default_vhost opts[:queue] = @default_queue opts end |
#formatter ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fleck/configuration.rb', line 105 def formatter return @formatter if @formatter @formatter = proc do |severity, datetime, progname, msg| color = severity_color(severity) "[#{datetime.strftime('%F %T.%L')}]".color(:cyan) + "(#{$PID})".color(:blue) + "|#{severity}|".color(color) + (progname ? "<#{progname}>".color(:yellow) : '') + " #{msg}\n" end @formatter end |
#logger ⇒ Object
77 78 79 |
# File 'lib/fleck/configuration.rb', line 77 def logger @logger || reset_logger end |
#logger=(new_logger) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fleck/configuration.rb', line 81 def logger=(new_logger) @logger&.close if new_logger.nil? @logger = ::Logger.new(nil) else @logger = new_logger.clone @logger.formatter = formatter @logger.progname = @progname @logger.level = @loglevel end end |
#reset_logger ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fleck/configuration.rb', line 94 def reset_logger new_logger = ::Logger.new(@logfile) new_logger.formatter = formatter new_logger.progname = @progname new_logger.level = @loglevel @logger&.close @logger = new_logger @logger end |