Module: ConcernsOnRails::Expirable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/expirable.rb
Constant Summary collapse
- DEFAULT_FIELD =
:expires_at
Instance Method Summary collapse
- #active? ⇒ Boolean
- #expire!(time = Time.zone.now) ⇒ Object
-
#expired? ⇒ Boolean
nil means never expires; equal-to-now is treated as expired (exclusive boundary).
-
#extend_expiry!(by:) ⇒ Object
Push expiry forward by ‘by:`.
-
#time_until_expiry ⇒ Object
Returns an ActiveSupport::Duration of how long until expiry, or nil when there’s no expiry set, or 0.seconds when already expired.
Instance Method Details
#active? ⇒ Boolean
42 43 44 |
# File 'lib/concerns_on_rails/expirable.rb', line 42 def active? !expired? end |
#expire!(time = Time.zone.now) ⇒ Object
54 55 56 |
# File 'lib/concerns_on_rails/expirable.rb', line 54 def expire!(time = Time.zone.now) update(self.class.expirable_field => time) end |
#expired? ⇒ Boolean
nil means never expires; equal-to-now is treated as expired (exclusive boundary).
47 48 49 50 51 52 |
# File 'lib/concerns_on_rails/expirable.rb', line 47 def expired? value = self[self.class.expirable_field] return false if value.nil? value <= Time.zone.now end |
#extend_expiry!(by:) ⇒ Object
Push expiry forward by ‘by:`. If the record has no expiry yet, or has already expired, the new expiry is `now + by`. Otherwise it’s added to the existing expiry.
61 62 63 |
# File 'lib/concerns_on_rails/expirable.rb', line 61 def extend_expiry!(by:) update(self.class.expirable_field => expiry_extension_base + by) end |
#time_until_expiry ⇒ Object
Returns an ActiveSupport::Duration of how long until expiry, or nil when there’s no expiry set, or 0.seconds when already expired.
67 68 69 70 71 72 73 74 75 |
# File 'lib/concerns_on_rails/expirable.rb', line 67 def time_until_expiry value = self[self.class.expirable_field] return nil if value.nil? now = Time.zone.now return 0.seconds if value <= now (value - now).seconds end |