Class: DynportTools::ETA
- Inherits:
-
Object
- Object
- DynportTools::ETA
- Defined in:
- lib/dynport_tools/eta.rb
Constant Summary collapse
- FACTORS =
[1, 60, 3600]
Instance Attribute Summary collapse
-
#current ⇒ Object
Returns the value of attribute current.
-
#started ⇒ Object
Returns the value of attribute started.
-
#total ⇒ Object
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
- #eta ⇒ Object
-
#initialize(options = {}) ⇒ ETA
constructor
A new instance of ETA.
- #pending ⇒ Object
- #per_second ⇒ Object
- #percs ⇒ Object
- #raise_error_when_current_or_total_not_set ⇒ Object
- #running_for ⇒ Object
- #seconds_to_time(time) ⇒ Object
- #to_go ⇒ Object
- #to_s ⇒ Object
- #total_time ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ETA
Returns a new instance of ETA.
4 5 6 7 8 |
# File 'lib/dynport_tools/eta.rb', line 4 def initialize( = {}) .each do |key, value| self.send(:"#{key}=", value) if self.respond_to?(:"#{key}=") end end |
Instance Attribute Details
#current ⇒ Object
Returns the value of attribute current.
2 3 4 |
# File 'lib/dynport_tools/eta.rb', line 2 def current @current end |
#started ⇒ Object
Returns the value of attribute started.
2 3 4 |
# File 'lib/dynport_tools/eta.rb', line 2 def started @started end |
#total ⇒ Object
Returns the value of attribute total.
2 3 4 |
# File 'lib/dynport_tools/eta.rb', line 2 def total @total end |
Class Method Details
.from_time_string(string, options = {}) ⇒ Object
66 67 68 |
# File 'lib/dynport_tools/eta.rb', line 66 def from_time_string(string, = {}) self.new(.merge(:started => Time.now - parse_time_string(string))) end |
.parse_time_string(string) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/dynport_tools/eta.rb', line 58 def parse_time_string(string) sum = 0.0 string.split(":").map { |s| s.to_i }.reverse.each_with_index do |value, i| sum += value * FACTORS[i] end sum end |
Instance Method Details
#eta ⇒ Object
32 33 34 |
# File 'lib/dynport_tools/eta.rb', line 32 def eta Time.now + to_go end |
#pending ⇒ Object
15 16 17 18 |
# File 'lib/dynport_tools/eta.rb', line 15 def pending raise_error_when_current_or_total_not_set total - current end |
#per_second ⇒ Object
36 37 38 |
# File 'lib/dynport_tools/eta.rb', line 36 def per_second current / running_for end |
#percs ⇒ Object
10 11 12 13 |
# File 'lib/dynport_tools/eta.rb', line 10 def percs raise_error_when_current_or_total_not_set current.to_f / total end |
#raise_error_when_current_or_total_not_set ⇒ Object
51 52 53 |
# File 'lib/dynport_tools/eta.rb', line 51 def raise_error_when_current_or_total_not_set raise "current and total must be set" if total.nil? || current.nil? end |
#running_for ⇒ Object
20 21 22 |
# File 'lib/dynport_tools/eta.rb', line 20 def running_for Time.now - started end |
#seconds_to_time(time) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/dynport_tools/eta.rb', line 40 def seconds_to_time(time) hours = time / 3600.0 minutes = time / 60.0 seconds = time "%02d:%02d:%02d" % [hours.floor, (minutes.floor % 3600) % 60, seconds.floor % 60] end |
#to_go ⇒ Object
28 29 30 |
# File 'lib/dynport_tools/eta.rb', line 28 def to_go total_time - running_for end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/dynport_tools/eta.rb', line 47 def to_s "%.2f%%, %.2f/second, ETA: %s" % [percs * 100, per_second, eta.iso8601] end |
#total_time ⇒ Object
24 25 26 |
# File 'lib/dynport_tools/eta.rb', line 24 def total_time running_for / percs end |