method_annotation 0.1.2 → 0.2.0
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 +14 -10
- data/lib/method_annotation.rb +9 -12
- data/lib/method_annotation/cache.rb +16 -0
- data/lib/method_annotation/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccbcf52da5ae78b9dc4247f8a47651ff8ae1affb
|
4
|
+
data.tar.gz: 0dcb7534c809472149aea6d53b9eb4211bd59b4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4cb2e87f0d78c735fa06c60553a81e1c5a5c98a538c3252f7b4c30a19e86b163987b0959deb6f31a627c68dcdee7ed26a58ff1225cc6ae648baf8842aac0320
|
7
|
+
data.tar.gz: b33092ed50a8bd725dcab2efef12c7207696184b0b9f305483a9cf89fa8adf1319715c42d9623936e4f064d220fb523fe3817655db5f556af030709e9416261a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# MethodAnnotation
|
2
2
|
|
3
|
-
MethodAnnotation You can define the
|
3
|
+
MethodAnnotation You can define the annotation function method.
|
4
4
|
Note translation function can also be added simply tagged to only cross-processing from applications.
|
5
5
|
|
6
6
|
## Installation
|
@@ -41,15 +41,15 @@ You can define an annotation in this way
|
|
41
41
|
|
42
42
|
|
43
43
|
About MethodAnnotation
|
44
|
-
- .describe .
|
44
|
+
- .describe .describe=
|
45
45
|
|
46
46
|
You can set forth a description
|
47
47
|
|
48
48
|
class MyMethodAnnotation < MethodAnnotation::Base
|
49
|
-
describe 'sample annotation'
|
49
|
+
self.describe = 'sample annotation'
|
50
50
|
end
|
51
51
|
|
52
|
-
MyMethodAnnotation.
|
52
|
+
MyMethodAnnotation.describe
|
53
53
|
=> "sample annotation"
|
54
54
|
|
55
55
|
- .list
|
@@ -115,8 +115,10 @@ About MethodAnnotation
|
|
115
115
|
# original is proc methods of target
|
116
116
|
around do |original, *args|
|
117
117
|
puts 'before'
|
118
|
-
original.call(*args)
|
118
|
+
return_value = original.call(*args)
|
119
119
|
puts 'after'
|
120
|
+
|
121
|
+
return_value
|
120
122
|
end
|
121
123
|
end
|
122
124
|
|
@@ -137,7 +139,7 @@ About MethodAnnotation
|
|
137
139
|
Example1
|
138
140
|
|
139
141
|
class PutsArg < MethodAnnotation::Base
|
140
|
-
describe 'output the arguments of the method'
|
142
|
+
self.describe = 'output the arguments of the method'
|
141
143
|
|
142
144
|
before do |*args|
|
143
145
|
puts '-------args-------'
|
@@ -146,7 +148,7 @@ Example1
|
|
146
148
|
end
|
147
149
|
end
|
148
150
|
|
149
|
-
PutsArg.
|
151
|
+
PutsArg.describe
|
150
152
|
=> "output the arguments of the method"
|
151
153
|
|
152
154
|
class Foo
|
@@ -180,12 +182,14 @@ Example1
|
|
180
182
|
Example2
|
181
183
|
|
182
184
|
class TimeMeasurement < MethodAnnotation::Base
|
183
|
-
describe 'measure the processing time of the method'
|
185
|
+
self.describe = 'measure the processing time of the method'
|
184
186
|
|
185
187
|
around do |original, *args|
|
186
188
|
start = Time.now
|
187
|
-
original.call(*args)
|
189
|
+
return_value = original.call(*args)
|
188
190
|
puts "#{Time.now - start} sec"
|
191
|
+
|
192
|
+
return_value
|
189
193
|
end
|
190
194
|
end
|
191
195
|
|
@@ -204,7 +208,7 @@ Example2
|
|
204
208
|
Example3
|
205
209
|
|
206
210
|
class ArgsToString < MethodAnnotation::Base
|
207
|
-
describe 'convert the arguments to string'
|
211
|
+
self.describe = 'convert the arguments to string'
|
208
212
|
|
209
213
|
around do |original, *args|
|
210
214
|
original.call(*args.map(&:to_s))
|
data/lib/method_annotation.rb
CHANGED
@@ -5,15 +5,7 @@ require 'active_support/core_ext'
|
|
5
5
|
module MethodAnnotation
|
6
6
|
class Base
|
7
7
|
class << self
|
8
|
-
attr_accessor :before_procs, :after_procs, :around_procs, :list
|
9
|
-
|
10
|
-
def describe(desc)
|
11
|
-
@desc = desc
|
12
|
-
end
|
13
|
-
|
14
|
-
def description
|
15
|
-
@desc
|
16
|
-
end
|
8
|
+
attr_accessor :before_procs, :after_procs, :around_procs, :list, :annotation_name, :describe
|
17
9
|
|
18
10
|
def before(&block)
|
19
11
|
(@before_procs ||= []) << block
|
@@ -70,11 +62,12 @@ module MethodAnnotation
|
|
70
62
|
chain = annotation_map[:around_procs].reverse.inject(original) do |block_chain, blk|
|
71
63
|
->(*params) { instance_exec(block_chain, *params, &blk) }
|
72
64
|
end
|
73
|
-
instance_exec(*args, &chain)
|
65
|
+
return_value = instance_exec(*args, &chain)
|
74
66
|
else
|
75
|
-
original.call(*args)
|
67
|
+
return_value = original.call(*args)
|
76
68
|
end
|
77
69
|
annotation_map[:after_procs].try(:each) { |blk| instance_exec(*args, &blk) }
|
70
|
+
return_value
|
78
71
|
end
|
79
72
|
|
80
73
|
end
|
@@ -83,7 +76,11 @@ module MethodAnnotation
|
|
83
76
|
end
|
84
77
|
|
85
78
|
def method_missing(method, *args)
|
86
|
-
annotation = MethodAnnotation::Base.subclasses.find
|
79
|
+
annotation = MethodAnnotation::Base.subclasses.find do |c|
|
80
|
+
puts method
|
81
|
+
puts c.annotation_name
|
82
|
+
c.annotation_name == method.to_s || (c.name == method.to_s.classify)
|
83
|
+
end
|
87
84
|
if annotation
|
88
85
|
(@_annotations ||= []) << annotation
|
89
86
|
else
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module MethodAnnotation
|
2
|
+
class Cache < Base
|
3
|
+
self.annotation_name = 'cache'
|
4
|
+
self.describe = 'It is cached after the second time the execution result of the method is returned from the cache'
|
5
|
+
|
6
|
+
around do |original, *args|
|
7
|
+
@__chached ||= {}
|
8
|
+
|
9
|
+
key = "#{self.class}__#{__method__}".to_sym
|
10
|
+
@__chached[key] = original.call(*args) unless @__chached.key?(key)
|
11
|
+
|
12
|
+
@__chached[key]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_annotation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masatoshi-watanuki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- bin/console
|
82
82
|
- bin/setup
|
83
83
|
- lib/method_annotation.rb
|
84
|
+
- lib/method_annotation/cache.rb
|
84
85
|
- lib/method_annotation/version.rb
|
85
86
|
- method_annotation.gemspec
|
86
87
|
homepage: https://github.com/masatoshi-watanuki/gems/tree/master/method_annotation
|