Class: Mongoid::PersistenceContext

Inherits:
Object
  • Object
show all
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.

Since:

  • 6.0.0

Constant Summary collapse

EXTRA_OPTIONS =

Extra options in addition to driver client options that determine the persistence context.

Returns:

  • (Array<Symbol>)

    The list of extra options besides client options that determine the persistence context.

Since:

  • 6.0.0

[ :client,
  :collection
].freeze
VALID_OPTIONS =

The full list of valid persistence context options.

Returns:

  • (Array<Symbol>)

    The full list of options defining the persistence context.

Since:

  • 6.0.0

( Mongo::Client::VALID_OPTIONS + EXTRA_OPTIONS ).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, opts = {}) ⇒ PersistenceContext

Initialize the persistence context object.

Examples:

Create a new persistence context.

PersistenceContext.new(model, collection: 'other')

Parameters:

  • parent (Object)

    The class or model instance for which a persistence context should be created.

  • opts (Hash) (defaults to: {})

    The persistence context options.

Since:

  • 6.0.0



52
53
54
55
# File 'lib/mongoid/persistence_context.rb', line 52

def initialize(object, opts = {})
  @object = object
  set_options!(opts)
end

Instance Attribute Details

#optionsHash (readonly)

The options defining this persistence context.

Returns:

  • (Hash)

    The persistence context options.

Since:

  • 6.0.0



21
22
23
# File 'lib/mongoid/persistence_context.rb', line 21

def options
  @options
end

Class Method Details

.clear(object, cluster = nil) ⇒ Object

Get the persistence context for a particular class or model instance.

Examples:

Get the persistence context for a class or model instance.

PersistenceContext.get(model)

Parameters:

  • object (Class, Object)

    The class or model instance.

  • cluster (Mongo::Cluster) (defaults to: nil)

    The original cluster before this context was used.

Since:

  • 6.0.0



208
209
210
211
212
213
# File 'lib/mongoid/persistence_context.rb', line 208

def clear(object, cluster = nil)
  if context = get(object)
    context.client.close unless (context.cluster.equal?(cluster) || cluster.nil?)
  end
  Thread.current["[mongoid][#{object.object_id}]:context"] = nil
end

.get(object) ⇒ Mongoid::PersistenceContext

Get the persistence context for a particular class or model instance.

Examples:

Get the persistence context for a class or model instance.

PersistenceContext.get(model)

Parameters:

  • object (Object)

    The class or model instance.

Returns:

Since:

  • 6.0.0



195
196
197
# File 'lib/mongoid/persistence_context.rb', line 195

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.

Examples:

Set the persistence context for a class or model instance.

PersistenceContext.set(model)

Parameters:

  • object (Object)

    The class or model instance.

  • options_or_context (Hash, Mongoid::PersistenceContext)

    The persistence options or a persistence context object.

Returns:

Since:

  • 6.0.0



179
180
181
182
183
# File 'lib/mongoid/persistence_context.rb', line 179

def set(object, options_or_context)
  context = PersistenceContext.new(object, options_or_context.is_a?(PersistenceContext) ?
                                             options_or_context.options : options_or_context)
  Thread.current["[mongoid][#{object.object_id}]:context"] = context
end

Instance Method Details

#==(other) ⇒ true, false

Determine if this persistence context is equal to another.

Examples:

Compare two persistence contexts.

context == other_context

Parameters:

  • other (Object)

    The object to be compared with this one.

Returns:

  • (true, false)

    Whether the two persistence contexts are equal.

Since:

  • 6.0.0



126
127
128
129
# File 'lib/mongoid/persistence_context.rb', line 126

def ==(other)
  return false unless other.is_a?(PersistenceContext)
  options == other.options
end

#clientMongo::Client

Get the client for this persistence context.

Examples:

Get the client for this persistence context.

context.client

Returns:

  • (Mongo::Client)

    The client for this persistence context.

Since:

  • 6.0.0



110
111
112
113
114
# File 'lib/mongoid/persistence_context.rb', line 110

def client
  @client ||= (client = Clients.with_name(client_name)
                client = client.use(database_name) if database_name_option
                client.with(client_options))
end

#collection(parent = nil) ⇒ Mongo::Collection

Get the collection for this persistence context.

Examples:

Get the collection for this persistence context.

context.collection

Parameters:

  • parent (Object) (defaults to: nil)

    The parent object whose collection name is used instead of this persistence context’s collection name.

Returns:

  • (Mongo::Collection)

    The collection for this persistence context.

Since:

  • 6.0.0



69
70
71
72
# File 'lib/mongoid/persistence_context.rb', line 69

def collection(parent = nil)
  name = parent ? parent.collection_name : collection_name
  client[name.to_sym]
end

#collection_nameString

Get the collection name for this persistence context.

Examples:

Get the collection name for this persistence context.

context.collection_name

Returns:

  • (String)

    The collection name for this persistence context.

Since:

  • 6.0.0



83
84
85
86
# File 'lib/mongoid/persistence_context.rb', line 83

def collection_name
  @collection_name ||= (__evaluate__(options[:collection] ||
                         storage_options[:collection]))
end

#database_nameString

Get the database name for this persistence context.

Examples:

Get the database name for this persistence context.

context.database_name

Returns:

  • (String)

    The database name for this persistence context.

Since:

  • 6.0.0



97
98
99
# File 'lib/mongoid/persistence_context.rb', line 97

def database_name
  __evaluate__(database_name_option) || client.database.name
end