okuribito 0.1.7 → 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/.rubocop.yml +2 -2
- data/CHANGELOG.md +5 -0
- data/README.md +6 -2
- data/lib/okuribito.rb +59 -26
- data/lib/okuribito/version.rb +1 -1
- data/okuribito.gemspec +2 -2
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34c97f4c90330748a01b3f2206bd7899019c2e0d
|
4
|
+
data.tar.gz: e646d0977a8cb50ef2a194c4c3871499a8c8fa58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ca60fd68975a617f691d4acf68d8a3c0c4d14afd55cd22cb00350d26bf5947e1225b5b4d685ead583e23e081fe17d8ee1b145edf3a711052a0d3e125917c270
|
7
|
+
data.tar.gz: 69a9c559aac2a8df9fbe3c67ed37ce7a49cbc1cb44d0cecd7eb65f6837d18a3a023ef9aacd889853d46c002fafb3510c955bdafdb8d103cafb25f7145dc932cb
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -10,7 +10,9 @@ https://rubygems.org/gems/okuribito
|
|
10
10
|
|
11
11
|
Okuribito is a gem to judge whether methods should be sent to the heaven :innocent:.
|
12
12
|
|
13
|
-
|
13
|
+
Okuribito monitors the method call with YAML, and exec specified code.
|
14
|
+
|
15
|
+
In other words, it can be used in order to extract the uncalled method.
|
14
16
|
|
15
17
|
Okuribito was named after a japanese movie.
|
16
18
|
|
@@ -39,6 +41,8 @@ User:
|
|
39
41
|
- '#feed'
|
40
42
|
Micropost:
|
41
43
|
- '.from_users_followed_by'
|
44
|
+
Admin::Manage:
|
45
|
+
- '.add_user'
|
42
46
|
```
|
43
47
|
|
44
48
|
By writing the following code to start the monitoring of the method.
|
@@ -135,4 +139,4 @@ okuribito.apply("config/okuribito.yml")
|
|
135
139
|
## License
|
136
140
|
|
137
141
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
138
|
-
|
142
|
+
Copyright 2016 Yasuhiro Matsumura.
|
data/lib/okuribito.rb
CHANGED
@@ -9,12 +9,9 @@ module Okuribito
|
|
9
9
|
INSTANCE_METHOD_SYMBOL = "#".freeze
|
10
10
|
PATTERN = /\A(?<symbol>[#{CLASS_METHOD_SYMBOL}#{INSTANCE_METHOD_SYMBOL}])(?<method_name>.+)\z/
|
11
11
|
|
12
|
-
def initialize(opt = {}, &callback)
|
13
|
-
@callback = callback
|
14
|
-
@opt ||= opt
|
15
|
-
end
|
16
|
-
|
17
12
|
module SimplePatchModule
|
13
|
+
private
|
14
|
+
|
18
15
|
def define_patch(method_name, _patch, _id, _opt = {})
|
19
16
|
define_method(method_name) do |*args|
|
20
17
|
yield(to_s, caller) if block_given?
|
@@ -24,18 +21,26 @@ module Okuribito
|
|
24
21
|
end
|
25
22
|
|
26
23
|
module FunctionalPatchModule
|
24
|
+
private
|
25
|
+
|
27
26
|
def define_patch(method_name, patch, id, opt = {})
|
28
|
-
|
27
|
+
sn = method_name.to_s.gsub(/\?/, "__q").gsub(/!/, "__e")
|
28
|
+
patch.instance_variable_set("@#{sn}_#{id}_called", false)
|
29
29
|
define_method(method_name) do |*args|
|
30
|
-
if block_given? && !patch.instance_variable_get("@#{
|
30
|
+
if block_given? && !patch.instance_variable_get("@#{sn}_#{id}_called")
|
31
31
|
yield(to_s, caller)
|
32
|
-
patch.instance_variable_set("@#{
|
32
|
+
patch.instance_variable_set("@#{sn}_#{id}_called", true) if opt[:once_detect]
|
33
33
|
end
|
34
34
|
super(*args)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
def initialize(opt = {}, &callback)
|
40
|
+
@callback = callback
|
41
|
+
@opt ||= opt
|
42
|
+
end
|
43
|
+
|
39
44
|
def apply(yaml_path)
|
40
45
|
yaml = YAML.load_file(yaml_path)
|
41
46
|
yaml.each do |class_name, observe_methods|
|
@@ -43,26 +48,20 @@ module Okuribito
|
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
46
|
-
|
47
|
-
if opt.present?
|
48
|
-
if FunctionalPatchModule.const_defined?(patch_name)
|
49
|
-
Module.new.extend(FunctionalPatchModule)
|
50
|
-
else
|
51
|
-
FunctionalPatchModule.const_set(patch_name, Module.new.extend(FunctionalPatchModule))
|
52
|
-
end
|
53
|
-
else
|
54
|
-
Module.new.extend(SimplePatchModule)
|
55
|
-
end
|
56
|
-
end
|
51
|
+
private
|
57
52
|
|
58
|
-
def patch_okuribito(
|
59
|
-
|
53
|
+
def patch_okuribito(full_class_name, observe_methods)
|
54
|
+
constants = full_class_name.split("::")
|
55
|
+
class_name = constants[-1]
|
56
|
+
return unless (namespace = constants_to_namespace(constants))
|
57
|
+
return unless defined_class?(namespace, class_name)
|
58
|
+
snamespace = namespace.to_s.gsub(/::/, "")
|
60
59
|
|
61
60
|
callback = @callback
|
62
61
|
opt ||= @opt
|
63
|
-
klass =
|
64
|
-
i_method_patch =
|
65
|
-
c_method_patch =
|
62
|
+
klass = full_class_name.constantize
|
63
|
+
i_method_patch = patch_module(opt, "Ns#{snamespace}Class#{class_name}InstancePatch")
|
64
|
+
c_method_patch = patch_module(opt, "Ns#{snamespace}Class#{class_name}ClassPatch")
|
66
65
|
i_method_patched = 0
|
67
66
|
c_method_patched = 0
|
68
67
|
|
@@ -77,7 +76,7 @@ module Okuribito
|
|
77
76
|
next unless klass.instance_methods.include?(method_name)
|
78
77
|
i_method_patch.module_eval do
|
79
78
|
define_patch(method_name, i_method_patch, "i", opt) do |obj_name, caller_info|
|
80
|
-
callback.call(method_name, obj_name, caller_info,
|
79
|
+
callback.call(method_name, obj_name, caller_info, full_class_name, symbol)
|
81
80
|
end
|
82
81
|
end
|
83
82
|
i_method_patched += 1
|
@@ -85,7 +84,7 @@ module Okuribito
|
|
85
84
|
next unless klass.respond_to?(method_name)
|
86
85
|
c_method_patch.module_eval do
|
87
86
|
define_patch(method_name, c_method_patch, "c", opt) do |obj_name, caller_info|
|
88
|
-
callback.call(method_name, obj_name, caller_info,
|
87
|
+
callback.call(method_name, obj_name, caller_info, full_class_name, symbol)
|
89
88
|
end
|
90
89
|
end
|
91
90
|
c_method_patched += 1
|
@@ -95,5 +94,39 @@ module Okuribito
|
|
95
94
|
singleton_class.send(:prepend, c_method_patch) if c_method_patched > 0
|
96
95
|
end
|
97
96
|
end
|
97
|
+
|
98
|
+
def patch_module(opt, patch_name)
|
99
|
+
if opt.present?
|
100
|
+
if FunctionalPatchModule.const_defined?(patch_name)
|
101
|
+
Module.new.extend(FunctionalPatchModule)
|
102
|
+
else
|
103
|
+
FunctionalPatchModule.const_set(patch_name, Module.new.extend(FunctionalPatchModule))
|
104
|
+
end
|
105
|
+
else
|
106
|
+
Module.new.extend(SimplePatchModule)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def defined_namespace?(constants)
|
111
|
+
namespace = Object
|
112
|
+
constants.each do |constant|
|
113
|
+
return false unless namespace.const_defined?(constant)
|
114
|
+
namespace = "#{namespace}::#{constant}".constantize
|
115
|
+
end
|
116
|
+
true
|
117
|
+
end
|
118
|
+
|
119
|
+
def defined_class?(namespace, class_name)
|
120
|
+
namespace.const_defined?(class_name) && namespace.const_get(class_name).is_a?(Class)
|
121
|
+
end
|
122
|
+
|
123
|
+
def constants_to_namespace(constants)
|
124
|
+
if constants.size == 1
|
125
|
+
Object
|
126
|
+
else
|
127
|
+
return false unless defined_namespace?(constants[0..-2])
|
128
|
+
constants[0..-2].join("::").constantize
|
129
|
+
end
|
130
|
+
end
|
98
131
|
end
|
99
132
|
end
|
data/lib/okuribito/version.rb
CHANGED
data/okuribito.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["muramurasan"]
|
10
10
|
spec.email = ["ym.works1985@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = "
|
13
|
-
spec.description = "Okuribito monitors the method call
|
12
|
+
spec.summary = "Okuribito monitors the method call, and exec specified code."
|
13
|
+
spec.description = "Okuribito monitors the method call, and exec specified code."
|
14
14
|
spec.homepage = "https://github.com/muramurasan/okuribito"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: okuribito
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- muramurasan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 1.0.0
|
83
|
-
description: Okuribito monitors the method call
|
83
|
+
description: Okuribito monitors the method call, and exec specified code.
|
84
84
|
email:
|
85
85
|
- ym.works1985@gmail.com
|
86
86
|
executables: []
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- .gitignore
|
91
91
|
- .rspec
|
92
92
|
- .rubocop.yml
|
93
|
+
- CHANGELOG.md
|
93
94
|
- Gemfile
|
94
95
|
- LICENSE.txt
|
95
96
|
- README.md
|
@@ -119,8 +120,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
120
|
version: '0'
|
120
121
|
requirements: []
|
121
122
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.
|
123
|
+
rubygems_version: 2.6.8
|
123
124
|
signing_key:
|
124
125
|
specification_version: 4
|
125
|
-
summary:
|
126
|
+
summary: Okuribito monitors the method call, and exec specified code.
|
126
127
|
test_files: []
|