123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Config::Validators::Client

Relationships & Source Files
Defined in: lib/mongoid/config/validators/client.rb

Overview

Validator for client specific configuration.

Constant Summary

Instance Method Summary

Instance Method Details

#both_uri_and_standard?(config) ⇒ true | false (private)

This method is for internal use only.

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

Examples:

Validate the options.

validator.no_database_or_uri?(config)

Parameters:

  • config (Hash)

    The configuration options.

Returns:

  • (true | false)

    If both standard and uri are defined.

[ GitHub ]

  
# File 'lib/mongoid/config/validators/client.rb', line 121

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) ⇒ true | false (private)

This method is for internal use only.

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

Examples:

Validate the options.

validator.no_database_or_uri?(config)

Parameters:

  • config (Hash)

    The configuration options.

Returns:

  • (true | false)

    If no database or uri is defined.

[ GitHub ]

  
# File 'lib/mongoid/config/validators/client.rb', line 91

def no_database_or_uri?(config)
  !config.has_key?(:database) && !config.has_key?(:uri)
end

#no_hosts_or_uri?(config) ⇒ true | false (private)

This method is for internal use only.

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

Examples:

Validate the options.

validator.no_hosts_or_uri?(config)

Parameters:

  • config (Hash)

    The configuration options.

Returns:

  • (true | false)

    If no hosts or uri is defined.

[ GitHub ]

  
# File 'lib/mongoid/config/validators/client.rb', line 106

def no_hosts_or_uri?(config)
  !config.has_key?(:hosts) && !config.has_key?(:uri)
end

#validate(clients)

Validate the client configuration.

Examples:

Validate the client config.

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

Parameters:

  • clients (Hash)

    The clients config.

[ GitHub ]

  
# File 'lib/mongoid/config/validators/client.rb', line 21

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

#validate_client_database(name, config) (private)

This method is for internal use only.

Validate that the client config has database.

Examples:

Validate the client has database.

validator.validate_client_database(:default, {})

Parameters:

  • name (String | Symbol)

    The config key.

  • config (Hash)

    The configuration.

[ GitHub ]

  
# File 'lib/mongoid/config/validators/client.rb', line 43

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) (private)

This method is for internal use only.

Validate that the client config has hosts.

Examples:

Validate the client has hosts.

validator.validate_client_hosts(:default, {})

Parameters:

  • name (String | Symbol)

    The config key.

  • config (Hash)

    The configuration.

[ GitHub ]

  
# File 'lib/mongoid/config/validators/client.rb', line 58

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) (private)

This method is for internal use only.

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

Examples:

Validate the uri and options.

validator.validate_client_uri(:default, {})

Parameters:

  • name (String | Symbol)

    The config key.

  • config (Hash)

    The configuration.

[ GitHub ]

  
# File 'lib/mongoid/config/validators/client.rb', line 74

def validate_client_uri(name, config)
  if both_uri_and_standard?(config)
    raise Errors::MixedClientConfiguration.new(name, config)
  end
end