clockblock 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,93 @@
1
- clockblock
1
+ ClockBlock
2
2
  ==========
3
3
 
4
- Wrap your code in a Clock Block to measure execution duration.
4
+ Wrap your code in a Clock Block to measure execution duration.
5
+
6
+ ````ruby
7
+ require 'clockblock'
8
+
9
+ class Foo
10
+ extend Clockblock::Timing
11
+
12
+ def bar
13
+ sleep 2
14
+ end
15
+ add_timing_to :bar
16
+
17
+ end
18
+
19
+ f = Foo.new
20
+ f.bar
21
+
22
+ f.clockblock_timers
23
+ # => {:bar=>{:started_at=>2012-07-26 16:04:03 -0400, :finished_at=>2012-07-26 16:04:05 -0400, :duration=>2.001059, :stage=>:finished}}
24
+
25
+ ````
26
+
27
+ ````ruby
28
+ require 'clockblock'
29
+
30
+ class Foo
31
+ def bar
32
+ sleep 2
33
+ end
34
+ end
35
+
36
+ f = Foo.new
37
+ f.extend Clockblock::Timing
38
+ f.add_timing_to :bar
39
+ f.bar
40
+
41
+ f.clockblock_timers
42
+ # => {:bar=>{:started_at=>2012-07-26 16:04:03 -0400, :finished_at=>2012-07-26 16:04:05 -0400, :duration=>2.001059, :stage=>:finished}}
43
+
44
+ ````
45
+
46
+ ````ruby
47
+ class Foo1
48
+ extend Clockblock::Timing
49
+ add_timing_to :baz
50
+
51
+ def bar
52
+ sleep 1
53
+ end
54
+ add_timing_to :bar
55
+
56
+ def baz
57
+ sleep 1
58
+ end
59
+ end
60
+
61
+ class Foo2
62
+ def bar
63
+ sleep 1
64
+ end
65
+ end
66
+
67
+ class Foo3
68
+ def bar
69
+ sleep 1
70
+ end
71
+ end
72
+
73
+ f1 = Foo1.new
74
+ f1.bar
75
+ puts f1.clockblock_timers
76
+ puts Foo1.instance_variable_get(:@future_timer_methods)
77
+
78
+ f2 = Foo2.new
79
+ f2.extend Clockblock::Timing
80
+ f2.add_timing_to :bar, :baz
81
+ f2.bar
82
+ puts f2.clockblock_timers
83
+ puts f2.instance_variable_get(:@future_timer_methods)
84
+
85
+ Foo3.extend Clockblock::Timing
86
+ Foo3.add_timing_to :bar, :baz
87
+ puts Foo3.instance_variable_get(:@future_timer_methods)
88
+ class Foo3; def baz; sleep 1; end; end
89
+ f3 = Foo3.new
90
+ f3.bar
91
+ f3.baz
92
+ puts f3.clockblock_timers
93
+ ````
data/lib/clockblock.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "clockblock/version"
2
2
  require "clockblock/timer"
3
+ require "clockblock/timing"
3
4
 
4
5
  module Clockblock
5
6
  # Your code goes here...
@@ -0,0 +1,50 @@
1
+ module Clockblock
2
+ module Timing
3
+
4
+ module ClassMethods
5
+ def add_timing_to *methods
6
+ methods.each do |method|
7
+ klass = self.is_a?(Class) ? self : (class << self; self; end)
8
+ if klass.method_defined? method
9
+ add_timing_to_method method, klass
10
+ else
11
+ @future_timer_methods ||= []
12
+ @future_timer_methods << method
13
+ end
14
+ end
15
+ end
16
+
17
+ def add_timing_to_method(method, klass)
18
+ stashed_method = "#{method}_stashed_by_clockblock".to_sym
19
+ klass.class_eval do
20
+ alias_method stashed_method, method
21
+ define_method method do |*args, &block|
22
+ @clockblock_timers ||= {}
23
+ @clockblock_timers[method] = Clockblock::Timer.new
24
+
25
+ @clockblock_timers[method].clock(method) do
26
+ send stashed_method, *args, &block
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def method_added(method)
33
+ if @future_timer_methods && @future_timer_methods.include?(method)
34
+ unless @added_by_clockblock
35
+ @added_by_clockblock = true
36
+ klass = self.is_a?(Class) ? self : (class << self; self; end)
37
+ add_timing_to_method method, klass
38
+ end
39
+ @added_by_clockblock = false
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.extended(base)
45
+ (base.is_a?(Class) ? base : base.class).send :attr_accessor, :clockblock_timers, :future_timer_methods
46
+ base.send :extend, ClassMethods
47
+ end
48
+
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module Clockblock
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clockblock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-26 00:00:00.000000000 Z
12
+ date: 2012-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70101676678120 !ruby/object:Gem::Requirement
16
+ requirement: &70341854789020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70101676678120
24
+ version_requirements: *70341854789020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &70101676677580 !ruby/object:Gem::Requirement
27
+ requirement: &70341854773700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70101676677580
35
+ version_requirements: *70341854773700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: turn
38
- requirement: &70101676676840 !ruby/object:Gem::Requirement
38
+ requirement: &70341854772740 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70101676676840
46
+ version_requirements: *70341854772740
47
47
  description: Enables easy and DRY code timing capabilities.
48
48
  email:
49
49
  - jack.ross@technekes.com
@@ -59,6 +59,7 @@ files:
59
59
  - clockblock.gemspec
60
60
  - lib/clockblock.rb
61
61
  - lib/clockblock/timer.rb
62
+ - lib/clockblock/timing.rb
62
63
  - lib/clockblock/version.rb
63
64
  - spec/lib/clockblock/timer_spec.rb
64
65
  - spec/spec_helper.rb
@@ -82,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
83
  version: '0'
83
84
  requirements: []
84
85
  rubyforge_project:
85
- rubygems_version: 1.8.15
86
+ rubygems_version: 1.8.17
86
87
  signing_key:
87
88
  specification_version: 3
88
89
  summary: Wrap your code in a Clock Block to measure execution duration.