Class: Clocker

Inherits:
Object
  • Object
show all
Defined in:
lib/clocker.rb,
lib/clocker/version.rb

Overview

lib/clocker/version.rb Version of Clocker

Constant Summary collapse

VERSION =
'1.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Clocker

Returns a new instance of Clocker.



7
8
9
# File 'lib/clocker.rb', line 7

def initialize(options = {})
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/clocker.rb', line 5

def options
  @options
end

Instance Method Details

#clock(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/clocker.rb', line 11

def clock(&block)
  @start = Time.now

  if options[:show_messages]
    puts "\nstart: #{@start}"
  end

  begin
    block.call
  rescue
    puts "ERROR: invalid command or block"
  end

  return stop
end

#stopObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/clocker.rb', line 27

def stop
  @stop = Time.now

  if options[:show_messages]
    puts "\nended: #{@stop}"
  end

  ms = ((@stop - @start) * 1000).to_i

  if ms > 60000
    mins = ms / 60000
    ms = ms - (60000 * mins)
    if ms > 1000
      secs = ms / 1000
      ms = ms % 1000
    end

    return { mins: mins, secs: secs, ms: ms }
  elsif ms > 1000
    secs = ms / 1000
    ms = ms % 1000

    return { mins: 0, secs: secs, ms: ms }
  else
    return { mins: 0, secs: 0, ms: ms }
  end
end