clockblock 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -63,7 +63,7 @@ end
63
63
  Foo.extend Clockblock::Timing
64
64
  Foo.add_timing_to :bar, :baz
65
65
  puts Foo.instance_variable_get(:@future_timer_methods)
66
- class Foo; def baz; sleep 1; end; end
66
+ class Foo; def baz; sleep 2; end; end
67
67
  f = Foo.new
68
68
  f.bar
69
69
  f.baz
@@ -3,7 +3,22 @@ module Clockblock
3
3
 
4
4
  module ClassMethods
5
5
  def receiver
6
- self.is_a?(Class) ? self : (class << self; self; end)
6
+ self.is_a?(Class) ? self : self.singleton_class #(class << self; self; end)
7
+ end
8
+
9
+ def add_timing_to_method(method)
10
+ stashed_method = "#{method}_stashed_by_clockblock".to_sym
11
+ receiver.class_eval do
12
+ alias_method stashed_method, method
13
+ define_method method do |*args, &block|
14
+ @clockblock_timers ||= {}
15
+ @clockblock_timers[method] = Clockblock::Timer.new
16
+
17
+ @clockblock_timers[method].clock(method) do
18
+ send stashed_method, *args, &block
19
+ end
20
+ end
21
+ end
7
22
  end
8
23
 
9
24
  def add_timing_to *methods
@@ -17,30 +32,25 @@ module Clockblock
17
32
  end
18
33
  end
19
34
 
20
- def method_added(method)
35
+ def future_method_added(method)
21
36
  if @future_timer_methods && @future_timer_methods.include?(method)
22
37
  unless @added_by_clockblock
23
38
  @added_by_clockblock = true
39
+ @future_timer_methods -= [method]
24
40
  add_timing_to_method method
25
41
  end
26
42
  @added_by_clockblock = false
27
43
  end
28
44
  end
29
45
 
30
- def add_timing_to_method(method)
31
- stashed_method = "#{method}_stashed_by_clockblock".to_sym
32
- receiver.class_eval do
33
- alias_method stashed_method, method
34
- define_method method do |*args, &block|
35
- @clockblock_timers ||= {}
36
- @clockblock_timers[method] = Clockblock::Timer.new
46
+ def method_added(method)
47
+ future_method_added method
48
+ end
37
49
 
38
- @clockblock_timers[method].clock(method) do
39
- send stashed_method, *args, &block
40
- end
41
- end
42
- end
50
+ def singleton_method_added(method)
51
+ future_method_added method
43
52
  end
53
+
44
54
  end
45
55
 
46
56
  def self.extended(base)
@@ -1,3 +1,3 @@
1
1
  module Clockblock
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,15 +1,18 @@
1
1
  require File.expand_path('../../../spec_helper', __FILE__)
2
2
 
3
3
  describe Clockblock::Timing do
4
- describe "extending Clockblock::Timing in a class definition" do
5
- let(:ret_val) { 42 }
4
+ let(:ret_val) { 42 }
6
5
 
7
- def run_timing_specs(f)
8
- f.bar(ret_val).must_equal ret_val
6
+ def run_timing_specs_on(f, *methods)
7
+ methods.each do |method|
8
+ f.send(method, ret_val).must_equal ret_val
9
9
  f.clockblock_timers.must_be_kind_of Hash
10
- f.clockblock_timers.keys.must_include :bar
11
- f.clockblock_timers[:bar].must_be_instance_of Clockblock::Timer
10
+ f.clockblock_timers.keys.must_include method
11
+ f.clockblock_timers[method].must_be_instance_of Clockblock::Timer
12
12
  end
13
+ end
14
+
15
+ describe "extending Clockblock::Timing in a class definition" do
13
16
 
14
17
  it "wraps an already defined method in a clock block" do
15
18
 
@@ -23,11 +26,11 @@ describe Clockblock::Timing do
23
26
 
24
27
  end
25
28
 
26
- run_timing_specs foo_klass.new
29
+ run_timing_specs_on foo_klass.new, :bar
27
30
 
28
31
  end
29
32
 
30
- it "wraps an undefined method in a clock block" do
33
+ it "eventually wraps an as yet undefined method in a clock block" do
31
34
 
32
35
  foo_klass = Class.new do
33
36
  extend Clockblock::Timing
@@ -39,7 +42,27 @@ describe Clockblock::Timing do
39
42
 
40
43
  end
41
44
 
42
- run_timing_specs foo_klass.new
45
+ run_timing_specs_on foo_klass.new, :bar
46
+
47
+ end
48
+
49
+ it "wraps multiple methods in a clock block" do
50
+
51
+ foo_klass = Class.new do
52
+ extend Clockblock::Timing
53
+ add_timing_to :bar, :baz
54
+
55
+ def bar(ret_val)
56
+ sleep 0.1; ret_val
57
+ end
58
+
59
+ def baz(ret_val)
60
+ sleep 0.1; ret_val
61
+ end
62
+
63
+ end
64
+
65
+ run_timing_specs_on foo_klass.new, :bar, :baz
43
66
 
44
67
  end
45
68
 
@@ -57,25 +80,73 @@ describe Clockblock::Timing do
57
80
  end
58
81
  end
59
82
 
60
- run_timing_specs foo_klass.new
83
+ run_timing_specs_on foo_klass.new, :bar
61
84
 
62
85
  end
63
86
 
64
87
  it "class can be extended 'after the fact' and still wrap a method defined in a clock block" do
65
88
 
66
- foo_klass = Class.new do
89
+ foo_klass_a = Class.new do
67
90
  def bar(ret_val)
68
91
  sleep 0.1; ret_val
69
92
  end
70
93
  end
71
94
 
72
- foo_klass.class_eval do
95
+ foo_klass_b = Class.new do
96
+ def bar(ret_val)
97
+ sleep 0.1; ret_val
98
+ end
99
+ end
100
+
101
+ foo_klass_a.class_eval do
73
102
  extend Clockblock::Timing
74
103
  add_timing_to :bar
75
104
  end
76
105
 
77
- run_timing_specs foo_klass.new
106
+ foo_klass_b.extend Clockblock::Timing
107
+ foo_klass_b.add_timing_to :bar
108
+
109
+ run_timing_specs_on foo_klass_a.new, :bar
110
+ run_timing_specs_on foo_klass_b.new, :bar
111
+
112
+ end
113
+ end
114
+
115
+ describe "extending Clockblock::Timing with an instance" do
116
+
117
+ it "wraps a defined method in a clock block" do
118
+
119
+ foo_klass = Class.new do
120
+ def bar(ret_val)
121
+ sleep 0.1; ret_val
122
+ end
123
+ end
124
+
125
+ f = foo_klass.new
126
+ f.extend Clockblock::Timing
127
+ f.add_timing_to :bar
128
+
129
+ run_timing_specs_on f, :bar
78
130
 
79
131
  end
132
+
133
+ it "eventually wraps an undefined method in a clock block" do
134
+
135
+ foo_klass = Class.new do
136
+ end
137
+
138
+ f = foo_klass.new
139
+ f.extend Clockblock::Timing
140
+ f.add_timing_to :bar
141
+
142
+ def f.bar(ret_val)
143
+ sleep 0.1; ret_val
144
+ end
145
+
146
+ run_timing_specs_on f, :bar
147
+
148
+ end
149
+
80
150
  end
151
+
81
152
  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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: