Class: McDowall::TimeRemaining

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

Instance Method Summary collapse

Constructor Details

#initialize(min_data = 2, max_duration = 2) ⇒ TimeRemaining

Returns a new instance of TimeRemaining.



9
10
11
12
13
14
15
16
17
# File 'lib/time_remaining.rb', line 9

def initialize( min_data=2, max_duration=2 )
  @minimum_data  = min_data
  @maximum_ticks = max_duration * 10
  @queue = Containers::Queue.new
  @start_time = Time.now

  @current = {}
  @oldest  = {}
end

Instance Method Details

#add(progress) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/time_remaining.rb', line 24

def add progress
  clear_expired
  current_ticks = elapsed_ticks
  @current = {:current_ticks => current_ticks,:progress=>progress}

  @queue.push(@current);

  if @queue.size == 1
    @oldest = @current
  end
end

#completed_atObject



50
51
52
# File 'lib/time_remaining.rb', line 50

def completed_at
  Time.at(Time.now.to_i + completed_in)
end

#completed_inObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/time_remaining.rb', line 36

def completed_in
  oldest  = @oldest;
  current = @current;

  if (@queue.size < @minimum_data || oldest[:progress] == current[:progress])
    return 99999
  end

  #puts "(1.0 - #{current[:progress].to_f}) * ((#{current[:current_ticks]} - #{oldest[:current_ticks]}) / (#{current[:progress]} - #{oldest[:progress]}))"

  finishedInTicks = (1.0 - current[:progress].to_f) * ((current[:current_ticks] - oldest[:current_ticks]) / (current[:progress] - oldest[:progress]));
  finishedInTicks
end

#resetObject



19
20
21
22
# File 'lib/time_remaining.rb', line 19

def reset
  @queue.clear
  @start_time = nil
end

#time_remaining_available?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/time_remaining.rb', line 54

def time_remaining_available?
  return (@queue.size >= @minimum_data && @oldestcurrent[:progress] != @currentcurrent[:progress]);
end

#to_sObject



58
59
60
# File 'lib/time_remaining.rb', line 58

def to_s
  Time.diff( completed_at, Time.now-1 )[:diff]
end