Module: Mongoid::Timestamps::Timeless::ClassMethods

Defined in:
lib/mongoid/timestamps/timeless.rb

Instance Method Summary collapse

Instance Method Details

#clear_timeless_optiontrue

Removes the timeless option on the current class.

Returns:

  • (true)

    Always true.



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

def clear_timeless_option
  if counter = Timeless[name]
    counter -= 1
    set_timeless_counter(counter)
  end
  true
end

#clear_timeless_option_on_updatetrue

Sets to remove the timeless option when the next instance of the current class is updated.

Returns:

  • (true)

    Always true.



95
96
97
98
99
100
101
# File 'lib/mongoid/timestamps/timeless.rb', line 95

def clear_timeless_option_on_update
  if counter = Timeless[name]
    counter -= 1 if self < Mongoid::Timestamps::Created
    counter -= 1 if self < Mongoid::Timestamps::Updated
    set_timeless_counter(counter)
  end
end

#set_timeless_counter(counter) ⇒ Integer | nil

Clears the timeless counter for the current class if the value has reached zero.

Parameters:

  • counter (Integer)

    The counter value.

Returns:

  • (Integer | nil)

    The counter value, or nil if the counter was cleared.



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

def set_timeless_counter(counter)
  Timeless[name] = (counter == 0) ? nil : counter
end

#timelessClass

Begin an execution that should skip timestamping.

Examples:

Create a document but don’t timestamp.

Person.timeless.create(:title => "Sir")

Returns:

  • (Class)

    The class this was called on.



72
73
74
75
76
77
78
# File 'lib/mongoid/timestamps/timeless.rb', line 72

def timeless
  counter = 0
  counter += 1 if self < Mongoid::Timestamps::Created
  counter += 1 if self < Mongoid::Timestamps::Updated
  Timeless[name] = counter
  self
end

#timeless?true | false

Returns whether the current class should skip timestamping.

Returns:

  • (true | false)

    Whether the current class should skip timestamping.



118
119
120
# File 'lib/mongoid/timestamps/timeless.rb', line 118

def timeless?
  !!Timeless[name]
end