Class: Moped::Session
- Inherits:
-
Object
- Object
- Moped::Session
- Includes:
- Optionable
- Defined in:
- lib/moped/session.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.
Instance Attribute Summary collapse
-
#cluster ⇒ Cluster
The cluster of nodes.
- #options ⇒ Object
Class Method Summary collapse
-
.connect(uri) ⇒ Session
Create a new session from a URI.
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. -
#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.
-
#inspect ⇒ String
Provide a string inspection for the 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. -
#read_preference ⇒ Object
Get the read preference 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. -
#write_concern ⇒ Object
Get the write concern for the session.
Constructor Details
Instance Attribute Details
#cluster ⇒ Cluster
Returns The cluster of nodes.
40 41 42 |
# File 'lib/moped/session.rb', line 40 def cluster @cluster end |
#options ⇒ Object
40 |
# File 'lib/moped/session.rb', line 40 attr_reader :cluster, :options |
Class Method Details
Instance Method Details
#[](name) ⇒ Collection
Return collection
from the current database.
49 50 51 |
# File 'lib/moped/session.rb', line 49 def [](name) current_database[name] end |
#collection_names ⇒ Array<String>
Return non system collection name from the current database.
60 61 62 |
# File 'lib/moped/session.rb', line 60 def collection_names current_database.collection_names end |
#collections ⇒ Array<Collection>
Return non system collection name from the current database.
71 72 73 |
# File 'lib/moped/session.rb', line 71 def collections current_database.collections end |
#command(op) ⇒ Hash
Run command
on the current database.
82 83 84 |
# File 'lib/moped/session.rb', line 82 def command(op) current_database.command(op) end |
#database_names ⇒ Array<String>
This requires admin access on your server.
Get a list of all the database names for the session.
96 97 98 |
# File 'lib/moped/session.rb', line 96 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.
112 113 114 |
# File 'lib/moped/session.rb', line 112 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.
123 124 125 |
# File 'lib/moped/session.rb', line 123 def disconnect cluster.disconnect end |
#drop ⇒ nil
Drop the current database.
134 135 136 |
# File 'lib/moped/session.rb', line 134 def drop current_database.drop end |
#inspect ⇒ String
Provide a string inspection for the session.
146 147 148 |
# File 'lib/moped/session.rb', line 146 def inspect "<#{self.class.name} seeds=#{cluster.seeds} database=#{current_database_name}>" end |
#login(username, password) ⇒ Object
Log in with username
and password
on the current database.
157 158 159 |
# File 'lib/moped/session.rb', line 157 def login(username, password) current_database.login(username, password) end |
#logout ⇒ Object
Log out from the current database.
168 169 170 |
# File 'lib/moped/session.rb', line 168 def logout current_database.logout end |
#new(options = {}) {|session| ... } ⇒ Session
Create a new session with options
and use new socket connections.
302 303 304 305 306 307 308 309 310 |
# File 'lib/moped/session.rb', line 302 def new( = {}) session = with() session.instance_variable_set(:@cluster, cluster.dup) if block_given? yield(session) else session end end |
#read_preference ⇒ Object
Get the read preference for the session. Will default to primary if none was provided.
321 322 323 |
# File 'lib/moped/session.rb', line 321 def read_preference @read_preference ||= ReadPreference.get([:read] || :primary) end |
#use(database) ⇒ Object
Switch the session’s current database.
334 335 336 337 |
# File 'lib/moped/session.rb', line 334 def use(database) [:database] = database set_current_database(database) end |
#with(options = {}) {|session| ... } ⇒ Session, Object
Create a new session with options
reusing existing connections.
370 371 372 373 374 375 376 377 378 |
# File 'lib/moped/session.rb', line 370 def with( = {}) session = dup session..update() if block_given? yield(session) else session end end |
#write_concern ⇒ Object
Get the write concern for the session. Will default to propagate if none was provided.
389 390 391 |
# File 'lib/moped/session.rb', line 389 def write_concern @write_concern ||= WriteConcern.get([:write] || { w: 1 }) end |