Included Modules

Mongo::Support

Constants

SECONDARY_OK_COMMANDS

Commands that may be sent to replica-set secondaries, depending on read preference and tags. All other commands are always run on the primary.

Public Instance Methods

auth_key(username, password, nonce) click to toggle source

Generate an MD5 for authentication.

@param [String] username @param [String] password @param [String] nonce

@return [String] a key for db authentication.

# File lib/mongo/util/support.rb, line 50
def auth_key(username, password, nonce)
  Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(username, password)}")
end
format_order_clause(order) click to toggle source
# File lib/mongo/util/support.rb, line 91
def format_order_clause(order)
  case order
    when Hash, BSON::OrderedHash then hash_as_sort_parameters(order)
    when String, Symbol then string_as_sort_parameters(order)
    when Array then array_as_sort_parameters(order)
    else
      raise InvalidSortValueError, "Illegal sort clause, '#{order.class.name}'; must be of the form " +
        "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"
  end
end
hash_password(username, plaintext) click to toggle source

Return a hashed password for auth.

@param [String] username @param [String] plaintext

@return [String]

# File lib/mongo/util/support.rb, line 60
def hash_password(username, plaintext)
  Digest::MD5.hexdigest("#{username}:mongo:#{plaintext}")
end
is_i?(value) click to toggle source
# File lib/mongo/util/support.rb, line 102
def is_i?(value)
  return !!(value =~ /^\d+$/)
end
ok?(doc) click to toggle source

Determine if a database command has succeeded by checking the document response.

@param [Hash] doc

@return [Boolean] true if the 'ok' key is either 1 or true.

# File lib/mongo/util/support.rb, line 112
def ok?(doc)
  doc['ok'] == 1.0 || doc['ok'] == true
end
secondary_ok?(selector) click to toggle source
# File lib/mongo/util/support.rb, line 78
def secondary_ok?(selector)
  command = selector.keys.first.to_s.downcase

  if command == 'mapreduce'
    out = selector.select { |k, v| k.to_s.downcase == 'out' }.first.last
    # mongo looks at the first key in the out object, and doesn't
    # look at the value
    out.is_a?(Hash) && out.keys.first.to_s.downcase == 'inline' ? true : false
  else
    SECONDARY_OK_COMMANDS.member?(command)
  end
end
validate_db_name(db_name) click to toggle source
# File lib/mongo/util/support.rb, line 64
def validate_db_name(db_name)
  unless [String, Symbol].include?(db_name.class)
    raise TypeError, "db_name must be a string or symbol"
  end

  [" ", ".", "$", "/", "\\"].each do |invalid_char|
    if db_name.include? invalid_char
      raise Mongo::InvalidNSName, "database names cannot contain the character '#{invalid_char}'"
    end
  end
  raise Mongo::InvalidNSName, "database name cannot be the empty string" if db_name.empty?
  db_name
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.