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) ⇒ 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.
-
#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.
52 53 54 55 |
# File 'lib/mongoid/persistence_context.rb', line 52 def initialize(object, opts = {}) @object = object (opts) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
The options defining this persistence context.
21 22 23 |
# File 'lib/mongoid/persistence_context.rb', line 21 def @options end |
Class Method Details
.clear(object, cluster = nil) ⇒ Object
Clear the persistence context for a particular class or model instance.
211 212 213 214 215 216 217 |
# File 'lib/mongoid/persistence_context.rb', line 211 def clear(object, cluster = nil) if context = get(object) context.client.close unless (context.cluster.equal?(cluster) || cluster.nil?) end ensure Thread.current["[mongoid][#{object.object_id}]:context"] = nil end |
.get(object) ⇒ Mongoid::PersistenceContext
Get the persistence context for a particular class or model instance.
198 199 200 |
# File 'lib/mongoid/persistence_context.rb', line 198 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.
182 183 184 185 186 |
# File 'lib/mongoid/persistence_context.rb', line 182 def set(object, ) context = PersistenceContext.new(object, .is_a?(PersistenceContext) ? . : ) Thread.current["[mongoid][#{object.object_id}]:context"] = context end |
Instance Method Details
#==(other) ⇒ true, false
Determine if this persistence context is equal to another.
129 130 131 132 |
# File 'lib/mongoid/persistence_context.rb', line 129 def ==(other) return false unless other.is_a?(PersistenceContext) == other. end |
#client ⇒ Mongo::Client
Get the client for this persistence context.
109 110 111 112 113 114 115 116 117 |
# File 'lib/mongoid/persistence_context.rb', line 109 def client = send(:client_options) if [:read].is_a?(Symbol) = .merge(read: {mode: [:read]}) end @client ||= (client = Clients.with_name(client_name) client = client.use(database_name) if database_name_option client.with()) end |
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for this persistence context.
69 70 71 |
# File 'lib/mongoid/persistence_context.rb', line 69 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.
82 83 84 85 |
# File 'lib/mongoid/persistence_context.rb', line 82 def collection_name @collection_name ||= (__evaluate__([:collection] || [:collection])) end |
#database_name ⇒ String
Get the database name for this persistence context.
96 97 98 |
# File 'lib/mongoid/persistence_context.rb', line 96 def database_name __evaluate__(database_name_option) || client.database.name end |