Module: Mongoid::Timestamps::Timeless

Extended by:
ActiveSupport::Concern, Forwardable
Defined in:
lib/mongoid/timestamps/timeless.rb

Overview

This module adds behavior for turning off timestamping in single or multiple calls.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

TIMELESS_TABLE_KEY =

The key to use to store the timeless table

'[mongoid]:timeless'
TIMELESS_FLAG_KEY =

The key to use to store the block-based timeless flag.

'[mongoid]:timeless-flag'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.set_suppressing_timestamps(value) ⇒ Object

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.

Set whether a block-based timeless scope is active on this thread/fiber.

Parameters:

  • value (true | false)

    Whether to suppress timestamps.



111
112
113
# File 'lib/mongoid/timestamps/timeless.rb', line 111

def set_suppressing_timestamps(value)
  Threaded.set(TIMELESS_FLAG_KEY, value)
end

.suppressing_timestamps?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 a block-based timeless scope is currently active on this thread/fiber.

Returns:

  • (true | false)

    Whether timestamps are being suppressed.



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

def suppressing_timestamps?
  !!Threaded.get(TIMELESS_FLAG_KEY) { false }
end

.timeless_tableHash

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 the in-memory thread cache of classes for which to skip timestamping.

Returns:

  • (Hash)

    The timeless table.



66
67
68
# File 'lib/mongoid/timestamps/timeless.rb', line 66

def timeless_table
  Threaded.get(TIMELESS_TABLE_KEY) { {} }
end

.with_timelessObject

Skip timestamping for the duration of the given block, on the current thread or fiber. This applies to every document persisted while the block is executing, regardless of class, including cascaded embedded children at any nesting depth.

Examples:

Skip timestamping for a block.

Mongoid::Timestamps::Timeless.with_timeless do
  person.save
end

Returns:

  • (Object)

    The return value of the block.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mongoid/timestamps/timeless.rb', line 83

def with_timeless
  # Only the outermost block owns the flag: if we are already inside a
  # timeless scope, we leave the suppression in place when this block
  # ends. This avoids tracking a nesting depth that could drift out of
  # sync.
  already_timeless = suppressing_timestamps?
  set_suppressing_timestamps(true) unless already_timeless
  yield
ensure
  set_suppressing_timestamps(false) unless already_timeless
end

Instance Method Details

#clear_timeless_optiontrue

Clears out the timeless option.

Examples:

Clear the timeless option.

document.clear_timeless_option

Returns:

  • (true)

    True.



16
17
18
19
20
21
22
23
# File 'lib/mongoid/timestamps/timeless.rb', line 16

def clear_timeless_option
  if persisted?
    self.class.clear_timeless_option_on_update
  else
    self.class.clear_timeless_option
  end
  true
end

#timeless(&block) ⇒ Object | Document

Skip timestamping for the duration of the given block, or (in the deprecated, block-less form) for the next persistence operation.

Examples:

Save a document but don't timestamp (block form).

person.timeless { person.save }

Save a document but don't timestamp (deprecated chained form).

person.timeless.save

Returns:

  • (Object | Document)

    The return value of the block, or (in the block-less form) the document this was called on.



36
37
38
39
40
41
# File 'lib/mongoid/timestamps/timeless.rb', line 36

def timeless(&block)
  return Timeless.with_timeless(&block) if block

  self.class.timeless
  self
end

#timeless?true | false

Returns whether the document should skip timestamping.

Returns:

  • (true | false)

    Whether the document should skip timestamping.



47
48
49
# File 'lib/mongoid/timestamps/timeless.rb', line 47

def timeless?
  self.class.timeless?
end