Created
December 27, 2009 20:46
-
-
Save peregrinator/264395 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original | |
def add_metadata(options={}) | |
options.each_pair do |key, value| | |
unless [:messages, :request_time, :ip, :runtime].include?(key.to_sym) | |
next if value.is_a?(Hash) | |
options.delete(key) | |
info("[MongoLogger : metadata] '#{key}' => '#{value}'") | |
@mongo_record[key] = value | |
else | |
raise ArgumentError, ":#{key} is a reserved key for the mongo logger. Please choose a different key" | |
end | |
end | |
end | |
# Refactor and add nested values | |
def add_metadata(options={}) | |
if options.present? | |
raise ArgumentError, "#{RESERVED_MONGO_LOGGER_KEYS.join(', ')} are reserved keys for the mongo logger. Please choose a different key" if options.any?{|key, value| RESERVED_MONGO_LOGGER_KEYS.include?(key.to_sym)} | |
@mongo_record.recursive_merge!(options) | |
end | |
end | |
class Hash | |
def recursive_merge!(other) | |
other.each do |key, value| | |
myval = self[key] | |
if value.is_a?(Hash) && myval.is_a?(Hash) | |
myval.recursive_merge!(value) | |
else | |
self[key] = value | |
end | |
end | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment