Last active
August 29, 2015 14:08
-
-
Save valikos/8bb92e25a31c48a0fbea to your computer and use it in GitHub Desktop.
Class implements 'time to live' functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'singleton' | |
require 'active_support' | |
class TimeToLive | |
include Singleton | |
attr_reader :start_time | |
def initialize | |
@live = true | |
@start_time = Time.now | |
end | |
def time_to_die? | |
if expired? && alive? | |
@live = false | |
raise StandardError.new('Time to die') | |
end | |
end | |
private | |
def alive? | |
@live | |
end | |
def expired? | |
(@start_time + expiration_value) < Time.now | |
end | |
def expiration_value | |
2.minutes | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For fun ^_^