Class: Bones::RPC::Session

Inherits:
Object
  • Object
show all
Includes:
Optionable
Defined in:
lib/bones/rpc/session.rb

Overview

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.

Examples:

Single database (console-style)

session = Bones::RPC::Session.new(["127.0.0.1:27017"])
session.use(:bones_rpc)
session[:users].find.one

Multiple databases

session = Bones::RPC::Session.new(["127.0.0.1:27017"])
session.with(database: :admin) do |admin|
  admin.command(ismaster: 1)
end

Authentication

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

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new database session.

Examples:

Initialize a new session.

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

Parameters:

  • seeds (Array)

    An array of host:port pairs.

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

    The options for the session.

See Also:

  • options validations for allowed values in the options hash.

Since:

  • 1.0.0



136
137
138
139
140
141
# File 'lib/bones/rpc/session.rb', line 136

def initialize(seeds, options = {}, &callback)
  validate_strict(options)
  @options = options
  @callback = callback
  @cluster = Cluster.new(self, seeds)
end

Instance Attribute Details

#clusterCluster

Returns The cluster of nodes.

Returns:

  • (Cluster)

    The cluster of nodes.



39
40
41
# File 'lib/bones/rpc/session.rb', line 39

def cluster
  @cluster
end

#optionsObject

Since:

  • 1.0.0



39
# File 'lib/bones/rpc/session.rb', line 39

attr_reader :cluster, :options

Class Method Details

.connect(uri, &block) ⇒ Session

Create a new session from a URI.

Examples:

Initialize a new session.

Session.connect("bones-rpc://localhost:27017/my_db")

Parameters:

  • Bones::RPC (String)

    URI formatted string.

Returns:

Since:

  • 3.0.0



180
181
182
183
184
# File 'lib/bones/rpc/session.rb', line 180

def connect(uri, &block)
  uri = Uri.new(uri)
  session = new(*uri.bones_rpc_arguments, &block)
  session
end

Instance Method Details

#command(op) ⇒ Object

Run command on the current database.

Since:

  • 1.0.0



48
49
50
# File 'lib/bones/rpc/session.rb', line 48

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

#contextObject

Since:

  • 1.0.0



52
53
54
# File 'lib/bones/rpc/session.rb', line 52

def context
  @context ||= Context.new(self)
end

#disconnecttrue

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.

Returns:

  • (true)

    True if the disconnect succeeded.

Since:

  • 1.2.0



63
64
65
# File 'lib/bones/rpc/session.rb', line 63

def disconnect
  cluster.disconnect
end

#handle_refresh(node) ⇒ Object

Since:

  • 1.0.0



67
68
69
# File 'lib/bones/rpc/session.rb', line 67

def handle_refresh(node)
  @callback.call(node) if @callback
end

#inspectString

Provide a string inspection for the session.

Examples:

Inspect the session.

session.inspect

Returns:

  • (String)

    The string inspection.

Since:

  • 1.4.0



79
80
81
# File 'lib/bones/rpc/session.rb', line 79

def inspect
  "<#{self.class.name} seeds=#{cluster.seeds}>"
end

#notify(method, *params) ⇒ Object

Since:

  • 1.0.0



143
144
145
# File 'lib/bones/rpc/session.rb', line 143

def notify(method, *params)
  context.notify(method, params)
end

#read_preferenceObject

Get the read preference for the session. Will default to primary if none was provided.

Examples:

Get the session’s read preference.

session.read_preference

Returns:

  • (Object)

    The read preference.

Since:

  • 2.0.0



156
157
158
# File 'lib/bones/rpc/session.rb', line 156

def read_preference
  @read_preference ||= ReadPreference.get(options[:read] || :nearest)
end

#request(method, *params) ⇒ Object

Since:

  • 1.0.0



160
161
162
# File 'lib/bones/rpc/session.rb', line 160

def request(method, *params)
  context.request(method, params)
end

#synchronizeObject

Since:

  • 1.0.0



164
165
166
# File 'lib/bones/rpc/session.rb', line 164

def synchronize
  context.synchronize
end