Module: Mongoid::Clients::Options::ClassMethods

Defined in:
build/mongoid-master/lib/mongoid/clients/options.rb

Instance Method Summary collapse

Instance Method Details

#client_nameObject



60
61
62
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 60

def client_name
  persistence_context.client_name
end

#collectionObject



72
73
74
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 72

def collection
  persistence_context.collection
end

#collection_nameObject



64
65
66
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 64

def collection_name
  persistence_context.collection_name
end

#database_nameObject



68
69
70
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 68

def database_name
  persistence_context.database_name
end

#mongo_clientObject



76
77
78
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 76

def mongo_client
  persistence_context.client
end

#persistence_contextObject



101
102
103
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 101

def persistence_context
  PersistenceContext.get(self) || PersistenceContext.new(self)
end

#with(options, &block) ⇒ Object

Change the persistence context for this class during the block.

Examples:

Save the current document to a different collection.

Model.with(collection: "bands") do |m|
  m.create
end

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String | Symbol)

    The collection name.

  • :database (String | Symbol)

    The database name.

  • :client (String | Symbol)

    The client name.



92
93
94
95
96
97
98
99
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 92

def with(options, &block)
  original_context = PersistenceContext.get(self)
  original_cluster = persistence_context.cluster
  PersistenceContext.set(self, options)
  yield self
ensure
  PersistenceContext.clear(self, original_cluster, original_context)
end