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
-
#create_accessor_for(key) ⇒ Object
Use instance_eval/class_eval because they’re actually more efficient than define_method{} stackoverflow.com/questions/185947/ruby-definemethod-vs-def bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/.
- #create_accessors! ⇒ Object
- #deep_merge(a, b) ⇒ Object
- #get(param) ⇒ Object
- #has?(param) ⇒ Boolean
-
#initialize(config = nil) ⇒ Config
constructor
A new instance of Config.
- #load_config(config) ⇒ Object
- #load_file(file) ⇒ Object
- #namespaced(namespace) ⇒ Object
- #namespaces ⇒ Object
- #with_indifferent_access ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ Config
Returns a new instance of Config.
9 10 11 12 13 14 |
# File 'lib/mongo_request_logger/config.rb', line 9 def initialize(config=nil) @raw_config = {} @params = {} @namespace = nil load_config(config) unless config.nil? end |
Instance Attribute Details
#namespace ⇒ Object
Returns the value of attribute namespace.
7 8 9 |
# File 'lib/mongo_request_logger/config.rb', line 7 def namespace @namespace end |
#params ⇒ Object
Returns the value of attribute params.
6 7 8 |
# File 'lib/mongo_request_logger/config.rb', line 6 def params @params end |
#raw_config ⇒ Object
Returns the value of attribute raw_config.
5 6 7 |
# File 'lib/mongo_request_logger/config.rb', line 5 def raw_config @raw_config end |
Instance Method Details
#[](param) ⇒ Object
73 74 75 |
# File 'lib/mongo_request_logger/config.rb', line 73 def [](param) get(param) end |
#[]=(param, value) ⇒ Object
77 78 79 |
# File 'lib/mongo_request_logger/config.rb', line 77 def []=(param, value) @params[param.to_s] = value end |
#create_accessor_for(key) ⇒ Object
Use instance_eval/class_eval because they’re actually more efficient than define_method{} stackoverflow.com/questions/185947/ruby-definemethod-vs-def bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mongo_request_logger/config.rb', line 94 def create_accessor_for(key) key = key.to_s return unless key =~ /^[\w_]+$/ # could have "some-setting:" which blows up eval self.class.class_eval " def \#{key}\n return @params[\"\#{key}\"]\n end\n\n def \#{key}= value\n @params[\"\#{key}\"] = value\n end\n EndEval\nend\n" |
#create_accessors! ⇒ Object
85 86 87 88 89 |
# File 'lib/mongo_request_logger/config.rb', line 85 def create_accessors! @params.each do |key,val| create_accessor_for(key) end end |
#deep_merge(a, b) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mongo_request_logger/config.rb', line 37 def deep_merge(a, b) if a.is_a? Hash and b.is_a? Hash r = a.dup for k, v in b r[k] = deep_merge(a[k], v) end r elsif b.is_a? Hash b.dup else b end end |
#get(param) ⇒ Object
69 70 71 |
# File 'lib/mongo_request_logger/config.rb', line 69 def get(param) @params[param.to_s] end |
#has?(param) ⇒ Boolean
65 66 67 |
# File 'lib/mongo_request_logger/config.rb', line 65 def has?(param) @params.key?(param.to_s) end |
#load_config(config) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mongo_request_logger/config.rb', line 51 def load_config(config) @raw_config = deep_merge(@raw_config, config) @params = {} @raw_config.each { |k, v| if v.is_a? Hash @params[k] = self.class.new(v) else @params[k] = v end } create_accessors! end |
#load_file(file) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/mongo_request_logger/config.rb', line 28 def load_file(file) begin hash = YAML::load(IO.read(file)) rescue => e hash = nil end load_config(hash) unless hash.nil? end |
#namespaced(namespace) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/mongo_request_logger/config.rb', line 16 def namespaced(namespace) config = deep_merge(@raw_config['common'], @raw_config[namespace]) ac = self.class.new(config) ac.namespace = namespace ac end |
#namespaces ⇒ Object
24 25 26 |
# File 'lib/mongo_request_logger/config.rb', line 24 def namespaces @raw_config.keys - %w(common) end |
#with_indifferent_access ⇒ Object
81 82 83 |
# File 'lib/mongo_request_logger/config.rb', line 81 def with_indifferent_access self end |