Class: Mongo::Options::Redacted

Inherits:
BSON::Document
  • Object
show all
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

SENSITIVE_OPTIONS =

The options whose values will be redacted.

Since:

  • 2.1.0

[ :password,
:pwd ].freeze
STRING_REPLACEMENT =

The replacement string used in place of the value for sensitive keys.

Since:

  • 2.1.0

'<REDACTED>'.freeze

Instance Method Summary collapse

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



63
64
65
# File 'lib/mongo/options/redacted.rb', line 63

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



40
41
42
# File 'lib/mongo/options/redacted.rb', line 40

def inspect
  redacted_string(:inspect)
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



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

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



93
94
95
96
97
98
99
100
101
102
103
# 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

#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



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

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



130
131
132
133
134
135
136
137
138
139
140
# 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

#to_sString

Get a string representation of the options.

Returns:

  • (String)

    The string representation of the options.

Since:

  • 2.1.0



49
50
51
# File 'lib/mongo/options/redacted.rb', line 49

def to_s
  redacted_string(:to_s)
end