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.
Class Method Summary collapse
Instance Method Summary collapse
-
#age ⇒ Float
Calculates age since creation.
-
#initialize_timestamps ⇒ void
Sets initial timestamps on creation.
-
#recently_created?(threshold = 3600) ⇒ Boolean
Checks if the object was recently created.
-
#recently_updated?(threshold = 3600) ⇒ Boolean
Checks if the object was recently updated.
-
#touch_updated_at ⇒ Time
Updates the updated_at timestamp.
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
#age ⇒ Float
Calculates age since creation
39 40 41 |
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 39 def age current_time - created_at end |
#initialize_timestamps ⇒ void
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 now = current_time @created_at = now @updated_at = now end |
#recently_created?(threshold = 3600) ⇒ Boolean
Checks if the object was recently created
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
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_at ⇒ Time
Updates the updated_at timestamp
32 33 34 |
# File 'lib/dbwatcher/storage/concerns/timestampable.rb', line 32 def touch_updated_at @updated_at = current_time end |