Class: ROM::Mongo::Gateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/rom/mongo/gateway.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Mongo::Gateway #initialize(uri, options) ⇒ Mongo::Gateway #initialize(connection) ⇒ Mongo::Gateway

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

Overloads:

  • #initialize(uri) ⇒ Mongo::Gateway

    Connects to a database via URI

    Examples:

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

    Parameters:

    • uri (String)

      connection URI

  • #initialize(uri, options) ⇒ Mongo::Gateway

    Connects to a database via URI and options

    Examples:

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

    Parameters:

    • uri (String, Symbol)

      connection URI

    • options (Hash)

      connection options

    Options Hash (options):

    • :inferrable_relations (Array<Symbol>)

      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

    • :not_inferrable_relations (Array<Symbol>)

      A list of collection names that should NOT be inferred

  • #initialize(connection) ⇒ Mongo::Gateway

    Creates a gateway from an existing database connection.

    Examples:

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

    Parameters:

    • connection (Mongo::Client)

      a connection instance

See Also:



64
65
66
67
# 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

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections.



14
15
16
# File 'lib/rom/mongo/gateway.rb', line 14

def collections
  @collections
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/rom/mongo/gateway.rb', line 18

def options
  @options
end

Instance Method Details

#[](name) ⇒ Object



78
79
80
# File 'lib/rom/mongo/gateway.rb', line 78

def [](name)
  collections.fetch(name)
end

#command_namespaceObject



90
91
92
# File 'lib/rom/mongo/gateway.rb', line 90

def command_namespace
  Mongo::Commands
end

#dataset(name) ⇒ Object



82
83
84
# File 'lib/rom/mongo/gateway.rb', line 82

def dataset(name)
  collections[name] = Dataset.new(connection[name])
end

#dataset?(name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rom/mongo/gateway.rb', line 86

def dataset?(name)
  connection.database.collection_names.include?(name.to_s)
end

#schemaArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List of defined collections

Returns:

  • (Array<Symbol>)

    An array with dataset names



74
75
76
# File 'lib/rom/mongo/gateway.rb', line 74

def schema
  connection.database.collection_names.map(&:to_sym)
end