Module: Mongoid::Threaded

Extended by:
Threaded
Included in:
Threaded
Defined in:
lib/mongoid/threaded.rb

Overview

This module contains logic for easy access to objects that have a lifecycle on the current thread.

Instance Method Summary collapse

Instance Method Details

#assign_stackArray

Get the assign stack for the current thread. Is simply an array of calls to Mongoid’s assigning method.

Examples:

Get the assign stack.

Threaded.assign_stack

Returns:

  • (Array)

    The array of assign calls.

Since:

  • 2.1.9



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

def assign_stack
  Thread.current[:"[mongoid]:assign-stack"] ||= []
end

#assigning?true, false

Is the current thread in assigning mode?

Examples:

Is the thread in assigning mode?

Threaded.assigning?

Returns:

  • (true, false)

    If the thread is in assigning mode?

Since:

  • 2.1.0



77
78
79
# File 'lib/mongoid/threaded.rb', line 77

def assigning?
  !assign_stack.empty?
end

#begin_assigntrue

Begins a assigning block.

Examples:

Begin the assign.

Threaded.begin_assign

Returns:

  • (true)

    Always true.

Since:

  • 2.1.9



17
18
19
# File 'lib/mongoid/threaded.rb', line 17

def begin_assign
  assign_stack.push(true)
end

#begin_bindtrue

Begins a binding block.

Examples:

Begin the bind.

Threaded.begin_bind

Returns:

  • (true)

    Always true.

Since:

  • 2.1.9



29
30
31
# File 'lib/mongoid/threaded.rb', line 29

def begin_bind
  bind_stack.push(true)
end

#begin_buildtrue

Begins a building block.

Examples:

Begin the build.

Threaded.begin_build

Returns:

  • (true)

    Always true.

Since:

  • 2.1.9



41
42
43
# File 'lib/mongoid/threaded.rb', line 41

def begin_build
  build_stack.push(true)
end

#begin_createtrue

Begins a creating block.

Examples:

Begin the create.

Threaded.begin_create

Returns:

  • (true)

    Always true.

Since:

  • 2.1.9



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

def begin_create
  create_stack.push(true)
end

#begin_validate(document) ⇒ Object

Begin validating a document on the current thread.

Examples:

Begin validation.

Threaded.begin_validate(doc)

Parameters:

  • document (Document)

    The document to validate.

Since:

  • 2.1.9



65
66
67
# File 'lib/mongoid/threaded.rb', line 65

def begin_validate(document)
  validations_for(document.class).push(document.id)
end

#bind_stackArray

Get the bind stack for the current thread. Is simply an array of calls to Mongoid’s binding method.

Examples:

Get the bind stack.

Threaded.bind_stack

Returns:

  • (Array)

    The array of bind calls.

Since:

  • 2.1.9



139
140
141
# File 'lib/mongoid/threaded.rb', line 139

def bind_stack
  Thread.current[:"[mongoid]:bind-stack"] ||= []
end

#binding?true, false

Is the current thread in binding mode?

Examples:

Is the thread in binding mode?

Threaded.binding?

Returns:

  • (true, false)

    If the thread is in binding mode?

Since:

  • 2.1.0



89
90
91
# File 'lib/mongoid/threaded.rb', line 89

def binding?
  !bind_stack.empty?
end

#build_stackArray

Get the build stack for the current thread. Is simply an array of calls to Mongoid’s building method.

Examples:

Get the build stack.

Threaded.build_stack

Returns:

  • (Array)

    The array of build calls.

Since:

  • 2.1.9



152
153
154
# File 'lib/mongoid/threaded.rb', line 152

def build_stack
  Thread.current[:"[mongoid]:build-stack"] ||= []
end

#building?true, false

Is the current thread in building mode?

Examples:

Is the thread in building mode?

Threaded.building?

Returns:

  • (true, false)

    If the thread is in building mode?

Since:

  • 2.1.0



101
102
103
# File 'lib/mongoid/threaded.rb', line 101

def building?
  !build_stack.empty?
end

#clear_safety_options!nil

Clear out all the safety options set using the safely proxy.

Examples:

Clear out the options.

Threaded.clear_safety_options!

Returns:

  • (nil)

    nil

Since:

  • 2.1.0



177
178
179
# File 'lib/mongoid/threaded.rb', line 177

def clear_safety_options!
  Thread.current[:"[mongoid]:safety-options"] = nil
end

#create_stackArray

Get the create stack for the current thread. Is simply an array of calls to Mongoid’s creating method.

Examples:

Get the create stack.

Threaded.create_stack

Returns:

  • (Array)

    The array of create calls.

Since:

  • 2.1.9



165
166
167
# File 'lib/mongoid/threaded.rb', line 165

def create_stack
  Thread.current[:"[mongoid]:create-stack"] ||= []
end

#creating?true, false

Is the current thread in creating mode?

Examples:

Is the thread in creating mode?

Threaded.creating?

Returns:

  • (true, false)

    If the thread is in creating mode?

Since:

  • 2.1.0



113
114
115
# File 'lib/mongoid/threaded.rb', line 113

def creating?
  !create_stack.empty?
end

#exit_assigntrue

Exit the assigning block.

Examples:

Exit the assigning block.

Threaded.exit_assign

Returns:

  • (true)

    The last element in the stack.

Since:

  • 2.1.9



189
190
191
# File 'lib/mongoid/threaded.rb', line 189

def exit_assign
  assign_stack.pop
end

#exit_bindtrue

Exit the binding block.

Examples:

Exit the binding block.

Threaded.exit_bind

Returns:

  • (true)

    The last element in the stack.

Since:

  • 2.1.9



201
202
203
# File 'lib/mongoid/threaded.rb', line 201

def exit_bind
  bind_stack.pop
end

#exit_buildtrue

Exit the building block.

Examples:

Exit the building block.

Threaded.exit_build

Returns:

  • (true)

    The last element in the stack.

Since:

  • 2.1.9



213
214
215
# File 'lib/mongoid/threaded.rb', line 213

def exit_build
  build_stack.pop
end

#exit_createtrue

Exit the creating block.

Examples:

Exit the creating block.

Threaded.exit_create

Returns:

  • (true)

    The last element in the stack.

Since:

  • 2.1.9



225
226
227
# File 'lib/mongoid/threaded.rb', line 225

def exit_create
  create_stack.pop
end

#exit_validate(document) ⇒ Object

Exit validating a document on the current thread.

Examples:

Exit validation.

Threaded.exit_validate(doc)

Parameters:

  • document (Document)

    The document to validate.

Since:

  • 2.1.9



237
238
239
# File 'lib/mongoid/threaded.rb', line 237

def exit_validate(document)
  validations_for(document.class).delete_one(document.id)
end

#identity_mapIdentityMap

Get the identity map off the current thread.

Examples:

Get the identity map.

Threaded.identity_map

Returns:

Since:

  • 2.1.0



249
250
251
# File 'lib/mongoid/threaded.rb', line 249

def identity_map
  Thread.current[:"[mongoid]:identity-map"] ||= IdentityMap.new
end

#insertObject

Get the insert consumer from the current thread.

Examples:

Get the insert consumer.

Threaded.insert

Returns:

  • (Object)

    The batch insert consumer.

Since:

  • 2.1.0



261
262
263
# File 'lib/mongoid/threaded.rb', line 261

def insert
  Thread.current[:"[mongoid]:insert-consumer"]
end

#insert=(consumer) ⇒ Object

Set the insert consumer on the current thread.

Examples:

Set the insert consumer.

Threaded.insert = consumer

Parameters:

  • consumer (Object)

    The insert consumer.

Returns:

  • (Object)

    The insert consumer.

Since:

  • 2.1.0



275
276
277
# File 'lib/mongoid/threaded.rb', line 275

def insert=(consumer)
  Thread.current[:"[mongoid]:insert-consumer"] = consumer
end

#safety_optionsHash

Get the safety options for the current thread.

Examples:

Get the safety options.

Threaded.safety_options

Returns:

  • (Hash)

    The current safety options.

Since:

  • 2.1.0



287
288
289
# File 'lib/mongoid/threaded.rb', line 287

def safety_options
  Thread.current[:"[mongoid]:safety-options"]
end

#safety_options=(options) ⇒ Hash

Set the safety options on the current thread.

Examples:

Set the safety options.

Threaded.safety_options = { :fsync => true }

Parameters:

  • options (Hash)

    The safety options.

Returns:

  • (Hash)

    The safety options.

Since:

  • 2.1.0



301
302
303
# File 'lib/mongoid/threaded.rb', line 301

def safety_options=(options)
  Thread.current[:"[mongoid]:safety-options"] = options
end

#scope_stackHash

Get the mongoid scope stack for chained criteria.

Examples:

Get the scope stack.

Threaded.scope_stack

Returns:

  • (Hash)

    The scope stack.

Since:

  • 2.1.0



313
314
315
# File 'lib/mongoid/threaded.rb', line 313

def scope_stack
  Thread.current[:"[mongoid]:scope-stack"] ||= {}
end

#set_update_consumer(klass, consumer) ⇒ Object

Set the update consumer on the current thread.

Examples:

Set the update consumer.

Threaded.update = consumer

Parameters:

  • consumer (Object)

    The update consumer.

Returns:

  • (Object)

    The update consumer.

Since:

  • 2.1.0



339
340
341
# File 'lib/mongoid/threaded.rb', line 339

def set_update_consumer(klass, consumer)
  Thread.current[:"[mongoid][#{klass}]:update-consumer"] = consumer
end

#update_consumer(klass) ⇒ Object

Get the update consumer from the current thread.

Examples:

Get the update consumer.

Threaded.update

Returns:

  • (Object)

    The atomic update consumer.

Since:

  • 2.1.0



325
326
327
# File 'lib/mongoid/threaded.rb', line 325

def update_consumer(klass)
  Thread.current[:"[mongoid][#{klass}]:update-consumer"]
end

#validated?(document) ⇒ true, false

Is the document validated on the current thread?

Examples:

Is the document validated?

Threaded.validated?(doc)

Parameters:

  • document (Document)

    The document to check.

Returns:

  • (true, false)

    If the document is validated.

Since:

  • 2.1.9



353
354
355
# File 'lib/mongoid/threaded.rb', line 353

def validated?(document)
  validations_for(document.class).include?(document.id)
end

#validationsHash

Get all validations on the current thread.

Examples:

Get all validations.

Threaded.validations

Returns:

  • (Hash)

    The current validations.

Since:

  • 2.1.9



365
366
367
# File 'lib/mongoid/threaded.rb', line 365

def validations
  Thread.current[:"[mongoid]:validations"] ||= {}
end

#validations_for(klass) ⇒ Array

Get all validations on the current thread for the class.

Examples:

Get all validations.

Threaded.validations_for(Person)

Parameters:

  • The (Class)

    class to check.

Returns:

  • (Array)

    The current validations.

Since:

  • 2.1.9



379
380
381
# File 'lib/mongoid/threaded.rb', line 379

def validations_for(klass)
  validations[klass] ||= []
end