Module: Mongoid::Threaded

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

Overview

:nodoc:

Defined Under Namespace

Modules: Lifecycle

Instance Method Summary collapse

Instance Method Details

#begin(name) ⇒ true

Begin entry into a named thread local stack.

Examples:

Begin entry into the stack.

Threaded.begin(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (true)

    True.

Since:

  • 2.4.0



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

def begin(name)
  stack(name).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



75
76
77
# File 'lib/mongoid/threaded.rb', line 75

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

#clear_options!Object

Clear out all options set on a one-time basis.

Examples:

Clear out the options.

Threaded.clear_options!

Since:

  • 2.3.0



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

def clear_options!
  clear_safety_options!
  self.timeless = false
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



87
88
89
# File 'lib/mongoid/threaded.rb', line 87

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

#executing?(name) ⇒ true

Are in the middle of executing the named stack

Examples:

Are we in the stack execution?

Threaded.executing?(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (true)

    If the stack is being executed.

Since:

  • 2.4.0



35
36
37
# File 'lib/mongoid/threaded.rb', line 35

def executing?(name)
  !stack(name).empty?
end

#exit(name) ⇒ true

Exit from a named thread local stack.

Examples:

Exit from the stack.

Threaded.exit(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (true)

    True.

Since:

  • 2.4.0



49
50
51
# File 'lib/mongoid/threaded.rb', line 49

def exit(name)
  stack(name).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



110
111
112
# File 'lib/mongoid/threaded.rb', line 110

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



122
123
124
# File 'lib/mongoid/threaded.rb', line 122

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

#insert(name) ⇒ Object

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



134
135
136
# File 'lib/mongoid/threaded.rb', line 134

def insert(name)
  Thread.current[:"[mongoid][#{name}]:insert-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



160
161
162
# File 'lib/mongoid/threaded.rb', line 160

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



174
175
176
# File 'lib/mongoid/threaded.rb', line 174

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



186
187
188
# File 'lib/mongoid/threaded.rb', line 186

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

#set_insert(name, 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



148
149
150
# File 'lib/mongoid/threaded.rb', line 148

def set_insert(name, consumer)
  Thread.current[:"[mongoid][#{name}]:insert-consumer"] = consumer
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



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

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

#stack(name) ⇒ Array

Get the named stack.

Examples:

Get a stack by name

Threaded.stack(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

Since:

  • 2.4.0



63
64
65
# File 'lib/mongoid/threaded.rb', line 63

def stack(name)
  Thread.current[:"[mongoid]:#{name}-stack"] ||= []
end

#timelesstrue, false

Get the value of the one-off timeless call.

Examples:

Get the timeless value.

Threaded.timeless

Returns:

  • (true, false)

    The timeless setting.

Since:

  • 2.3.0



198
199
200
# File 'lib/mongoid/threaded.rb', line 198

def timeless
  !!Thread.current[:"[mongoid]:timeless"]
end

#timeless=(value) ⇒ Object

Set the value of the one-off timeless call.

Examples:

Set the timeless value.

Threaded.timeless = true

Parameters:

  • value (true, false)

    The value.

Since:

  • 2.3.0



210
211
212
# File 'lib/mongoid/threaded.rb', line 210

def timeless=(value)
  Thread.current[:"[mongoid]:timeless"] = value
end

#timestamping?true, false

Is the current thread setting timestamps?

Examples:

Is the current thread timestamping?

Threaded.timestamping?

Returns:

  • (true, false)

    If timestamps can be applied.

Since:

  • 2.3.0



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

def timestamping?
  !timeless
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



222
223
224
# File 'lib/mongoid/threaded.rb', line 222

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



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

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



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

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



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

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