class TrickBag::Timing::Elapser

Very simple class that enables you to specify an elapsed time in either seconds or by the time itself.

Attributes

end_time[R]
never_elapse[RW]
seconds[R]

Public Class Methods

from(object) click to toggle source

Can be used to create an instance or return the passed instance (see test for example).

# File lib/trick_bag/timing/elapser.rb, line 22
def self.from(object)
  case object
    when :never
      never_elapser
    when self
      object
    else
      new(object)
  end
end
never_elapser() click to toggle source
# File lib/trick_bag/timing/elapser.rb, line 12
def self.never_elapser
  @never_elapser ||= begin
     instance = new(0)
     instance.never_elapse = true
     instance
  end
end
new(seconds_or_end_time) click to toggle source

Create the instance with the passed parameter. If it's a Time instance, it is assumed to be the end time at which elapsed? should return true. If it's a number, it's assumed to be a number of seconds after which elapsed? should return true.

# File lib/trick_bag/timing/elapser.rb, line 38
def initialize(seconds_or_end_time)
  case seconds_or_end_time
    when Time
      @end_time = seconds_or_end_time
      @seconds = @end_time - Time.now
    when ::Numeric
      @seconds = seconds_or_end_time
      @end_time = Time.now + @seconds
    else
      raise ArgumentError.new("Invalid parameter class: #{seconds_or_end_time.class}, object: #{seconds_or_end_time}")
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/trick_bag/timing/elapser.rb, line 61
def ==(other)
  other.class == self.class && other.seconds == self.seconds && other.end_time == self.end_time
end
elapsed?() click to toggle source
# File lib/trick_bag/timing/elapser.rb, line 51
def elapsed?
  never_elapse ? false : Time.now >= @end_time
end
hash() click to toggle source
# File lib/trick_bag/timing/elapser.rb, line 56
def hash
  Integer(@seconds - @end_time.to_i)
end