123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Timestamps::Timeless

Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable, ActiveSupport::Concern
Defined in: lib/mongoid/timestamps/timeless.rb

Overview

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

Constant Summary

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Class Attribute Details

.suppressing_timestamps?true | false (readonly)

Whether a block-based timeless scope is currently active on this thread/fiber.

Returns:

  • (true | false)

    Whether timestamps are being suppressed.

[ GitHub ]

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

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

Class Method Details

.set_suppressing_timestamps(value)

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

Parameters:

  • value (true | false)

    Whether to suppress timestamps.

[ GitHub ]

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

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

.timeless_tableHash

Returns the in-memory thread cache of classes for which to skip timestamping.

Returns:

  • (Hash)

    The timeless table.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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 Attribute Details

#timeless?true | false (readonly)

Returns whether the document should skip timestamping.

Returns:

  • (true | false)

    Whether the document should skip timestamping.

[ GitHub ]

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

def timeless?
  self.class.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.

[ GitHub ]

  
# 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 (readonly)

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.

[ GitHub ]

  
# File 'lib/mongoid/timestamps/timeless.rb', line 36

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

  self.class.timeless
  self
end