Class: Mongoid::PersistenceContext
- Inherits:
-
Object
- Object
- Mongoid::PersistenceContext
- Extended by:
- Forwardable
- Defined in:
- build/mongoid-master/lib/mongoid/persistence_context.rb
Overview
Object encapsulating logic for setting/getting a collection and database name and a client with particular options to use when persisting models.
Constant Summary collapse
- EXTRA_OPTIONS =
Extra options in addition to driver client options that determine the persistence context.
[ :client, :collection ].freeze
- VALID_OPTIONS =
The full list of valid persistence context options.
( Mongo::Client::VALID_OPTIONS + EXTRA_OPTIONS ).freeze
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
The options defining this persistence context.
Class Method Summary collapse
-
.clear(object, cluster = nil, original_context = nil) ⇒ Object
Clear the persistence context for a particular class or model instance.
-
.get(object) ⇒ Mongoid::PersistenceContext
Get the persistence context for a particular class or model instance.
-
.set(object, options_or_context) ⇒ Mongoid::PersistenceContext
Set the persistence context for a particular class or model instance.
Instance Method Summary collapse
-
#==(other) ⇒ true | false
Determine if this persistence context is equal to another.
-
#client ⇒ Mongo::Client
Get the client for this persistence context.
-
#client_name ⇒ Symbol
Get the client name for this persistence context.
-
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for this persistence context.
-
#collection_name ⇒ String
Get the collection name for this persistence context.
-
#database_name ⇒ String
Get the database name for this persistence context.
-
#initialize(object, opts = {}) ⇒ PersistenceContext
constructor
Initialize the persistence context object.
-
#reusable_client? ⇒ true | false
private
Whether the client of the context can be reused later, and therefore should not be closed.
Constructor Details
#initialize(object, opts = {}) ⇒ PersistenceContext
Initialize the persistence context object.
45 46 47 48 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 45 def initialize(object, opts = {}) @object = object (opts) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
The options defining this persistence context.
20 21 22 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 20 def @options end |
Class Method Details
.clear(object, cluster = nil, original_context = nil) ⇒ Object
Clear the persistence context for a particular class or model instance.
235 236 237 238 239 240 241 242 243 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 235 def clear(object, cluster = nil, original_context = nil) if context = get(object) unless cluster.nil? || context.cluster.equal?(cluster) context.client.close unless context.reusable_client? end end ensure store_context(object, original_context) end |
.get(object) ⇒ Mongoid::PersistenceContext
Get the persistence context for a particular class or model instance.
222 223 224 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 222 def get(object) get_context(object) end |
.set(object, options_or_context) ⇒ Mongoid::PersistenceContext
Set the persistence context for a particular class or model instance.
If there already is a persistence context set, options in the existing context are combined with options given to the set call.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 199 def set(object, ) existing_context = get_context(object) = if existing_context existing_context. else {} end if .is_a?(PersistenceContext) = . end = .merge() context = PersistenceContext.new(object, ) store_context(object, context) end |
Instance Method Details
#==(other) ⇒ true | false
Determine if this persistence context is equal to another.
130 131 132 133 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 130 def ==(other) return false unless other.is_a?(PersistenceContext) == other. end |
#client ⇒ Mongo::Client
Get the client for this persistence context.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 96 def client @client ||= begin client = Clients.with_name(client_name) if database_name_option client = client.use(database_name) end unless .empty? client = client.with() end client end end |
#client_name ⇒ Symbol
Get the client name for this persistence context.
116 117 118 119 120 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 116 def client_name @client_name ||= [:client] || Threaded.client_override || && __evaluate__([:client]) end |
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for this persistence context.
60 61 62 63 64 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 60 def collection(parent = nil) parent ? parent.collection.with(.except(:database, "database")) : client[collection_name.to_sym] end |
#collection_name ⇒ String
Get the collection name for this persistence context.
73 74 75 76 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 73 def collection_name @collection_name ||= (__evaluate__([:collection] || [:collection])) end |
#database_name ⇒ String
Get the database name for this persistence context.
85 86 87 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 85 def database_name __evaluate__(database_name_option) || client.database.name end |
#reusable_client? ⇒ true | false
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.
Whether the client of the context can be reused later, and therefore should not be closed.
If the persistence context is requested with :client option only, it means that the context should use a client configured in mongoid.yml. Such clients should not be closed when the context is cleared since they will be reused later.
146 147 148 |
# File 'build/mongoid-master/lib/mongoid/persistence_context.rb', line 146 def reusable_client? @options.keys == [:client] end |