Class: Moped::Session
- 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.
Defined Under Namespace
Classes: Context
Instance Attribute Summary collapse
-
#cluster ⇒ Object
readonly
Returns the value of attribute cluster.
- #cluster The session cluster.(Thesessioncluster.) ⇒ Object readonly
-
#context ⇒ Object
readonly
Returns the value of attribute context.
- #context The session context.(Thesessioncontext.) ⇒ Object readonly
-
#options ⇒ Object
readonly
Returns the value of attribute options.
- #options The session options.(Thesessionoptions.) ⇒ Object readonly
Instance Method Summary collapse
-
#[](name) ⇒ Collection
Return
collection
from the current database. -
#collection_names ⇒ Array<String>
Return non system collection name from the current database.
-
#collections ⇒ Array<Collection>
Return non system collection name from the current database.
-
#command(op) ⇒ Hash
Run
command
on the current database. -
#consistency ⇒ :strong, :eventual
Get the session’s consistency.
-
#database_names ⇒ Array<String>
Get a list of all the database names for the session.
-
#databases ⇒ Hash
Get information on all databases for the session.
-
#disconnect ⇒ true
Disconnects all nodes in the session’s cluster.
-
#drop ⇒ nil
Drop the current database.
-
#initialize(seeds, options = {}) ⇒ Session
constructor
Initialize a new database session.
-
#login(username, password) ⇒ Object
Log in with
username
andpassword
on the current database. -
#logout ⇒ Object
Log out from the current database.
-
#new(options = {}) {|session| ... } ⇒ Session
Create a new session with
options
and use new socket connections. -
#safe? ⇒ true, false
Is the session operating in safe mode?.
-
#safety ⇒ Boolean, Hash
Get the safety level for the session.
-
#use(database) ⇒ Object
Switch the session’s current database.
-
#with(options = {}) {|session| ... } ⇒ Session, Object
Create a new session with
options
reusing existing connections.
Constructor Details
Instance Attribute Details
#cluster ⇒ Object (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 |
#context ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
35 36 37 |
# File 'lib/moped/session.rb', line 35 def @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.
44 45 46 |
# File 'lib/moped/session.rb', line 44 def [](name) current_database[name] end |
#collection_names ⇒ Array<String>
Return non system collection name from the current database.
55 56 57 |
# File 'lib/moped/session.rb', line 55 def collection_names current_database.collection_names end |
#collections ⇒ Array<Collection>
Return non system collection name from the current database.
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.
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.
163 164 165 |
# File 'lib/moped/session.rb', line 163 def consistency [:consistency] end |
#database_names ⇒ Array<String>
This requires admin access on your server.
Get a list of all the database names for the session.
91 92 93 |
# File 'lib/moped/session.rb', line 91 def database_names databases["databases"].map { |database| database["name"] } end |
#databases ⇒ Hash
This requires admin access on your server.
Get information on all databases for the session. This includes the name, size on disk, and if it is empty or not.
107 108 109 |
# File 'lib/moped/session.rb', line 107 def databases with(database: :admin).command(listDatabases: 1) end |
#disconnect ⇒ true
Disconnects all nodes in the session’s cluster. This should only be used in cases # where you know you’re not going to use the cluster on the thread anymore and need to force the connections to close.
118 119 120 |
# File 'lib/moped/session.rb', line 118 def disconnect cluster.disconnect end |
#drop ⇒ nil
Drop the current database.
129 130 131 |
# File 'lib/moped/session.rb', line 129 def drop current_database.drop end |
#login(username, password) ⇒ Object
Log in with username
and password
on the current database.
140 141 142 |
# File 'lib/moped/session.rb', line 140 def login(username, password) current_database.login(username, password) end |
#logout ⇒ Object
Log out from the current database.
151 152 153 |
# File 'lib/moped/session.rb', line 151 def logout current_database.logout end |
#new(options = {}) {|session| ... } ⇒ Session
Create a new session with options
and use new socket connections.
225 226 227 228 229 230 231 232 233 |
# File 'lib/moped/session.rb', line 225 def new( = {}) session = with() 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?
244 245 246 |
# File 'lib/moped/session.rb', line 244 def safe? !!safety end |
#safety ⇒ Boolean, Hash
Get the safety level for the session.
256 257 258 |
# File 'lib/moped/session.rb', line 256 def safety [:safe]. end |
#use(database) ⇒ Object
Switch the session’s current database.
269 270 271 272 |
# File 'lib/moped/session.rb', line 269 def use(database) [:database] = database set_current_database database end |