module Mongoid::Config::Validators::Client

Validator for client specific configuration.

Constants

STANDARD

Standard configuration options.

@since 3.0.0

Public Instance Methods

validate(clients) click to toggle source

Validate the client configuration.

@example Validate the client config.

Client.validate({ default: { hosts: [ "localhost:27017" ] }})

@param [ Hash ] clients The clients config.

@since 3.0.0

# File lib/mongoid/config/validators/client.rb, line 25
def validate(clients)
  unless clients.has_key?(:default)
    raise Errors::NoDefaultClient.new(clients.keys)
  end
  clients.each_pair do |name, config|
    validate_client_database(name, config)
    validate_client_hosts(name, config)
    validate_client_uri(name, config)
  end
end

Private Instance Methods

both_uri_and_standard?(config) click to toggle source

Return true if the configuration has both standard options and a uri defined.

@api private

@example Validate the options.

validator.no_database_or_uri?(config)

@param [ Hash ] config The configuration options.

@return [ true, false ] If both standard and uri are defined.

@since 3.0.0

# File lib/mongoid/config/validators/client.rb, line 137
def both_uri_and_standard?(config)
  config.has_key?(:uri) && config.keys.any? do |key|
    STANDARD.include?(key.to_sym)
  end
end
no_database_or_uri?(config) click to toggle source

Return true if the configuration has no database or uri option defined.

@api private

@example Validate the options.

validator.no_database_or_uri?(config)

@param [ Hash ] config The configuration options.

@return [ true, false ] If no database or uri is defined.

@since 3.0.0

# File lib/mongoid/config/validators/client.rb, line 103
def no_database_or_uri?(config)
  !config.has_key?(:database) && !config.has_key?(:uri)
end
no_hosts_or_uri?(config) click to toggle source

Return true if the configuration has no hosts or uri option defined.

@api private

@example Validate the options.

validator.no_hosts_or_uri?(config)

@param [ Hash ] config The configuration options.

@return [ true, false ] If no hosts or uri is defined.

@since 3.0.0

# File lib/mongoid/config/validators/client.rb, line 120
def no_hosts_or_uri?(config)
  !config.has_key?(:hosts) && !config.has_key?(:uri)
end
validate_client_database(name, config) click to toggle source

Validate that the client config has database.

@api private

@example Validate the client has database.

validator.validate_client_database(:default, {})

@param [ String, Symbol ] name The config key. @param [ Hash ] config The configuration.

@since 3.0.0

# File lib/mongoid/config/validators/client.rb, line 49
def validate_client_database(name, config)
  if no_database_or_uri?(config)
    raise Errors::NoClientDatabase.new(name, config)
  end
end
validate_client_hosts(name, config) click to toggle source

Validate that the client config has hosts.

@api private

@example Validate the client has hosts.

validator.validate_client_hosts(:default, {})

@param [ String, Symbol ] name The config key. @param [ Hash ] config The configuration.

@since 3.0.0

# File lib/mongoid/config/validators/client.rb, line 66
def validate_client_hosts(name, config)
  if no_hosts_or_uri?(config)
    raise Errors::NoClientHosts.new(name, config)
  end
end
validate_client_uri(name, config) click to toggle source

Validate that not both a uri and standard options are provided for a single client.

@api private

@example Validate the uri and options.

validator.validate_client_uri(:default, {})

@param [ String, Symbol ] name The config key. @param [ Hash ] config The configuration.

@since 3.0.0

# File lib/mongoid/config/validators/client.rb, line 84
def validate_client_uri(name, config)
  if both_uri_and_standard?(config)
    raise Errors::MixedClientConfiguration.new(name, config)
  end
end