Module: ConcernsOnRails::Models::Expirable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/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).
- #expiry_extension_base ⇒ Object
-
#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
52 53 54 |
# File 'lib/concerns_on_rails/models/expirable.rb', line 52 def active? !expired? end |
#expire!(time = Time.zone.now) ⇒ Object
64 65 66 |
# File 'lib/concerns_on_rails/models/expirable.rb', line 64 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).
57 58 59 60 61 62 |
# File 'lib/concerns_on_rails/models/expirable.rb', line 57 def expired? value = self[self.class.expirable_field] return false if value.nil? value <= Time.zone.now end |
#expiry_extension_base ⇒ Object
87 88 89 90 91 |
# File 'lib/concerns_on_rails/models/expirable.rb', line 87 def expiry_extension_base value = self[self.class.expirable_field] now = Time.zone.now value.nil? || value <= now ? now : value 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.
71 72 73 |
# File 'lib/concerns_on_rails/models/expirable.rb', line 71 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.
77 78 79 80 81 82 83 84 85 |
# File 'lib/concerns_on_rails/models/expirable.rb', line 77 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 |