Class: TrickBag::Timing::Elapser
- Inherits:
-
Object
- Object
- TrickBag::Timing::Elapser
- Defined in:
- lib/trick_bag/timing/elapser.rb
Overview
Very simple class that enables you to specify an elapsed time in either seconds or by the time itself.
Instance Attribute Summary collapse
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#never_elapse ⇒ Object
Returns the value of attribute never_elapse.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
Class Method Summary collapse
-
.from(object) ⇒ Object
Can be used to create an instance or return the passed instance (see test for example).
- .never_elapser ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #elapsed? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(seconds_or_end_time) ⇒ Elapser
constructor
Create the instance with the passed parameter.
Constructor Details
#initialize(seconds_or_end_time) ⇒ Elapser
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.
38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 |
Instance Attribute Details
#end_time ⇒ Object (readonly)
Returns the value of attribute end_time.
9 10 11 |
# File 'lib/trick_bag/timing/elapser.rb', line 9 def end_time @end_time end |
#never_elapse ⇒ Object
Returns the value of attribute never_elapse.
10 11 12 |
# File 'lib/trick_bag/timing/elapser.rb', line 10 def never_elapse @never_elapse end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
9 10 11 |
# File 'lib/trick_bag/timing/elapser.rb', line 9 def seconds @seconds end |
Class Method Details
.from(object) ⇒ Object
Can be used to create an instance or return the passed instance (see test for example).
22 23 24 25 26 27 28 29 30 31 |
# 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 ⇒ Object
12 13 14 15 16 17 18 |
# 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 |
Instance Method Details
#==(other) ⇒ Object
61 62 63 |
# 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? ⇒ Boolean
51 52 53 |
# File 'lib/trick_bag/timing/elapser.rb', line 51 def elapsed? never_elapse ? false : Time.now >= @end_time end |
#hash ⇒ Object
56 57 58 |
# File 'lib/trick_bag/timing/elapser.rb', line 56 def hash Integer(@seconds - @end_time.to_i) end |