Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Validate session config options.
Browse files Browse the repository at this point in the history
[ fix #179 ]
  • Loading branch information
durran committed Jul 9, 2013
1 parent f1b94fd commit 2d3a069
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/moped.rb
Expand Up @@ -4,6 +4,7 @@
require "monitor"
require "timeout"
require "bson"
require "optionable"
require "moped/errors"
require "moped/indexes"
require "moped/loggable"
Expand Down
74 changes: 59 additions & 15 deletions lib/moped/session.rb
Expand Up @@ -31,6 +31,7 @@ module Moped
#
# @since 1.0.0
class Session
include Optionable

# @!attribute cluster
# @return [ Cluster ] The cluster of nodes.
Expand Down Expand Up @@ -168,6 +169,62 @@ def logout
current_database.logout
end

# Setup validation of allowed write concern options.
#
# @since 2.0.0
option(:write).allow(w: Optionable.any(Integer))
option(:write).allow(w: Optionable.any(String))
option(:write).allow(j: true)
option(:write).allow(j: false)
option(:write).allow(fsync: true)
option(:write).allow(fsync: false)

# Setup validation of allowed read preference options.
#
# @since 2.0.0
option(:read).allow(
:nearest,
:primary,
:primary_preferred,
:secondary,
:secondary_preferred
)

# Setup validation of allowed database options. (Any string or symbol)
#
# @since 2.0.0
option(:database).allow(Optionable.any(String), Optionable.any(Symbol))

# Setup validation of allowed max retry options. (Any integer)
#
# @since 2.0.0
option(:max_retries).allow(Optionable.any(Integer))

# Setup validation of allowed pool size options. (Any integer)
#
# @since 2.0.0
option(:pool_size).allow(Optionable.any(Integer))

# Setup validation of allowed retry interval options. (Any numeric)
#
# @since 2.0.0
option(:retry_interval).allow(Optionable.any(Numeric))

# Setup validation of allowed reap interval options. (Any numeric)
#
# @since 2.0.0
option(:reap_interval).allow(Optionable.any(Numeric))

# Setup validation of allowed ssl options. (Any boolean)
#
# @since 2.0.0
option(:ssl).allow(true, false)

# Setup validation of allowed timeout options. (Any numeric)
#
# @since 2.0.0
option(:timeout).allow(Optionable.any(Numeric))

# Initialize a new database session.
#
# @example Initialize a new session.
Expand All @@ -176,24 +233,11 @@ def logout
# @param [ Array ] seeds An array of host:port pairs.
# @param [ Hash ] options The options for the session.
#
# @option options [ Hash ] :write Ensure writes are persisted with the
# specified safety level e.g., "fsync: true", or "w: 2, wtimeout: 5", "w:
# 0".
# @option options [ Symbol, String ] :database The database to use.
# @option options [ Boolean ] :ssl Connect using SSL.
# @option options [ Integer ] :max_retries The maximum number of attempts
# to retry an operation. (30)
# @option options [ Integer ] :retry_interval The time in seconds to retry
# connections to a secondary or primary after a failure. (1)
# @option options [ Integer ] :timeout The time in seconds to wait for an
# operation to timeout. (5)
# @option options [ Float ] :reap_interval The interval, in seconds, in
# which to reap connections on dead threads.
# @option options [ Integer ] :pool_size The size of the connection pool
# for each node. This defaults to 5.
# @see Above options validations for allowed values in the options hash.
#
# @since 1.0.0
def initialize(seeds, options = {})
validate_strict(options)
@options = options
@cluster = Cluster.new(seeds, options)
end
Expand Down
1 change: 1 addition & 0 deletions moped.gemspec
Expand Up @@ -16,4 +16,5 @@ Gem::Specification.new do |s|
s.files = Dir.glob("lib/**/*") + %w(CHANGELOG.md LICENSE README.md)
s.require_path = "lib"
s.add_dependency("bson", ["~> 2.0.0.rc1"])
s.add_dependency("optionable", ["~> 0.1.1"])
end

0 comments on commit 2d3a069

Please sign in to comment.