Module: Dbwatcher::Storage::Concerns::Timestampable

Included in:
BaseStorage
Defined in:
lib/dbwatcher/storage/concerns/timestampable.rb

Overview

Provides timestamping capabilities for storage objects

This concern adds created_at and updated_at functionality to storage objects, following Rails conventions for timestamp management.

Examples:

class SessionStorage < BaseStorage
  include Concerns::Timestampable
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 16

def self.included(base)
  base.attr_reader :created_at, :updated_at
end

Instance Method Details

#ageFloat

Calculates age since creation

Returns:

  • (Float)

    age in seconds since creation



39
40
41
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 39

def age
  current_time - created_at
end

#initialize_timestampsvoid

This method returns an undefined value.

Sets initial timestamps on creation



23
24
25
26
27
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 23

def initialize_timestamps
  now = current_time
  @created_at = now
  @updated_at = now
end

#recently_created?(threshold = 3600) ⇒ Boolean

Checks if the object was recently created

Parameters:

  • threshold (Integer) (defaults to: 3600)

    threshold in seconds (default: 1 hour)

Returns:

  • (Boolean)

    true if created within threshold



47
48
49
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 47

def recently_created?(threshold = 3600)
  age < threshold
end

#recently_updated?(threshold = 3600) ⇒ Boolean

Checks if the object was recently updated

Parameters:

  • threshold (Integer) (defaults to: 3600)

    threshold in seconds (default: 1 hour)

Returns:

  • (Boolean)

    true if updated within threshold



55
56
57
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 55

def recently_updated?(threshold = 3600)
  (current_time - updated_at) < threshold
end

#touch_updated_atTime

Updates the updated_at timestamp

Returns:

  • (Time)

    the new updated_at timestamp



32
33
34
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 32

def touch_updated_at
  @updated_at = current_time
end