Sha256: c96da6cd57a423e92c4f79952f6d39b63437d770ed6112f57ec85281fe748aec

Contents?: true

Size: 1.2 KB

Versions: 39

Compression:

Stored size: 1.2 KB

Contents

module Spring
  # 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.
  class ProcessTitleUpdater
    SECOND = 1
    MINUTE = 60
    HOUR   = 60*60

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

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

    attr_reader :block

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

    def interval
      distance = Time.now - @start

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

    def next
      sleep interval
      value
    end

    def value
      block.call(distance_in_words)
    end

    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

    private

    def pluralize(amount, unit)
      "#{amount.to_i} #{amount.to_i == 1 ? unit : "#{unit}s"}"
    end
  end
end

Version data entries

39 entries across 39 versions & 2 rubygems

Version Path
spring-1.7.1 lib/spring/process_title_updater.rb
spring-1.7.0 lib/spring/process_title_updater.rb
spring-1.6.4 lib/spring/process_title_updater.rb
spring-1.6.3 lib/spring/process_title_updater.rb
spring-1.6.2 lib/spring/process_title_updater.rb
spring-1.6.1 lib/spring/process_title_updater.rb
spring-1.6.0 lib/spring/process_title_updater.rb
spring-1.5.0 lib/spring/process_title_updater.rb
spring-1.4.4 lib/spring/process_title_updater.rb
spring-jruby-1.4.3 lib/spring-jruby/process_title_updater.rb
spring-1.4.3 lib/spring/process_title_updater.rb
spring-1.4.2 lib/spring/process_title_updater.rb
spring-1.4.1 lib/spring/process_title_updater.rb
spring-1.4.0 lib/spring/process_title_updater.rb
spring-1.3.6 lib/spring/process_title_updater.rb
spring-1.3.5 lib/spring/process_title_updater.rb
spring-1.3.4 lib/spring/process_title_updater.rb
spring-1.3.3 lib/spring/process_title_updater.rb
spring-1.3.2 lib/spring/process_title_updater.rb
spring-1.3.1 lib/spring/process_title_updater.rb