method_annotation 0.2.0 → 0.2.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +42 -22
- data/lib/annotations.rb +11 -0
- data/lib/{method_annotation → annotations}/cache.rb +4 -2
- data/lib/method_annotation.rb +4 -87
- data/lib/method_annotation/base.rb +19 -0
- data/lib/method_annotation/enable.rb +69 -0
- data/lib/method_annotation/version.rb +1 -1
- data/method_annotation.gemspec +5 -5
- metadata +12 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f8d9b27ee0706f184c6f7a7aade7cc72fb0e4cfc
|
|
4
|
+
data.tar.gz: e0d0de2acdd0753fc733da3fe1fec7c7bf462bd5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 807e915e1d54789cb8839ee8cb462223de87c00d985cbe8a525e5513c2e19591eef0df364490dd04820dfa1644f22254413b530145fdeafc2bc3f79b6966957a
|
|
7
|
+
data.tar.gz: 98079a56c935c56a5dec9208458da0d70289841208cbd57c67d2f24faaed745e5f94df475cf60e1bac341e1a76b28c06be33cbdcb11faf45d73113596ee4a1e0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -41,6 +41,17 @@ You can define an annotation in this way
|
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
About MethodAnnotation
|
|
44
|
+
- .annotation_name .annotation_name=
|
|
45
|
+
|
|
46
|
+
You can set forth a annotation name
|
|
47
|
+
|
|
48
|
+
class MyMethodAnnotation < MethodAnnotation::Base
|
|
49
|
+
self.annotation_name = 'my_method_annotation'
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
MyMethodAnnotation.annotation_name
|
|
53
|
+
=> "my_method_annotation"
|
|
54
|
+
|
|
44
55
|
- .describe .describe=
|
|
45
56
|
|
|
46
57
|
You can set forth a description
|
|
@@ -59,35 +70,16 @@ About MethodAnnotation
|
|
|
59
70
|
MyMethodAnnotation.list
|
|
60
71
|
=> [[Foo, :bar]]
|
|
61
72
|
|
|
62
|
-
- .before
|
|
73
|
+
- .before .after
|
|
63
74
|
|
|
64
|
-
You can define the processing to be performed in method execution before the target
|
|
75
|
+
You can define the processing to be performed in method execution before/after the target
|
|
65
76
|
|
|
66
77
|
class MyMethodAnnotation < MethodAnnotation::Base
|
|
67
78
|
# args is the argument of the method of target
|
|
68
79
|
before do |*args|
|
|
69
80
|
puts 'before'
|
|
70
81
|
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
class Foo
|
|
74
|
-
include MethodAnnotation::Enable
|
|
75
|
-
|
|
76
|
-
my_method_annotation
|
|
77
|
-
def bar
|
|
78
|
-
puts 'bar'
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
Foo.new.bar
|
|
83
|
-
=> before
|
|
84
|
-
=> bar
|
|
85
|
-
|
|
86
|
-
- .after
|
|
87
|
-
|
|
88
|
-
You can define the processing to be performed in method execution after the target
|
|
89
82
|
|
|
90
|
-
class MyMethodAnnotation < MethodAnnotation::Base
|
|
91
83
|
# args is the argument of the method of target
|
|
92
84
|
after do |*args|
|
|
93
85
|
puts 'after'
|
|
@@ -104,6 +96,7 @@ About MethodAnnotation
|
|
|
104
96
|
end
|
|
105
97
|
|
|
106
98
|
Foo.new.bar
|
|
99
|
+
=> before
|
|
107
100
|
=> bar
|
|
108
101
|
=> after
|
|
109
102
|
|
|
@@ -136,9 +129,36 @@ About MethodAnnotation
|
|
|
136
129
|
=> bar
|
|
137
130
|
=> after
|
|
138
131
|
|
|
132
|
+
- MethodAnnotation::Annotations::Cache
|
|
133
|
+
|
|
134
|
+
It is cached after the second time the execution result of the method is returned from the cache
|
|
135
|
+
|
|
136
|
+
require 'method_annotation'
|
|
137
|
+
require 'annotations'
|
|
138
|
+
|
|
139
|
+
class Foo
|
|
140
|
+
include MethodAnnotation::Enable
|
|
141
|
+
|
|
142
|
+
cache
|
|
143
|
+
def bar
|
|
144
|
+
puts 'exec'
|
|
145
|
+
'return value'
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
foo = Foo.new
|
|
150
|
+
foo.bar
|
|
151
|
+
=> exec
|
|
152
|
+
=> "return value"
|
|
153
|
+
|
|
154
|
+
# The second time is not puts 'exec'
|
|
155
|
+
foo.bar
|
|
156
|
+
=> "return value"
|
|
157
|
+
|
|
139
158
|
Example1
|
|
140
159
|
|
|
141
160
|
class PutsArg < MethodAnnotation::Base
|
|
161
|
+
self.annotation_name = 'puts_arg'
|
|
142
162
|
self.describe = 'output the arguments of the method'
|
|
143
163
|
|
|
144
164
|
before do |*args|
|
|
@@ -154,7 +174,7 @@ Example1
|
|
|
154
174
|
class Foo
|
|
155
175
|
include MethodAnnotation::Enable
|
|
156
176
|
|
|
157
|
-
# write "#{your annotation class}.name.underscore"
|
|
177
|
+
# write annotation_name or "#{your annotation class}.name.underscore"
|
|
158
178
|
puts_arg
|
|
159
179
|
def hoge(arg1, arg2)
|
|
160
180
|
puts 'hoge'
|
data/lib/annotations.rb
ADDED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
require 'method_annotation'
|
|
2
|
+
|
|
3
|
+
module Annotations
|
|
4
|
+
class Cache < MethodAnnotation::Base
|
|
3
5
|
self.annotation_name = 'cache'
|
|
4
6
|
self.describe = 'It is cached after the second time the execution result of the method is returned from the cache'
|
|
5
7
|
|
data/lib/method_annotation.rb
CHANGED
|
@@ -1,92 +1,9 @@
|
|
|
1
1
|
require "method_annotation/version"
|
|
2
|
-
require 'active_support'
|
|
3
|
-
require 'active_support/core_ext'
|
|
2
|
+
require 'active_support/dependencies/autoload'
|
|
4
3
|
|
|
5
4
|
module MethodAnnotation
|
|
6
|
-
|
|
7
|
-
class << self
|
|
8
|
-
attr_accessor :before_procs, :after_procs, :around_procs, :list, :annotation_name, :describe
|
|
5
|
+
extend ActiveSupport::Autoload
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def after(&block)
|
|
15
|
-
(@after_procs ||= []) << block
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def around(&block)
|
|
19
|
-
(@around_procs ||= []) << block
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
concern :Enable do
|
|
25
|
-
def self.included(klass)
|
|
26
|
-
Module.new.tap do |mod|
|
|
27
|
-
klass.prepend(mod)
|
|
28
|
-
klass._prepended_module = mod
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# includeしたクラスが継承された場合もMethodAnnotaionを有効にする
|
|
32
|
-
klass.class_eval do
|
|
33
|
-
def self.inherited(subclass)
|
|
34
|
-
Module.new.tap do |mod|
|
|
35
|
-
subclass.prepend(mod)
|
|
36
|
-
subclass._prepended_module = mod
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
class_methods do
|
|
43
|
-
attr_accessor :_prepended_module, :_annotations
|
|
44
|
-
|
|
45
|
-
def method_added(name)
|
|
46
|
-
if @_annotations.present?
|
|
47
|
-
annotations = @_annotations
|
|
48
|
-
_prepended_module.module_eval do
|
|
49
|
-
|
|
50
|
-
define_method(name) do |*args, &block|
|
|
51
|
-
annotation_map = annotations.each_with_object({}) do |annotation, hash|
|
|
52
|
-
# 宣言した[class, method_name]をリストに追加する
|
|
53
|
-
(annotation.list ||= []) << [self.class, name.to_sym]
|
|
54
|
-
%i(before_procs around_procs after_procs).each do |proc_name|
|
|
55
|
-
(hash[proc_name] ||= []).push(*annotation.send(proc_name))
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
annotation_map[:before_procs].try(:each) { |blk| instance_exec(*args, &blk) }
|
|
60
|
-
original = ->(*params) { instance_eval { super(*params, &block) } }
|
|
61
|
-
if annotation_map[:around_procs].present?
|
|
62
|
-
chain = annotation_map[:around_procs].reverse.inject(original) do |block_chain, blk|
|
|
63
|
-
->(*params) { instance_exec(block_chain, *params, &blk) }
|
|
64
|
-
end
|
|
65
|
-
return_value = instance_exec(*args, &chain)
|
|
66
|
-
else
|
|
67
|
-
return_value = original.call(*args)
|
|
68
|
-
end
|
|
69
|
-
annotation_map[:after_procs].try(:each) { |blk| instance_exec(*args, &blk) }
|
|
70
|
-
return_value
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
@_annotations = nil
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def method_missing(method, *args)
|
|
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
|
|
84
|
-
if annotation
|
|
85
|
-
(@_annotations ||= []) << annotation
|
|
86
|
-
else
|
|
87
|
-
super
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
7
|
+
autoload :Base
|
|
8
|
+
autoload :Enable
|
|
92
9
|
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module MethodAnnotation
|
|
2
|
+
class Base
|
|
3
|
+
class << self
|
|
4
|
+
attr_accessor :before_procs, :after_procs, :around_procs, :list, :annotation_name, :describe
|
|
5
|
+
|
|
6
|
+
def before(&block)
|
|
7
|
+
(@before_procs ||= []) << block
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def after(&block)
|
|
11
|
+
(@after_procs ||= []) << block
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def around(&block)
|
|
15
|
+
(@around_procs ||= []) << block
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'active_support'
|
|
2
|
+
require 'active_support/core_ext'
|
|
3
|
+
|
|
4
|
+
module MethodAnnotation
|
|
5
|
+
concern :Enable do
|
|
6
|
+
def self.included(klass)
|
|
7
|
+
Module.new.tap do |mod|
|
|
8
|
+
klass.prepend(mod)
|
|
9
|
+
klass._prepended_module = mod
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
klass.class_eval do
|
|
13
|
+
def self.inherited(subclass)
|
|
14
|
+
Module.new.tap do |mod|
|
|
15
|
+
subclass.prepend(mod)
|
|
16
|
+
subclass._prepended_module = mod
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class_methods do
|
|
23
|
+
attr_accessor :_prepended_module, :_annotations
|
|
24
|
+
|
|
25
|
+
def method_added(name)
|
|
26
|
+
if @_annotations.present?
|
|
27
|
+
annotations = @_annotations
|
|
28
|
+
_prepended_module.module_eval do
|
|
29
|
+
|
|
30
|
+
define_method(name) do |*args, &block|
|
|
31
|
+
annotation_map = annotations.each_with_object({}) do |annotation, hash|
|
|
32
|
+
(annotation.list ||= []) << [self.class, name.to_sym]
|
|
33
|
+
%i(before_procs around_procs after_procs).each do |proc_name|
|
|
34
|
+
(hash[proc_name] ||= []).push(*annotation.send(proc_name))
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
annotation_map[:before_procs].try(:each) { |blk| instance_exec(*args, &blk) }
|
|
39
|
+
original = ->(*params) { instance_eval { super(*params, &block) } }
|
|
40
|
+
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) }
|
|
43
|
+
end
|
|
44
|
+
return_value = instance_exec(*args, &chain)
|
|
45
|
+
else
|
|
46
|
+
return_value = original.call(*args)
|
|
47
|
+
end
|
|
48
|
+
annotation_map[:after_procs].try(:each) { |blk| instance_exec(*args, &blk) }
|
|
49
|
+
return_value
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
@_annotations = nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def method_missing(method, *args)
|
|
58
|
+
annotation = MethodAnnotation::Base.subclasses.find do |c|
|
|
59
|
+
c.annotation_name == method.to_s || (c.name == method.to_s.classify)
|
|
60
|
+
end
|
|
61
|
+
if annotation
|
|
62
|
+
(@_annotations ||= []) << annotation
|
|
63
|
+
else
|
|
64
|
+
super
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
data/method_annotation.gemspec
CHANGED
|
@@ -10,7 +10,10 @@ Gem::Specification.new do |spec|
|
|
|
10
10
|
spec.email = ["masatoshi.watanuki@gmail.com"]
|
|
11
11
|
|
|
12
12
|
spec.summary = 'method annotation'
|
|
13
|
-
spec.description =
|
|
13
|
+
spec.description = <<-EOS
|
|
14
|
+
MethodAnnotation You can define the annotation function method.
|
|
15
|
+
Note translation function can also be added simply tagged to only cross-processing from applications.
|
|
16
|
+
EOS
|
|
14
17
|
spec.homepage = "https://github.com/masatoshi-watanuki/gems/tree/master/method_annotation"
|
|
15
18
|
|
|
16
19
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
@@ -19,10 +22,7 @@ Gem::Specification.new do |spec|
|
|
|
19
22
|
spec.require_paths = ["lib"]
|
|
20
23
|
|
|
21
24
|
spec.required_ruby_version = Gem::Requirement.create('~> 2.2.1')
|
|
22
|
-
|
|
23
|
-
#if spec.respond_to?(:metadata)
|
|
24
|
-
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
|
25
|
-
#end
|
|
25
|
+
spec.licenses = ['MIT']
|
|
26
26
|
|
|
27
27
|
spec.add_development_dependency "bundler", "~> 1.9"
|
|
28
28
|
spec.add_development_dependency "rake", "~> 10.0"
|
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.2.
|
|
4
|
+
version: 0.2.1
|
|
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-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -66,7 +66,9 @@ dependencies:
|
|
|
66
66
|
- - "~>"
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
68
|
version: 4.2.3
|
|
69
|
-
description: method
|
|
69
|
+
description: "MethodAnnotation You can define the annotation function method. \nNote
|
|
70
|
+
translation function can also be added simply tagged to only cross-processing from
|
|
71
|
+
applications.\n"
|
|
70
72
|
email:
|
|
71
73
|
- masatoshi.watanuki@gmail.com
|
|
72
74
|
executables: []
|
|
@@ -80,12 +82,16 @@ files:
|
|
|
80
82
|
- Rakefile
|
|
81
83
|
- bin/console
|
|
82
84
|
- bin/setup
|
|
85
|
+
- lib/annotations.rb
|
|
86
|
+
- lib/annotations/cache.rb
|
|
83
87
|
- lib/method_annotation.rb
|
|
84
|
-
- lib/method_annotation/
|
|
88
|
+
- lib/method_annotation/base.rb
|
|
89
|
+
- lib/method_annotation/enable.rb
|
|
85
90
|
- lib/method_annotation/version.rb
|
|
86
91
|
- method_annotation.gemspec
|
|
87
92
|
homepage: https://github.com/masatoshi-watanuki/gems/tree/master/method_annotation
|
|
88
|
-
licenses:
|
|
93
|
+
licenses:
|
|
94
|
+
- MIT
|
|
89
95
|
metadata: {}
|
|
90
96
|
post_install_message:
|
|
91
97
|
rdoc_options: []
|
|
@@ -108,3 +114,4 @@ signing_key:
|
|
|
108
114
|
specification_version: 4
|
|
109
115
|
summary: method annotation
|
|
110
116
|
test_files: []
|
|
117
|
+
has_rdoc:
|