Class: Spring::ProcessTitleUpdater

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

Overview

Yes, I know this reimplements a bunch of stuff in Active Support, but I don’t want the spring client to depend on AS, in order to keep its load time down.

Constant Summary collapse

SECOND =
1
MINUTE =
60
HOUR =
60*60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start = Time.now, &block) ⇒ ProcessTitleUpdater

Returns a new instance of ProcessTitleUpdater.



21
22
23
24
# File 'lib/spring/process_title_updater.rb', line 21

def initialize(start = Time.now, &block)
  @start = start
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



19
20
21
# File 'lib/spring/process_title_updater.rb', line 19

def block
  @block
end

Class Method Details

.run(&block) ⇒ Object



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

def self.run(&block)
  updater = new(&block)

  Thread.new {
    $0 = updater.value
    loop { $0 = updater.next }
  }
end

Instance Method Details

#distance_in_words(now = Time.now) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spring/process_title_updater.rb', line 47

def distance_in_words(now = Time.now)
  distance = now - @start

  if distance < MINUTE
    pluralize(distance, "sec")
  elsif distance < HOUR
    pluralize(distance / MINUTE, "min")
  else
    pluralize(distance / HOUR, "hour")
  end
end

#intervalObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spring/process_title_updater.rb', line 26

def interval
  distance = Time.now - @start

  if distance < MINUTE
    SECOND
  elsif distance < HOUR
    MINUTE
  else
    HOUR
  end
end

#nextObject



38
39
40
41
# File 'lib/spring/process_title_updater.rb', line 38

def next
  sleep interval
  value
end

#valueObject



43
44
45
# File 'lib/spring/process_title_updater.rb', line 43

def value
  block.call(distance_in_words)
end