Class: EasyTimers::Timers
- Inherits:
-
Object
- Object
- EasyTimers::Timers
- Defined in:
- lib/easy_timers/timers.rb
Instance Method Summary collapse
- #after(time, name = nil, &block) ⇒ Object
- #after_then_every(start, interval, name = nil, &block) ⇒ Object
-
#cancel(name) ⇒ Object
Cancel a timer.
-
#every(seconds, name = nil, &block) ⇒ Symbol
Schedule a block to fire every given amount of seconds.
-
#initialize ⇒ Timers
constructor
Create a new Timers instance to hold our timers.
Constructor Details
Instance Method Details
#after(time, name = nil, &block) ⇒ Symbol #after(seconds, name = nil, &block) ⇒ Symbol
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/easy_timers/timers.rb', line 27 def after(time, name = nil, &block) if time.is_a?(Time) time = time.gmtime.to_f elsif time.is_a?(Numeric) time = Time.now.gmtime.to_f + time.to_f else raise ArgumentError, "Invalid arguments to method." end timer = Timer.new(time, name, 0, false, block) name = @group.insert(timer) return name end |
#after_then_every(start_date, interval, name = nil, &block) ⇒ Symbol #after_then_every(start_seconds, interval, name = nil, &block) ⇒ Symbol
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/easy_timers/timers.rb', line 76 def after_then_every(start, interval, name = nil, &block) if start.is_a?(Time) && interval.is_a?(Numeric) && block_given? time = start.gmtime.to_f elsif start.is_a?(Numeric) && interval.is_a?(Numeric) && block_given? time = Time.now.gmtime.to_f + start.to_f else raise ArgumentError, "Invalid arguments." end timer = Timer.new(time, name, interval.to_f, true, block) name = @group.insert(timer) return name end |
#cancel(name) ⇒ Object
Cancel a timer.
93 94 95 |
# File 'lib/easy_timers/timers.rb', line 93 def cancel(name) @group.delete(name) end |
#every(seconds, name = nil, &block) ⇒ Symbol
Schedule a block to fire every given amount of seconds
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/easy_timers/timers.rb', line 48 def every(seconds, name = nil, &block) if seconds <= 0 # zero disallowed because you can't run it every 0 seconds. raise ArgumentError, "Interval must be greater than 0." end interval = seconds.to_f time = Time.now.gmtime.to_f + interval timer = Timer.new(time, name, interval, true, block) name = @group.insert(timer) return name end |