Class: Moped::Session

Inherits:
Object show all
Defined in:
lib/moped/session.rb,
lib/moped/session/context.rb

Overview

A session in moped is root for all interactions with a MongoDB server or replica set.

It can talk to a single default database, or dynamically speak to multiple databases.

Examples:

Single database (console-style)

session = Moped::Session.new(["127.0.0.1:27017"])
session.use :moped
session[:users].find.one # => { name: "John" }

Multiple databases

session = Moped::Session.new(["127.0.0.1:27017"])

session.with(database: :admin) do |admin|
  admin.command ismaster: 1
end

session.with(database: :moped) do |moped|
  moped[:users].find.one # => { name: "John" }
end

Authentication


session = Moped::Session.new %w[127.0.0.1:27017],
session.with(database: "admin").("admin", "s3cr3t")

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seeds, options = {}) ⇒ Session

Initialize a new database session.

Examples:

Initialize a new session.

Session.new([ "localhost:27017" ])

Parameters:

  • seeds (Array)

    an of host:port pairs

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :safe (Boolean) — default: false

    Ensure writes are persisted.

  • :safe (Hash)

    Ensure writes are persisted with the specified safety level e.g., “fsync: true”, or “w: 2, wtimeout: 5”.

  • :database (Symbol, String)

    The database to use.

  • :consistency (:strong, :eventual) — default: :eventual

    .

Since:

  • 1.0.0



141
142
143
144
145
146
# File 'lib/moped/session.rb', line 141

def initialize(seeds, options = {})
  @cluster = Cluster.new(seeds, {})
  @context = Context.new(self)
  @options = options
  @options[:consistency] ||= :eventual
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



35
36
37
# File 'lib/moped/session.rb', line 35

def cluster
  @cluster
end

#cluster The session cluster.(Thesessioncluster.) ⇒ Object (readonly)



35
# File 'lib/moped/session.rb', line 35

attr_reader :cluster, :context, :options

#contextObject (readonly)

Returns the value of attribute context.



35
36
37
# File 'lib/moped/session.rb', line 35

def context
  @context
end

#context The session context.(Thesessioncontext.) ⇒ Object (readonly)



35
# File 'lib/moped/session.rb', line 35

attr_reader :cluster, :context, :options

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/moped/session.rb', line 35

def options
  @options
end

#options The session options.(Thesessionoptions.) ⇒ Object (readonly)



35
# File 'lib/moped/session.rb', line 35

attr_reader :cluster, :context, :options

Instance Method Details

#[](name) ⇒ Collection

Return collection from the current database.

Parameters:

Returns:

Since:

  • 1.0.0



44
45
46
# File 'lib/moped/session.rb', line 44

def [](name)
  current_database[name]
end

#collection_namesArray<String>

Return non system collection name from the current database.

Returns:

Since:

  • 1.0.0



55
56
57
# File 'lib/moped/session.rb', line 55

def collection_names
  current_database.collection_names
end

#collectionsArray<Collection>

Return non system collection name from the current database.

Returns:

Since:

  • 1.0.0



66
67
68
# File 'lib/moped/session.rb', line 66

def collections
  current_database.collections
end

#command(op) ⇒ Hash

Run command on the current database.

Parameters:

  • command (Hash)

    The command to run.

Returns:

  • (Hash)

    the result of the command.

Since:

  • 1.0.0



77
78
79
# File 'lib/moped/session.rb', line 77

def command(op)
  current_database.command(op)
end

#consistency:strong, :eventual

Get the session’s consistency.

Examples:

Get the session consistency.

session.consistency

Returns:

  • (:strong, :eventual)

    The session’s consistency.

Since:

  • 1.0.0



122
123
124
# File 'lib/moped/session.rb', line 122

def consistency
  options[:consistency]
end

#dropnil

Drop the current database.

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



88
89
90
# File 'lib/moped/session.rb', line 88

def drop
  current_database.drop
end

#login(username, password) ⇒ Object

Log in with username and password on the current database.

Parameters:

  • username (String)

    The username.

  • password (String)

    The password.

Since:

  • 1.0.0



99
100
101
# File 'lib/moped/session.rb', line 99

def (username, password)
  current_database.(username, password)
end

#logoutObject

Log out from the current database.

Since:

  • 1.0.0



110
111
112
# File 'lib/moped/session.rb', line 110

def logout
  current_database.logout
end

#new(options = {}) {|session| ... } ⇒ Session

Create a new session with options and use new socket connections.

Examples:

Change safe mode

session.with(safe: { w: 2 })[:people].insert(name: "Joe")

Change safe mode with block

session.with(safe: { w: 2 }) do |session|
  session[:people].insert(name: "Joe")
end

Temporarily change database

session.with(database: "admin") do |admin|
  admin.command ismaster: 1
end

Copy between databases

session.use "moped"
session.with(database: "backup") do |backup|
  session[:people].each do |person|
    backup[:people].insert person
  end
end

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Yield Parameters:

  • session (Session)

    The new session.

Returns:

See Also:

Since:

  • 1.0.0



180
181
182
183
184
185
186
187
188
# File 'lib/moped/session.rb', line 180

def new(options = {})
  session = with(options)
  session.instance_variable_set(:@cluster, cluster.dup)
  if block_given?
    yield session
  else
    session
  end
end

#safe?true, false

Is the session operating in safe mode?

Examples:

Is the session operating in safe mode?

session.safe?

Returns:

  • (true, false)

    Whether the current session requires safe operations.

Since:

  • 1.0.0



199
200
201
# File 'lib/moped/session.rb', line 199

def safe?
  !!safety
end

#safetyBoolean, Hash

Get the safety level for the session.

Examples:

Get the safety level.

session.safety

Returns:

  • (Boolean, Hash)

    The safety level for this session.

Since:

  • 1.0.0



211
212
213
# File 'lib/moped/session.rb', line 211

def safety
  options[:safe].__safe_options__
end

#use(database) ⇒ Object

Switch the session’s current database.

Examples:

Switch the current database.

session.use :moped
session[:people].find.one # => { :name => "John" }

Parameters:

Since:

  • 1.0.0



224
225
226
227
# File 'lib/moped/session.rb', line 224

def use(database)
  options[:database] = database
  set_current_database database
end

#with(options = {}) {|session| ... } ⇒ Session, Object

Create a new session with options reusing existing connections.

Examples:

Change safe mode

session.with(safe: { w: 2 })[:people].insert(name: "Joe")

Change safe mode with block

session.with(safe: { w: 2 }) do |session|
  session[:people].insert(name: "Joe")
end

Temporarily change database

session.with(database: "admin") do |admin|
  admin.command ismaster: 1
end

Copy between databases

session.use "moped"
session.with(database: "backup") do |backup|
  session[:people].each do |person|
    backup[:people].insert person
  end
end

Parameters:

  • options (Hash) (defaults to: {})

    The session options.

Yield Parameters:

  • session (Session)

    The new session.

Returns:

  • (Session, Object)

    The new session, or the value returned by the block if provided.

Since:

  • 1.0.0



260
261
262
263
264
265
266
267
268
# File 'lib/moped/session.rb', line 260

def with(options = {})
  session = dup
  session.options.update(options)
  if block_given?
    yield session
  else
    session
  end
end