File: timer.rb

package info (click to toggle)
ruby-cabin 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: ruby: 1,306; makefile: 12
file content (25 lines) | stat: -rw-r--r-- 650 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require "cabin/namespace"
require "cabin/timer"

module Cabin::Mixins::Timer
  # Start timing something.
  # Returns an instance of Cabin::Timer bound to this Cabin::Channel.
  # To stop the timer and immediately emit the result to this channel, invoke
  # the Cabin::Timer#stop method.
  def time(data, &block)
    # TODO(sissel): need to refactor string->hash shoving.
    data = dataify(data)

    timer = Cabin::Timer.new do |duration|
      data[:duration] = duration
      publish(data)
    end

    if block_given?
      block.call
      return timer.stop
    else
      return timer
    end
  end # def time
end # module Cabin::Mixins::Timer