Class: Mongoid::PersistenceContext
- Inherits:
-
Object
- Object
- Mongoid::PersistenceContext
- Extended by:
- Forwardable
- Defined in:
- 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 ⇒ Object
-
#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.
Constructor Details
#initialize(object, opts = {}) ⇒ PersistenceContext
Initialize the persistence context object.
55 56 57 58 |
# File 'lib/mongoid/persistence_context.rb', line 55 def initialize(object, opts = {}) @object = object (opts) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
The options defining this persistence context.
24 25 26 |
# File 'lib/mongoid/persistence_context.rb', line 24 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.
238 239 240 241 242 243 244 |
# File 'lib/mongoid/persistence_context.rb', line 238 def clear(object, cluster = nil, original_context = nil) if context = get(object) context.client.close unless (context.cluster.equal?(cluster) || cluster.nil?) end ensure Thread.current["[mongoid][#{object.object_id}]:context"] = original_context end |
.get(object) ⇒ Mongoid::PersistenceContext
Get the persistence context for a particular class or model instance.
223 224 225 |
# File 'lib/mongoid/persistence_context.rb', line 223 def get(object) Thread.current["[mongoid][#{object.object_id}]:context"] 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.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/mongoid/persistence_context.rb', line 197 def set(object, ) key = "[mongoid][#{object.object_id}]:context" existing_context = Thread.current[key] = if existing_context existing_context. else {} end if .is_a?(PersistenceContext) = . end = .merge() context = PersistenceContext.new(object, ) Thread.current[key] = context end |
Instance Method Details
#==(other) ⇒ true, false
Determine if this persistence context is equal to another.
141 142 143 144 |
# File 'lib/mongoid/persistence_context.rb', line 141 def ==(other) return false unless other.is_a?(PersistenceContext) == other. end |
#client ⇒ Mongo::Client
Get the client for this persistence context.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mongoid/persistence_context.rb', line 112 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 ⇒ Object
125 126 127 128 129 |
# File 'lib/mongoid/persistence_context.rb', line 125 def client_name @client_name ||= [:client] || Threaded.client_override || && __evaluate__([:client]) end |
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for this persistence context.
72 73 74 |
# File 'lib/mongoid/persistence_context.rb', line 72 def collection(parent = nil) parent ? parent.collection.with() : client[collection_name.to_sym] end |
#collection_name ⇒ String
Get the collection name for this persistence context.
85 86 87 88 |
# File 'lib/mongoid/persistence_context.rb', line 85 def collection_name @collection_name ||= (__evaluate__([:collection] || [:collection])) end |
#database_name ⇒ String
Get the database name for this persistence context.
99 100 101 |
# File 'lib/mongoid/persistence_context.rb', line 99 def database_name __evaluate__(database_name_option) || client.database.name end |