Class: Mongo::Options::Redacted

Inherits:
BSON::Document
  • Object
show all
Defined in:
build/ruby-driver-v2.19/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 collapse

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



66
67
68
# File 'build/ruby-driver-v2.19/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



43
44
45
# File 'build/ruby-driver-v2.19/lib/mongo/options/redacted.rb', line 43

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



81
82
83
84
# File 'build/ruby-driver-v2.19/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



96
97
98
99
100
101
102
103
104
105
106
# File 'build/ruby-driver-v2.19/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



118
119
120
121
# File 'build/ruby-driver-v2.19/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



133
134
135
136
137
138
139
140
141
142
143
# File 'build/ruby-driver-v2.19/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



52
53
54
# File 'build/ruby-driver-v2.19/lib/mongo/options/redacted.rb', line 52

def to_s
  redacted_string(:to_s)
end