123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Options::Redacted

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BSON::Document
Instance Chain:
self, BSON::Document
Inherits: BSON::Document
  • Object
Defined in: lib/mongo/options/redacted.rb

Overview

Class for wrapping options that could be sensitive. When printed, the sensitive values will be redacted.

Since:

  • 2.1.0

Constant Summary

Instance Method Summary

Instance Method Details

#has_key?(key) ⇒ true, false Also known as: #key?

Whether these options contain a given key.

Examples:

Determine if the options contain a given key.

options.has_key?(:name)

Parameters:

  • key (String, Symbol)

    The key to check for existence.

Returns:

  • (true, false)

    If the options contain the given key.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 66

def has_key?(key)
  super(convert_key(key))
end

#inspectString

Get a string representation of the options.

Returns:

  • (String)

    The string representation of the options.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 43

def inspect
  redacted_string(:inspect)
end

#key?(key)

Alias for #has_key?.

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 69

alias_method :key?, :has_key?

#redact(k, v, method) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 153

def redact(k, v, method)
  return STRING_REPLACEMENT if SENSITIVE_OPTIONS.include?(k.to_sym)
  v.send(method)
end

#redacted_string(method) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 147

def redacted_string(method)
  '{' + reduce([]) do |list, (k, v)|
    list << "#{k.send(method)}=>#{redact(k, v, method)}"
  end.join(', ') + '}'
end

#reject {|The| ... } ⇒ Options::Redacted

Returns a new options object consisting of pairs for which the block returns false.

Examples:

Get a new options object with pairs for which the block returns false.

new_options = options.reject { |k, v| k == 'database' }

Yield Parameters:

  • The (String, Object)

    key as a string and its value.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 81

def reject(&block)
  new_options = dup
  new_options.reject!(&block) || new_options
end

#reject! {|The| ... } ⇒ Options::Redacted?

Only keeps pairs for which the block returns false.

Examples:

Remove pairs from this object for which the block returns true.

options.reject! { |k, v| k == 'database' }

Yield Parameters:

  • The (String, Object)

    key as a string and its value.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 96

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

#select {|The| ... } ⇒ Options::Redacted

Returns a new options object consisting of pairs for which the block returns true.

Examples:

Get a new options object with pairs for which the block returns true.

ssl_options = options.select { |k, v| k =~ /ssl/ }

Yield Parameters:

  • The (String, Object)

    key as a string and its value.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 118

def select(&block)
  new_options = dup
  new_options.select!(&block) || new_options
end

#select! {|The| ... } ⇒ Options::Redacted?

Only keeps pairs for which the block returns true.

Examples:

Remove pairs from this object for which the block does not return true.

options.select! { |k, v| k =~ /ssl/ }

Yield Parameters:

  • The (String, Object)

    key as a string and its value.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 133

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

#to_sString

Get a string representation of the options.

Returns:

  • (String)

    The string representation of the options.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 52

def to_s
  redacted_string(:to_s)
end