Class: MongoRequestLogger::Config
- Inherits:
-
Object
- Object
- MongoRequestLogger::Config
- Defined in:
- lib/mongo_request_logger/config.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#params ⇒ Object
Returns the value of attribute params.
-
#raw_config ⇒ Object
Returns the value of attribute raw_config.
Instance Method Summary collapse
- #[](param) ⇒ Object
- #[]=(param, value) ⇒ Object
- #capsize ⇒ Object
- #collection ⇒ Object
- #credentials ⇒ Object
- #database ⇒ Object
- #deep_merge(a, b) ⇒ Object
- #get(param) ⇒ Object
- #has?(param) ⇒ Boolean
- #hosts ⇒ Object
-
#initialize(config = nil) ⇒ Config
constructor
A new instance of Config.
- #load_config(config) ⇒ Object
- #load_file(file) ⇒ Object
- #moped_arguments ⇒ Object
- #moped_config ⇒ Object
- #moped_uri ⇒ Object
- #namespaced(namespace) ⇒ Object
- #namespaces ⇒ Object
- #to_hash ⇒ Object
- #with_indifferent_access ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ Config
Returns a new instance of Config.
15 16 17 18 19 |
# File 'lib/mongo_request_logger/config.rb', line 15 def initialize(config=nil) @params = {} @namespace = nil load_config(config.stringify_keys) unless config.nil? end |
Instance Attribute Details
#namespace ⇒ Object
Returns the value of attribute namespace.
13 14 15 |
# File 'lib/mongo_request_logger/config.rb', line 13 def namespace @namespace end |
#params ⇒ Object
Returns the value of attribute params.
12 13 14 |
# File 'lib/mongo_request_logger/config.rb', line 12 def params @params end |
#raw_config ⇒ Object
Returns the value of attribute raw_config.
11 12 13 |
# File 'lib/mongo_request_logger/config.rb', line 11 def raw_config @raw_config end |
Instance Method Details
#[](param) ⇒ Object
69 70 71 |
# File 'lib/mongo_request_logger/config.rb', line 69 def [](param) get(param) end |
#[]=(param, value) ⇒ Object
73 74 75 |
# File 'lib/mongo_request_logger/config.rb', line 73 def []=(param, value) @params[param.to_s] = value end |
#capsize ⇒ Object
121 122 123 |
# File 'lib/mongo_request_logger/config.rb', line 121 def capsize self['capsize'].to_i*1024*1024 end |
#collection ⇒ Object
117 118 119 |
# File 'lib/mongo_request_logger/config.rb', line 117 def collection self['collection'] || 'server_log' end |
#credentials ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/mongo_request_logger/config.rb', line 97 def credentials if moped_uri && moped_uri.auth_provided? [moped_uri.username, moped_uri.password] elsif self['username'] && self['password'] [self['username'], self['password']] else nil end end |
#database ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/mongo_request_logger/config.rb', line 107 def database if self['database'] self['database'] elsif moped_uri moped_uri.database else nil end end |
#deep_merge(a, b) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mongo_request_logger/config.rb', line 42 def deep_merge(a, b) if a.is_a? Hash and b.is_a? Hash r = a.stringify_keys b = b.stringify_keys for k, v in b r[k] = deep_merge(a[k], v) end r elsif b.is_a? Hash b.stringify_keys else b end end |
#get(param) ⇒ Object
65 66 67 |
# File 'lib/mongo_request_logger/config.rb', line 65 def get(param) @params[param.to_s] end |
#has?(param) ⇒ Boolean
61 62 63 |
# File 'lib/mongo_request_logger/config.rb', line 61 def has?(param) @params.key?(param.to_s) end |
#hosts ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mongo_request_logger/config.rb', line 85 def hosts if self['hosts'] return self['hosts'] elsif self.moped_uri return self.moped_uri.hosts else host = self['host'] || '127.0.0.1' port = self['port'] || 27017 return ["#{host}:#{port}"] end end |
#load_config(config) ⇒ Object
57 58 59 |
# File 'lib/mongo_request_logger/config.rb', line 57 def load_config(config) @params = deep_merge(@params, config) end |
#load_file(file) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/mongo_request_logger/config.rb', line 33 def load_file(file) begin hash = YAML::load(IO.read(file)) rescue => e hash = nil end load_config(hash) unless hash.nil? end |
#moped_arguments ⇒ Object
152 153 154 |
# File 'lib/mongo_request_logger/config.rb', line 152 def moped_arguments [hosts, moped_config] end |
#moped_config ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mongo_request_logger/config.rb', line 133 def moped_config args = {} if moped_uri args = moped_uri.moped_arguments[1] end args[:database] = self.database exclude = %w(host port hosts uri collection database capsize) @params.each do |key, value| next if exclude.include? key args[key.to_sym] = cast(value) end args end |
#moped_uri ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/mongo_request_logger/config.rb', line 125 def moped_uri if self['uri'] && defined? ::Moped::MongoUri ::Moped::MongoUri.new(self['uri']) else nil end end |
#namespaced(namespace) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/mongo_request_logger/config.rb', line 21 def namespaced(namespace) config = deep_merge(@params['common'], @params[namespace]) ac = self.class.new(config) ac.namespace = namespace ac end |
#namespaces ⇒ Object
29 30 31 |
# File 'lib/mongo_request_logger/config.rb', line 29 def namespaces @params.keys - %w(common) end |
#to_hash ⇒ Object
81 82 83 |
# File 'lib/mongo_request_logger/config.rb', line 81 def to_hash @params.symbolize_keys end |
#with_indifferent_access ⇒ Object
77 78 79 |
# File 'lib/mongo_request_logger/config.rb', line 77 def with_indifferent_access self end |