class Mongo::Options::Redacted
Class for wrapping options that could be sensitive. When printed, the sensitive values will be redacted.
@since 2.1.0
Constants
- SENSITIVE_OPTIONS
The options whose values will be redacted.
@since 2.1.0
- STRING_REPLACEMENT
The replacement string used in place of the value for sensitive keys.
@since 2.1.0
Public Instance Methods
Whether these options contain a given key.
@example Determine if the options contain a given key.
options.has_key?(:name)
@param [ String, Symbol
] key The key to check for existence.
@return [ true, false ] If the options contain the given key.
@since 2.1.0
# File lib/mongo/options/redacted.rb, line 63 def has_key?(key) super(convert_key(key)) end
Get a string representation of the options.
@return [ String ] The string representation of the options.
@since 2.1.0
# File lib/mongo/options/redacted.rb, line 40 def inspect redacted_string(:inspect) end
Returns a new options object consisting of pairs for which the block returns false.
@example Get a new options object with pairs for which the block returns false.
new_options = options.reject { |k, v| k == 'database' }
@yieldparam [ String, Object ] The key as a string and its value.
@return [ Options::Redacted
] A new options object.
@since 2.1.0
# File lib/mongo/options/redacted.rb, line 78 def reject(&block) new_options = dup new_options.reject!(&block) || new_options end
Only keeps pairs for which the block returns false.
@example Remove pairs from this object for which the block returns true.
options.reject! { |k, v| k == 'database' }
@yieldparam [ String, Object ] The key as a string and its value.
@return [ Options::Redacted
, nil ] This object or nil if no changes were made.
@since 2.1.0
# File lib/mongo/options/redacted.rb, line 93 def reject! if block_given? n_keys = keys.size keys.each do |key| delete(key) if yield(key, self[key]) end n_keys == keys.size ? nil : self else to_enum end end
Returns a new options object consisting of pairs for which the block returns true.
@example Get a new options object with pairs for which the block returns true.
ssl_options = options.select { |k, v| k =~ /ssl/ }
@yieldparam [ String, Object ] The key as a string and its value.
@return [ Options::Redacted
] A new options object.
@since 2.1.0
# File lib/mongo/options/redacted.rb, line 115 def select(&block) new_options = dup new_options.select!(&block) || new_options end
Only keeps pairs for which the block returns true.
@example Remove pairs from this object for which the block does not return true.
options.select! { |k, v| k =~ /ssl/ }
@yieldparam [ String, Object ] The key as a string and its value.
@return [ Options::Redacted
, nil ] This object or nil if no changes were made.
@since 2.1.0
# File lib/mongo/options/redacted.rb, line 130 def select! if block_given? n_keys = keys.size keys.each do |key| delete(key) unless yield(key, self[key]) end n_keys == keys.size ? nil : self else to_enum end end
Get a string representation of the options.
@return [ String ] The string representation of the options.
@since 2.1.0
# File lib/mongo/options/redacted.rb, line 49 def to_s redacted_string(:to_s) end
Private Instance Methods
# File lib/mongo/options/redacted.rb, line 150 def redact(k, v, method) return STRING_REPLACEMENT if SENSITIVE_OPTIONS.include?(k.to_sym) v.send(method) end
# File lib/mongo/options/redacted.rb, line 144 def redacted_string(method) '{' + reduce([]) do |list, (k, v)| list << "#{k.send(method)}=>#{redact(k, v, method)}" end.join(', ') + '}' end