method_annotation 0.2.2 → 0.3.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6925dc1e7c828cfddd9bae35a14119041f722ed1
4
- data.tar.gz: 5bec416ec9822fe12f64dc3d69bc53ae2bba7676
3
+ metadata.gz: 4bc62c8971f19084f3a63abe630311f503fbf7bb
4
+ data.tar.gz: 38b89482d48bef69aae4ae0c799bb9b6a37d1aeb
5
5
  SHA512:
6
- metadata.gz: 11686070fb63ee1b266a8a9937533334df0ce28d11ae005ca8dea818b6fd114cfe6245a2895517d1efaf6ca4d2a34525823cc10af5be1295243cfd2082ef7677
7
- data.tar.gz: 5a4c2d5465879dd8cb092011dbcac391a38dd1926ab1f7922a50ad0b5d9f3d4134a16c858a9abb3522f689f28ed4577e0e0ba072d4c4b1adc5635efe07c7d776
6
+ metadata.gz: 56635664591df2b3aab37acd70fda1164ed47705e631a7dbefa6fab0bd642652c2d44e7e2977cf1bda2e067dd3e3d745218b54ff0baf1d93102e6629e7c06d1b
7
+ data.tar.gz: 60f5f195f0fa08477a8cd8a9510034ddb2e80fd22f38174d3fd7ca581eaeaeb348314bce375b3318e8f731fa2a3727b943763812d63da7c82d308c2cbc5e7b97
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- method_annotation (0.2.2)
4
+ method_annotation (0.3.1)
5
5
  activesupport (~> 4.2.3)
6
6
 
7
7
  GEM
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 "#{your annotation class}.name.underscore"
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
- before do |*args|
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 |*args|
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 |original, *args|
111
+ around do |param|
110
112
  puts 'before'
111
- return_value = original.call(*args)
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 |*args|
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 |original, *args|
230
+ around do |param|
207
231
  start = Time.now
208
- return_value = original.call(*args)
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 |original, *args|
233
- original.call(*args.map(&:to_s))
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
 
@@ -6,9 +6,11 @@ module MethodAnnotation
6
6
 
7
7
  autoload :Base
8
8
  autoload :Enable
9
+ autoload :Parameter
9
10
 
10
11
  eager_autoload do
11
12
  autoload :Cache
13
+ autoload :Trace
12
14
  end
13
15
  end
14
16
 
@@ -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 |original, *args|
6
+ around do |param|
7
7
  @__chached ||= {}
8
8
 
9
- key = "#{self.class}__#{__method__}".to_sym
10
- @__chached[key] = original.call(*args) unless @__chached.key?(key)
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
- annotation_map[:before_procs].try(:each) { |blk| instance_exec(*args, &blk) }
39
- original = ->(*params) { instance_eval { super(*params, &block) } }
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
- chain = annotation_map[:around_procs].reverse.inject(original) do |block_chain, blk|
42
- ->(*params) { instance_exec(block_chain, *params, &blk) }
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
- annotation_map[:after_procs].try(:each) { |blk| instance_exec(*args, &blk) }
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,11 @@
1
+ module MethodAnnotation
2
+ class Parameter
3
+ attr_accessor :original, :args, :method_name
4
+
5
+ def initialize(original: nil, args: nil, method_name: nil)
6
+ @original = original
7
+ @args = args
8
+ @method_name = method_name
9
+ end
10
+ end
11
+ end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module MethodAnnotation
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.1"
3
3
  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.2.2
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