Class: DynportTools::ETA

Inherits:
Object
  • Object
show all
Defined in:
lib/dynport_tools/eta.rb

Constant Summary collapse

FACTORS =
[1, 60, 3600]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  options.each do |key, value|
    self.send(:"#{key}=", value) if self.respond_to?(:"#{key}=")
  end
end

Instance Attribute Details

#currentObject

Returns the value of attribute current.



2
3
4
# File 'lib/dynport_tools/eta.rb', line 2

def current
  @current
end

#startedObject

Returns the value of attribute started.



2
3
4
# File 'lib/dynport_tools/eta.rb', line 2

def started
  @started
end

#totalObject

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, options = {})
  self.new(options.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

#etaObject



32
33
34
# File 'lib/dynport_tools/eta.rb', line 32

def eta
  Time.now + to_go
end

#pendingObject



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_secondObject



36
37
38
# File 'lib/dynport_tools/eta.rb', line 36

def per_second
  current / running_for
end

#percsObject



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_setObject



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_forObject



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_goObject



28
29
30
# File 'lib/dynport_tools/eta.rb', line 28

def to_go
  total_time - running_for
end

#to_sObject



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_timeObject



24
25
26
# File 'lib/dynport_tools/eta.rb', line 24

def total_time
  running_for / percs
end