method_annotation 0.2.2 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +41 -16
- data/lib/method_annotation.rb +2 -0
- data/lib/method_annotation/cache.rb +3 -3
- data/lib/method_annotation/enable.rb +18 -8
- data/lib/method_annotation/parameter.rb +11 -0
- data/lib/method_annotation/trace.rb +22 -0
- data/lib/method_annotation/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bc62c8971f19084f3a63abe630311f503fbf7bb
|
4
|
+
data.tar.gz: 38b89482d48bef69aae4ae0c799bb9b6a37d1aeb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56635664591df2b3aab37acd70fda1164ed47705e631a7dbefa6fab0bd642652c2d44e7e2977cf1bda2e067dd3e3d745218b54ff0baf1d93102e6629e7c06d1b
|
7
|
+
data.tar.gz: 60f5f195f0fa08477a8cd8a9510034ddb2e80fd22f38174d3fd7ca581eaeaeb348314bce375b3318e8f731fa2a3727b943763812d63da7c82d308c2cbc5e7b97
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -30,7 +30,8 @@ Next, let's add your class to the method
|
|
30
30
|
# To include a MethodAnnotation::Enable to enable MethodAnnotation
|
31
31
|
include MethodAnnotation::Enable
|
32
32
|
|
33
|
-
# write
|
33
|
+
# write annotaion_name or
|
34
|
+
# "#{your annotation class}.name.underscore"
|
34
35
|
# ex) MyMethodAnnotation => my_method_annotation
|
35
36
|
my_method_annotation
|
36
37
|
def bar
|
@@ -76,12 +77,13 @@ About MethodAnnotation
|
|
76
77
|
|
77
78
|
class MyMethodAnnotation < MethodAnnotation::Base
|
78
79
|
# args is the argument of the method of target
|
79
|
-
|
80
|
+
# param class is MethodAnnotation::Parameter
|
81
|
+
before do |param|
|
80
82
|
puts 'before'
|
81
83
|
end
|
82
84
|
|
83
85
|
# args is the argument of the method of target
|
84
|
-
after do
|
86
|
+
after do |param|
|
85
87
|
puts 'after'
|
86
88
|
end
|
87
89
|
end
|
@@ -106,9 +108,9 @@ About MethodAnnotation
|
|
106
108
|
|
107
109
|
class MyMethodAnnotation < MethodAnnotation::Base
|
108
110
|
# original is proc methods of target
|
109
|
-
around do |
|
111
|
+
around do |param|
|
110
112
|
puts 'before'
|
111
|
-
return_value = original.call(
|
113
|
+
return_value = param.original.call(param)
|
112
114
|
puts 'after'
|
113
115
|
|
114
116
|
return_value
|
@@ -131,7 +133,7 @@ About MethodAnnotation
|
|
131
133
|
|
132
134
|
- MethodAnnotation::Cache
|
133
135
|
|
134
|
-
It is cached after the second time the execution result of the method is returned from the cache
|
136
|
+
It is cached after the second time the execution result of the method is returned from the cache.
|
135
137
|
|
136
138
|
require 'method_annotation'
|
137
139
|
|
@@ -154,26 +156,48 @@ About MethodAnnotation
|
|
154
156
|
foo.bar
|
155
157
|
=> "return value"
|
156
158
|
|
159
|
+
- MethodAnnotation::Trace
|
160
|
+
|
161
|
+
It is will trace the method. This is still a prototype.
|
162
|
+
|
163
|
+
require 'method_annotation'
|
164
|
+
|
165
|
+
class Foo
|
166
|
+
include MethodAnnotation::Enable
|
167
|
+
|
168
|
+
trace
|
169
|
+
def bar
|
170
|
+
Hoge.new.hogehoge
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
class Hoge
|
175
|
+
def hogehoge
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
Foo.new.bar
|
180
|
+
=> <===== Foo.bar trace =====>
|
181
|
+
=> (irb):15: in `hogehoge'
|
182
|
+
=> (irb):9: in `bar'
|
183
|
+
=> <=========================>
|
184
|
+
|
157
185
|
Example1
|
158
186
|
|
159
187
|
class PutsArg < MethodAnnotation::Base
|
160
188
|
self.annotation_name = 'puts_arg'
|
161
189
|
self.describe = 'output the arguments of the method'
|
162
190
|
|
163
|
-
before do
|
191
|
+
before do |params|
|
164
192
|
puts '-------args-------'
|
165
|
-
puts args
|
193
|
+
puts *params.args
|
166
194
|
puts '------------------'
|
167
195
|
end
|
168
196
|
end
|
169
197
|
|
170
|
-
PutsArg.describe
|
171
|
-
=> "output the arguments of the method"
|
172
|
-
|
173
198
|
class Foo
|
174
199
|
include MethodAnnotation::Enable
|
175
200
|
|
176
|
-
# write annotation_name or "#{your annotation class}.name.underscore"
|
177
201
|
puts_arg
|
178
202
|
def hoge(arg1, arg2)
|
179
203
|
puts 'hoge'
|
@@ -203,9 +227,9 @@ Example2
|
|
203
227
|
class TimeMeasurement < MethodAnnotation::Base
|
204
228
|
self.describe = 'measure the processing time of the method'
|
205
229
|
|
206
|
-
around do |
|
230
|
+
around do |param|
|
207
231
|
start = Time.now
|
208
|
-
return_value = original.call(
|
232
|
+
return_value = param.original.call(param)
|
209
233
|
puts "#{Time.now - start} sec"
|
210
234
|
|
211
235
|
return_value
|
@@ -229,8 +253,9 @@ Example3
|
|
229
253
|
class ArgsToString < MethodAnnotation::Base
|
230
254
|
self.describe = 'convert the arguments to string'
|
231
255
|
|
232
|
-
around do |
|
233
|
-
|
256
|
+
around do |param|
|
257
|
+
remake_param = MethodAnnotation::Parameter.new(args: param.args.map(&:to_s))
|
258
|
+
param.original.call(remake_param)
|
234
259
|
end
|
235
260
|
end
|
236
261
|
|
data/lib/method_annotation.rb
CHANGED
@@ -3,11 +3,11 @@ module MethodAnnotation
|
|
3
3
|
self.annotation_name = 'cache'
|
4
4
|
self.describe = 'It is cached after the second time the execution result of the method is returned from the cache'
|
5
5
|
|
6
|
-
around do |
|
6
|
+
around do |param|
|
7
7
|
@__chached ||= {}
|
8
8
|
|
9
|
-
key = "#{self.class}__#{
|
10
|
-
@__chached[key] = original.call(
|
9
|
+
key = "#{self.class}__#{param.method_name}".to_sym
|
10
|
+
@__chached[key] = param.original.call(param) unless @__chached.key?(key)
|
11
11
|
|
12
12
|
@__chached[key]
|
13
13
|
end
|
@@ -35,17 +35,27 @@ module MethodAnnotation
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
param = Parameter.new(args: args, method_name: name)
|
39
|
+
|
40
|
+
# do before
|
41
|
+
annotation_map[:before_procs].try(:each) { |blk| instance_exec(param, &blk) }
|
42
|
+
|
43
|
+
# do around
|
44
|
+
last_block = ->(parameter) { instance_eval { super(*parameter.args, &block) } }
|
40
45
|
if annotation_map[:around_procs].present?
|
41
|
-
|
42
|
-
->(
|
46
|
+
last_block = annotation_map[:around_procs].reverse.inject(last_block) do |block_chain, blk|
|
47
|
+
->(parameter) do
|
48
|
+
instance_exec(
|
49
|
+
Parameter.new(original: block_chain, args: parameter.args, method_name: parameter.method_name),
|
50
|
+
&blk
|
51
|
+
)
|
52
|
+
end
|
43
53
|
end
|
44
|
-
return_value = instance_exec(*args, &chain)
|
45
|
-
else
|
46
|
-
return_value = original.call(*args)
|
47
54
|
end
|
48
|
-
|
55
|
+
return_value = last_block.call(param)
|
56
|
+
|
57
|
+
# do after
|
58
|
+
annotation_map[:after_procs].try(:each) { |blk| instance_exec(param, &blk) }
|
49
59
|
return_value
|
50
60
|
end
|
51
61
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MethodAnnotation
|
2
|
+
class Trace < MethodAnnotation::Base
|
3
|
+
self.annotation_name = 'trace'
|
4
|
+
self.describe = 'Output the trace. It is still a prototype'
|
5
|
+
|
6
|
+
around do |param|
|
7
|
+
results = []
|
8
|
+
trace = TracePoint.new(:call) do |tp|
|
9
|
+
results << " #{tp.path}:#{tp.lineno}: in `#{tp.method_id}'"
|
10
|
+
end
|
11
|
+
|
12
|
+
return_value = nil
|
13
|
+
trace.enable { return_value = param.original.call(param) }
|
14
|
+
|
15
|
+
start_message = "<===== #{self.class}.#{param.method_name} trace =====>"
|
16
|
+
puts start_message
|
17
|
+
results.reverse.each { |v| puts v }
|
18
|
+
return_value
|
19
|
+
puts "<#{'=' * (start_message.size - 2)}>"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_annotation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masatoshi-watanuki
|
@@ -87,6 +87,8 @@ files:
|
|
87
87
|
- lib/method_annotation/base.rb
|
88
88
|
- lib/method_annotation/cache.rb
|
89
89
|
- lib/method_annotation/enable.rb
|
90
|
+
- lib/method_annotation/parameter.rb
|
91
|
+
- lib/method_annotation/trace.rb
|
90
92
|
- lib/method_annotation/version.rb
|
91
93
|
- method_annotation.gemspec
|
92
94
|
homepage: https://github.com/masatoshi-watanuki/gems/tree/master/method_annotation
|