class Spring::ProcessTitleUpdater

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.

Constants

HOUR
MINUTE
SECOND

Attributes

block[R]

Public Class Methods

new(start = Time.now, &block) click to toggle source
# File lib/spring/process_title_updater.rb, line 21
def initialize(start = Time.now, &block)
  @start = start
  @block = block
end
run(&block) click to toggle source
# 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

Public Instance Methods

distance_in_words(now = Time.now) click to toggle source
# 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
interval() click to toggle source
# 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
next() click to toggle source
# File lib/spring/process_title_updater.rb, line 38
def next
  sleep interval
  value
end
value() click to toggle source
# File lib/spring/process_title_updater.rb, line 43
def value
  block.call(distance_in_words)
end

Private Instance Methods

pluralize(amount, unit) click to toggle source
# File lib/spring/process_title_updater.rb, line 61
def pluralize(amount, unit)
  "#{amount.to_i} #{amount.to_i == 1 ? unit : "#{unit}s"}"
end