class ROM::Mongo::Gateway

Attributes

collections[R]
options[R]

@!attribute [r] options

@return [Hash] Gateway options

Public Class Methods

new(uri, options = EMPTY_HASH) click to toggle source

Initialize an Mongo gateway

Gateways are typically initialized via ROM::Configuration object, gateway constructor arguments such as URI and options are passed directly to this constructor

@overload initialize(uri)

Connects to a database via URI

@example
  ROM.container(:mongo, 'mongodb://127.0.0.1:27017/db_name')

@param [String] uri connection URI

@overload initialize(uri, options)

Connects to a database via URI and options

@example
  ROM.container(:mongo, 'mongodb://127.0.0.1:27017/db_name', inferrable_relations: %i[users posts])

@param [String,Symbol] uri connection URI

@param [Hash] options connection options

@option options [Array<Symbol>] :inferrable_relations
  A list of collection names that should be inferred. If
  this is set explicitly to an empty array relations
  won't be inferred at all

@option options [Array<Symbol>] :not_inferrable_relations
  A list of collection names that should NOT be inferred

@overload initialize(connection)

Creates a gateway from an existing database connection.

@example
  ROM.container(:mongo, Mongo::Client.new('mongodb://127.0.0.1:27017/db_name'))

@param [Mongo::Client] connection a connection instance

@return [Mongo::Gateway]

@see docs.mongodb.com/ruby-driver/master/ MongoDB driver docs

@api public

# File lib/rom/mongo/gateway.rb, line 64
def initialize(uri, options = EMPTY_HASH)
  @connection = uri.is_a?(::Mongo::Client) ? uri : ::Mongo::Client.new(uri, options)
  @collections = {}
end

Public Instance Methods

[](name) click to toggle source
# File lib/rom/mongo/gateway.rb, line 78
def [](name)
  collections.fetch(name)
end
command_namespace() click to toggle source
# File lib/rom/mongo/gateway.rb, line 90
def command_namespace
  Mongo::Commands
end
dataset(name) click to toggle source
# File lib/rom/mongo/gateway.rb, line 82
def dataset(name)
  collections[name] = Dataset.new(connection[name])
end
dataset?(name) click to toggle source
# File lib/rom/mongo/gateway.rb, line 86
def dataset?(name)
  connection.database.collection_names.include?(name.to_s)
end
schema() click to toggle source

List of defined collections

@return [Array<Symbol>] An array with dataset names

@api private

# File lib/rom/mongo/gateway.rb, line 74
def schema
  connection.database.collection_names.map(&:to_sym)
end