class Bones::RPC::Session
A session in bones_rpc is root for all interactions with a Bones::RPC
server or replica set.
It can talk to a single default database, or dynamically speak to multiple databases.
@example Single database (console-style)
session = Bones::RPC::Session.new(["127.0.0.1:27017"]) session.use(:bones_rpc) session[:users].find.one
@example Multiple databases
session = Bones::RPC::Session.new(["127.0.0.1:27017"]) session.with(database: :admin) do |admin| admin.command(ismaster: 1) end
@example Authentication
session = Bones::RPC::Session.new %w[127.0.0.1:27017], session.with(database: "admin").login("admin", "s3cr3t")
@since 0.0.1
Attributes
@!attribute cluster
@return [ Cluster ] The cluster of nodes.
@!attribute options
@return [ Hash ] The configuration options.
@!attribute cluster
@return [ Cluster ] The cluster of nodes.
@!attribute options
@return [ Hash ] The configuration options.
Public Class Methods
Create a new session from a URI.
@example Initialize a new session.
Session.connect("bones-rpc://localhost:27017/my_db")
@param [ String ] Bones::RPC
URI formatted string.
@return [ Session
] The new session.
@since 0.0.1
# File lib/bones/rpc/session.rb, line 189 def connect(uri, &block) uri = Uri.new(uri) session = new(*uri.bones_rpc_arguments, &block) session end
Initialize a new database session.
@example Initialize a new session.
Session.new([ "localhost:27017" ])
@param [ Array ] seeds An array of host:port pairs. @param [ Hash ] options The options for the session.
@see Above options validations for allowed values in the options hash.
@since 0.0.1
# File lib/bones/rpc/session.rb, line 145 def initialize(seeds, options = {}, &callback) validate_strict(options) @options = options @callback = callback @cluster = Cluster.new(self, seeds) end
Public Instance Methods
# File lib/bones/rpc/session.rb, line 41 def backend @backend ||= Backend.get(options[:backend] || :synchronous).tap(&:setup) end
Run command
on the current database.
@param (see Bones::RPC::Database#command)
@return (see Bones::RPC::Database#command)
@since 0.0.1
# File lib/bones/rpc/session.rb, line 52 def command(op) current_database.command(op) end
# File lib/bones/rpc/session.rb, line 56 def context @context ||= Context.new(self) end
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.
@return [ true ] True if the disconnect succeeded.
@since 0.0.1
# File lib/bones/rpc/session.rb, line 67 def disconnect cluster.disconnect end
# File lib/bones/rpc/session.rb, line 71 def handle_refresh(node) @callback.call(node) if @callback end
Provide a string inspection for the session.
@example Inspect the session.
session.inspect
@return [ String ] The string inspection.
@since 0.0.1
# File lib/bones/rpc/session.rb, line 83 def inspect "<#{self.class.name} seeds=#{cluster.seeds}>" end
# File lib/bones/rpc/session.rb, line 152 def notify(method, *params) context.notify(method, params) end
Get the read preference for the session. Will default to primary if none was provided.
@example Get the session's read preference.
session.read_preference
@return [ Object ] The read preference.
@since 0.0.1
# File lib/bones/rpc/session.rb, line 165 def read_preference @read_preference ||= ReadPreference.get(options[:read] || :nearest) end
# File lib/bones/rpc/session.rb, line 169 def request(method, *params) context.request(method, params) end
# File lib/bones/rpc/session.rb, line 173 def synchronize context.synchronize end
Private Instance Methods
# File lib/bones/rpc/session.rb, line 198 def initialize_copy(_) @options = @options.dup @read_preference = nil end