rinterceptor 0.1.0 → 0.2.0
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.
- data/CHANGELOG +4 -0
- data/Rakefile +1 -1
- data/lib/rinterceptor.rb +6 -47
- data/lib/{example_rinterceptor.rb → rinterceptor_example.rb} +21 -18
- data/lib/rinterceptor_helper.rb +25 -0
- data/lib/rinterceptor_invocation.rb +16 -0
- metadata +5 -3
data/CHANGELOG
CHANGED
data/Rakefile
CHANGED
data/lib/rinterceptor.rb
CHANGED
@@ -21,6 +21,7 @@ end
|
|
21
21
|
#you can refer to example_rinterceptor.rb
|
22
22
|
=end
|
23
23
|
#TODO nested interceptor
|
24
|
+
require 'rinterceptor_helper'
|
24
25
|
module Rinterceptor
|
25
26
|
def self.included(base)
|
26
27
|
raise 'Class can not include me directly, only accept Module to include me' if base.is_a?(Class)
|
@@ -56,12 +57,12 @@ module Rinterceptor
|
|
56
57
|
base.ancestors[1..-1].reverse.each {|p| base.extend(p::ClassMethods) if p.respond_to?(:rinter_here?)}
|
57
58
|
include_s_methods = base.include_s_methods || []
|
58
59
|
include_i_methods = base.include_i_methods || []
|
59
|
-
include_s_methods =
|
60
|
-
include_i_methods =
|
60
|
+
include_s_methods = RinterceptorHelper.process_include_methods(include_s_methods)
|
61
|
+
include_i_methods = RinterceptorHelper.process_include_methods(include_i_methods)
|
61
62
|
exclude_s_methods = (base.exclude_s_methods || []) + [/^rinter_/, /include_[is]_methods/, /exclude_[is]_methods/]
|
62
63
|
exclude_i_methods = (base.exclude_i_methods || []) + [/^rinter_/, /include_[is]_methods/, /exclude_[is]_methods/]
|
63
|
-
s_methods = (base.private_methods + base.singleton_methods).delete_if{|m| !
|
64
|
-
i_methods = (base.private_instance_methods + base.instance_methods).delete_if{|m| !
|
64
|
+
s_methods = (base.private_methods + base.singleton_methods).delete_if{|m| ! RinterceptorHelper.match_method?(m, include_s_methods, exclude_s_methods) }
|
65
|
+
i_methods = (base.private_instance_methods + base.instance_methods).delete_if{|m| ! RinterceptorHelper.match_method?(m, include_i_methods, exclude_i_methods) }
|
65
66
|
s_methods.each{|m| rinter_generate_proxy(base, m, include_s_methods.is_a?(Hash) ? include_s_methods : nil, true)}
|
66
67
|
i_methods.each{|m| rinter_generate_proxy(base, m, include_i_methods.is_a?(Hash) ? include_i_methods : nil)}
|
67
68
|
rinter_after_include_class(base) #only available in last module for preparing data of Class which will include it
|
@@ -88,6 +89,7 @@ module Rinterceptor
|
|
88
89
|
end
|
89
90
|
obj.class_eval %{
|
90
91
|
#{"class << self" if singleton}
|
92
|
+
require "rinterceptor_invocation"
|
91
93
|
alias_method :old4rinter_#{method}, :#{method} unless method_defined?(:old4rinter_#{method})
|
92
94
|
def #{method}(*args)
|
93
95
|
return old4rinter_#{method}(*args) if rinter_skip?
|
@@ -133,46 +135,3 @@ module Rinterceptor
|
|
133
135
|
end
|
134
136
|
end
|
135
137
|
end
|
136
|
-
|
137
|
-
module RinterceptorUtil
|
138
|
-
def self.match_method?(method, includes, excludes)
|
139
|
-
excludes.each {|i| return false if method =~ i}
|
140
|
-
real_includes = includes.is_a?(Array) ? includes : []
|
141
|
-
if includes.is_a?(Hash)
|
142
|
-
includes.each{|k, v| real_includes += v}
|
143
|
-
end
|
144
|
-
real_includes.each {|i| return true if method =~ i}
|
145
|
-
false
|
146
|
-
end
|
147
|
-
def self.process_include_methods(methods)
|
148
|
-
if methods.is_a?(Hash)
|
149
|
-
methods.each do |k, v|
|
150
|
-
if v.is_a?(String) || v.is_a?(Symbol)
|
151
|
-
v = Regexp.new("^#{v}$")
|
152
|
-
elsif v.is_a?(Array)
|
153
|
-
v.each_index{|i| v[i] = Regexp.new("^#{v[i]}$") if v[i].is_a?(String) || v[i].is_a?(Symbol)}
|
154
|
-
end
|
155
|
-
v = [v] if v.is_a?(Regexp)
|
156
|
-
methods[k] = v
|
157
|
-
end
|
158
|
-
end
|
159
|
-
methods
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
class RinterceptorInvocation
|
164
|
-
attr_accessor :object, :method, :args, :options
|
165
|
-
def initialize(object, method, *args)
|
166
|
-
@object = object
|
167
|
-
@method = method
|
168
|
-
@args = args
|
169
|
-
@options = {}
|
170
|
-
end
|
171
|
-
def invoke(*args)
|
172
|
-
unless @options.nil? || @options.empty?
|
173
|
-
@args = @args + [@options]
|
174
|
-
@options = nil
|
175
|
-
end
|
176
|
-
@object.send("old4rinter_#{@method}".to_sym, *(args.empty? ? @args : args))
|
177
|
-
end
|
178
|
-
end
|
@@ -1,46 +1,46 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'rinterceptor'
|
3
|
-
module
|
3
|
+
module RinterceptorExample
|
4
4
|
def rinter_before(method, *args)
|
5
|
-
p "instance before -- method: #{method}, args: #{args} in
|
5
|
+
p "instance before -- method: #{method}, args: #{args} in RinterceptorExample"
|
6
6
|
end
|
7
|
-
def rinter_after(method, result,
|
8
|
-
p "instance after -- method: #{method}, result: #{result},
|
7
|
+
def rinter_after(method, result, exception, *args)
|
8
|
+
p "instance after -- method: #{method}, result: #{result}, exception: #{exception}, args: #{args} in RinterceptorExample"
|
9
9
|
result
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
13
|
def rinter_before(method, *args)
|
14
|
-
p "singleton before -- method: #{method}, args: #{args} in
|
14
|
+
p "singleton before -- method: #{method}, args: #{args} in RinterceptorExample"
|
15
15
|
end
|
16
|
-
def rinter_after(method, result,
|
17
|
-
p "singleton after -- method: #{method}, result: #{result},
|
16
|
+
def rinter_after(method, result, exception, *args)
|
17
|
+
p "singleton after -- method: #{method}, result: #{result}, exception: #{exception}, args: #{args} in RinterceptorExample"
|
18
18
|
end
|
19
19
|
end
|
20
20
|
include Rinterceptor
|
21
21
|
end
|
22
22
|
|
23
|
-
module
|
23
|
+
module SubRinterceptorExample
|
24
24
|
def self.rinter_before_include_class(base)
|
25
|
-
p "before_include_class in
|
25
|
+
p "before_include_class in SubRinterceptorExample"
|
26
26
|
#set interceptor rules of instance methods, the variable can also be defined in class before include this module
|
27
27
|
base.instance_variable_set(:@include_i_methods, {nil => [/^i2_/, /^i3_/], :handler_it => /^i1_/})
|
28
28
|
end
|
29
29
|
def self.rinter_after_include_class(base)
|
30
|
-
p "after_include_class in
|
30
|
+
p "after_include_class in SubRinterceptorExample"
|
31
31
|
end
|
32
32
|
def rinter_handler_it_around(invocation)
|
33
|
-
p "instance handler_it around begin -- method: #{invocation.method}, args: #{invocation.args} in
|
33
|
+
p "instance handler_it around begin -- method: #{invocation.method}, args: #{invocation.args} in SubRinterceptorExample"
|
34
34
|
result = invocation.invoke("inter", "ddddd")
|
35
|
-
p "instance handler_it around end -- method: #{invocation.method}, args: #{invocation.args} in
|
35
|
+
p "instance handler_it around end -- method: #{invocation.method}, args: #{invocation.args} in SubRinterceptorExample"
|
36
36
|
result
|
37
37
|
end
|
38
38
|
module ClassMethods
|
39
39
|
def rinter_before(method, *args)
|
40
|
-
p "singleton before -- method: #{method}, args: #{args} in
|
40
|
+
p "singleton before -- method: #{method}, args: #{args} in SubRinterceptorExample"
|
41
41
|
end
|
42
42
|
end
|
43
|
-
include
|
43
|
+
include RinterceptorExample
|
44
44
|
end
|
45
45
|
|
46
46
|
|
@@ -60,20 +60,23 @@ class TestRinterceptor
|
|
60
60
|
def self.s_test(x, y)
|
61
61
|
p "s_test x:#{x} y:#{y}"
|
62
62
|
end
|
63
|
-
#set interceptor rules of singleton methods, an alternative way is demonstrated in "self.rinter_before_include_class" of
|
63
|
+
#set interceptor rules of singleton methods, an alternative way is demonstrated in "self.rinter_before_include_class" of SubRinterceptorExample
|
64
64
|
#@include_s_methods value example:
|
65
65
|
#String(for exactly match)
|
66
66
|
#Regexp
|
67
67
|
#[Regexp]
|
68
68
|
#{} (unless key.nil? use rinter_#{key}_around to handle it, otherwise same behavior as before)
|
69
|
-
#refor to
|
69
|
+
#refor to SubRinterceptorExample
|
70
70
|
@include_s_methods = [/^s_/]
|
71
|
-
include
|
71
|
+
include SubRinterceptorExample
|
72
72
|
end
|
73
|
-
|
73
|
+
p "--------call TestRinterceptor.new.i1_test -------------------------------------"
|
74
74
|
TestRinterceptor.new.i1_test("xxx", "yyy")
|
75
|
+
p "--------call TestRinterceptor.new.i2_test -------------------------------------"
|
75
76
|
TestRinterceptor.new.i2_test("xxx", "yyy")
|
77
|
+
p "--------call TestRinterceptor.new.i3_test -------------------------------------"
|
76
78
|
TestRinterceptor.new.i3_test("xxx", "yyy")
|
79
|
+
p "--------call TestRinterceptor.s_test -------------------------------------"
|
77
80
|
TestRinterceptor.s_test("xxx", "yyy")
|
78
81
|
end
|
79
82
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module RinterceptorHelper
|
2
|
+
def self.match_method?(method, includes, excludes)
|
3
|
+
excludes.each {|i| return false if method =~ i}
|
4
|
+
real_includes = includes.is_a?(Array) ? includes : []
|
5
|
+
if includes.is_a?(Hash)
|
6
|
+
includes.each{|k, v| real_includes += v}
|
7
|
+
end
|
8
|
+
real_includes.each {|i| return true if method =~ i}
|
9
|
+
false
|
10
|
+
end
|
11
|
+
def self.process_include_methods(methods)
|
12
|
+
if methods.is_a?(Hash)
|
13
|
+
methods.each do |k, v|
|
14
|
+
if v.is_a?(String) || v.is_a?(Symbol)
|
15
|
+
v = Regexp.new("^#{v}$")
|
16
|
+
elsif v.is_a?(Array)
|
17
|
+
v.each_index{|i| v[i] = Regexp.new("^#{v[i]}$") if v[i].is_a?(String) || v[i].is_a?(Symbol)}
|
18
|
+
end
|
19
|
+
v = [v] if v.is_a?(Regexp)
|
20
|
+
methods[k] = v
|
21
|
+
end
|
22
|
+
end
|
23
|
+
methods
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class RinterceptorInvocation
|
2
|
+
attr_accessor :object, :method, :args, :options
|
3
|
+
def initialize(object, method, *args)
|
4
|
+
@object = object
|
5
|
+
@method = method
|
6
|
+
@args = args
|
7
|
+
@options = {}
|
8
|
+
end
|
9
|
+
def invoke(*args)
|
10
|
+
unless @options.nil? || @options.empty?
|
11
|
+
@args = @args + [@options]
|
12
|
+
@options = nil
|
13
|
+
end
|
14
|
+
@object.send("old4rinter_#{@method}".to_sym, *(args.empty? ? @args : args))
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rinterceptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leon Li
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-16 00:00:00 +08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,8 +25,10 @@ files:
|
|
25
25
|
- MIT-LICENSE
|
26
26
|
- Rakefile
|
27
27
|
- CHANGELOG
|
28
|
+
- lib/rinterceptor_helper.rb
|
29
|
+
- lib/rinterceptor_invocation.rb
|
28
30
|
- lib/rinterceptor.rb
|
29
|
-
- lib/
|
31
|
+
- lib/rinterceptor_example.rb
|
30
32
|
has_rdoc: false
|
31
33
|
homepage: http://rinter.rubyforge.org/
|
32
34
|
post_install_message:
|