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.

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.

[ :client,
  :collection,
  :collection_options
].freeze
VALID_OPTIONS =

The full list of valid persistence context options.

Returns:

  • (Array<Symbol>)

    The full list of options defining the persistence context.

( 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:

  • object (Object)

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

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

    The persistence context options.



46
47
48
49
# File 'lib/mongoid/persistence_context.rb', line 46

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.



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

def options
  @options
end

Class Method Details

.clear(object, cluster = nil, original_context = nil) ⇒ Object

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

Examples:

Clear the persistence context for a class or model instance.

PersistenceContext.clear(model)

Parameters:

  • object (Class | Object)

    The class or model instance.

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

    The original cluster before this context was used.

  • original_context (Mongoid::PersistenceContext) (defaults to: nil)

    The original persistence context that was set before this context was used.



271
272
273
274
275
276
277
278
279
# File 'lib/mongoid/persistence_context.rb', line 271

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.

Examples:

Get the persistence context for a class or model instance.

PersistenceContext.get(model)

Parameters:

  • object (Object)

    The class or model instance.

Returns:



258
259
260
# File 'lib/mongoid/persistence_context.rb', line 258

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.

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:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/mongoid/persistence_context.rb', line 235

def set(object, options_or_context)
  existing_context = get_context(object)
  existing_options = if existing_context
    existing_context.options
  else
    {}
  end
  if options_or_context.is_a?(PersistenceContext)
    options_or_context = options_or_context.options
  end
  new_options = existing_options.merge(options_or_context)
  context = PersistenceContext.new(object, new_options)
  store_context(object, 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.



154
155
156
157
# File 'lib/mongoid/persistence_context.rb', line 154

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mongoid/persistence_context.rb', line 117

def client
  @client ||= begin
    client = Clients.with_name(client_name)
    options = client_options

    if database_name_option
      client = client.use(database_name)
      options = options.except(:database, 'database')
    end

    client = client.with(options) unless options.empty?

    client
  end
end

#client_nameSymbol

Get the client name for this persistence context.

Examples:

Get the client name for this persistence context.

context.client_name

Returns:

  • (Symbol)

    The client name for this persistence context.



140
141
142
143
144
# File 'lib/mongoid/persistence_context.rb', line 140

def client_name
  @client_name ||= options[:client] ||
                     Threaded.client_override ||
                     __evaluate__(storage_options[:client])
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.



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

def collection(parent = nil)
  parent ?
    parent.collection.with(client_options.except(:database, "database")) :
    client[collection_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.



94
95
96
97
# File 'lib/mongoid/persistence_context.rb', line 94

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.



106
107
108
# File 'lib/mongoid/persistence_context.rb', line 106

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

#for_child(document) ⇒ PersistenceContext

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.

Returns a new persistence context that is consistent with the given child document, inheriting most appropriate settings.

Parameters:

Returns:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongoid/persistence_context.rb', line 59

def for_child(document)
  if document.is_a?(Class)
    return self if document == (@object.is_a?(Class) ? @object : @object.class)
  elsif document.is_a?(Mongoid::Document)
    return self if document.class == (@object.is_a?(Class) ? @object : @object.class)
  else
    raise ArgumentError, 'must specify a class or a document instance'
  end

  PersistenceContext.new(document, options.merge(document.storage_options))
end

#requested_storage_optionsHash | nil

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.

The subset of provided options that may be used as storage options.

Returns:

  • (Hash | nil)

    the requested storage options, or nil if none were specified.



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

def requested_storage_options
  slice = @options.slice(*Mongoid::Clients::Validators::Storage::VALID_OPTIONS)
  slice.any? ? slice : nil
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.

Returns:

  • (true | false)

    True if client can be reused, otherwise false.



170
171
172
# File 'lib/mongoid/persistence_context.rb', line 170

def reusable_client?
  @options.keys == [:client]
end