proxy_method 1.0.0 → 1.1.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.
- checksums.yaml +4 -4
- data/README.md +51 -0
- data/lib/proxy_method/version.rb +1 -1
- data/lib/proxy_method.rb +14 -6
- data/test/test_proxy_method.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bc049207e43c67354f9411eb043f1637cfd8cfc9c96214375527d5a9b077e5b
|
4
|
+
data.tar.gz: b64c28b819fe354192ee34f42fde428d4f27a0274ddd4da4cff6db2e9154b3f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a45bfc0ef058d8dd4b8a6fa24ffc2c9bbdc9abcd0b48085d59c4609cb855ee83b7a4ff8a0dc7dd9b723a1a516dbc053ccc0a08d7e0ab6ebe2bcd1c17e3f626a
|
7
|
+
data.tar.gz: 85098afdef7599ddfb4f910ffa56dbaa8d9b276f0fbd84a272a477603bec55904c2be03de5b432a551fa9c0833cd037666497c1742906a3da788c9bdd0e9867c
|
data/README.md
CHANGED
@@ -128,6 +128,57 @@ You can also use an unproxied version of the entire class:
|
|
128
128
|
duck_unproxied.save
|
129
129
|
# => 'saved'
|
130
130
|
|
131
|
+
Run a custom block, instead of raising an exception at all. In other words, *actually* proxy the
|
132
|
+
method! The object which is passed to the block is already unproxied, you don't have to do this
|
133
|
+
yourself:
|
134
|
+
|
135
|
+
class MethodicalMeerkat < Animal
|
136
|
+
include ProxyMethod
|
137
|
+
|
138
|
+
proxy_method(:save) do |unproxied_object|
|
139
|
+
"indirectly #{unproxied_object.save}!"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
MethodicalMeerkat.new.save
|
144
|
+
# => 'indirectly saved!'
|
145
|
+
|
146
|
+
The object which is passed to the block is already unproxied, so it's free to use as you'd expect.
|
147
|
+
|
148
|
+
From there, you can allow your proxied block it to be used for multiple methods:
|
149
|
+
|
150
|
+
class MethodicalMeerkat < Animal
|
151
|
+
include ProxyMethod
|
152
|
+
|
153
|
+
proxy_method([:save, :update]) do |object, method_name|
|
154
|
+
"indirectly #{object.send(method_name)}!"
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
MethodicalMeerkat.new.save
|
159
|
+
# => 'indirectly saved!'
|
160
|
+
|
161
|
+
MethodicalMeerkat.new.update
|
162
|
+
# => 'indirectly updated!'
|
163
|
+
|
164
|
+
Provide your proxied block with args and a block of its own. The block will be given the
|
165
|
+
unproxied object itself, followed by the standard arguments to `method_missing`,
|
166
|
+
so it works in much the same way.
|
167
|
+
|
168
|
+
class MethodicalMeerkat < Animal
|
169
|
+
include ProxyMethod
|
170
|
+
|
171
|
+
proxy_method(:save) do |object, method_name, *args, &block|
|
172
|
+
"indirectly #{object.send(method_name, *args, &block)}!"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
MethodicalMeerkat.new.save
|
177
|
+
# => 'indirectly saved!'
|
178
|
+
|
179
|
+
MethodicalMeerkat.new.update
|
180
|
+
# => 'indirectly updated!'
|
181
|
+
|
131
182
|
|
132
183
|
## Installation
|
133
184
|
Add this line to your application's Gemfile:
|
data/lib/proxy_method/version.rb
CHANGED
data/lib/proxy_method.rb
CHANGED
@@ -16,7 +16,7 @@ module ProxyMethod
|
|
16
16
|
@_proxy_class_methods_enabled = true
|
17
17
|
end
|
18
18
|
|
19
|
-
def proxy_class_method(original_method_names, options = {})
|
19
|
+
def proxy_class_method(original_method_names, options = {}, &proxy_block)
|
20
20
|
original_method_names = Array(original_method_names)
|
21
21
|
|
22
22
|
error_message = options[:raise] || DEFAULT_PROXY_MESSAGE
|
@@ -29,7 +29,11 @@ module ProxyMethod
|
|
29
29
|
self.singleton_class.send(:alias_method, new_method_name, original_method_name)
|
30
30
|
define_singleton_method(original_method_name) do |*args, &block|
|
31
31
|
if proxy_class_methods_enabled?
|
32
|
-
|
32
|
+
if proxy_block
|
33
|
+
proxy_block.call(self.unproxied, original_method_name, *args, &block)
|
34
|
+
else
|
35
|
+
raise error_message
|
36
|
+
end
|
33
37
|
else
|
34
38
|
send(new_method_name, *args, &block)
|
35
39
|
end
|
@@ -37,7 +41,7 @@ module ProxyMethod
|
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
40
|
-
def proxy_instance_method(original_method_names, options = {})
|
44
|
+
def proxy_instance_method(original_method_names, options = {}, &proxy_block)
|
41
45
|
original_method_names = Array(original_method_names)
|
42
46
|
|
43
47
|
error_message = options[:raise] || DEFAULT_PROXY_MESSAGE
|
@@ -51,7 +55,11 @@ module ProxyMethod
|
|
51
55
|
|
52
56
|
define_method(original_method_name) do |*args, &block|
|
53
57
|
if proxy_instance_methods_enabled?
|
54
|
-
|
58
|
+
if proxy_block
|
59
|
+
proxy_block.call(self.unproxied, original_method_name, *args, &block)
|
60
|
+
else
|
61
|
+
raise error_message
|
62
|
+
end
|
55
63
|
else
|
56
64
|
send(new_method_name, *args, &block)
|
57
65
|
end
|
@@ -66,7 +74,7 @@ module ProxyMethod
|
|
66
74
|
end
|
67
75
|
|
68
76
|
def proxied
|
69
|
-
self.dup.
|
77
|
+
self.dup.reproxy!
|
70
78
|
end
|
71
79
|
|
72
80
|
def unproxy!
|
@@ -94,7 +102,7 @@ module ProxyMethod
|
|
94
102
|
end
|
95
103
|
|
96
104
|
def proxied
|
97
|
-
self.dup.
|
105
|
+
self.dup.reproxy!
|
98
106
|
end
|
99
107
|
|
100
108
|
def unproxy!
|
data/test/test_proxy_method.rb
CHANGED
@@ -62,6 +62,18 @@ class ArgumentativeAardvark < Animal
|
|
62
62
|
proxy_method :blocky
|
63
63
|
end
|
64
64
|
|
65
|
+
class MethodicalMeerkat < Animal
|
66
|
+
include ProxyMethod
|
67
|
+
|
68
|
+
proxy_class_method(:create) do |klass, method_name, *args, &block|
|
69
|
+
'indirectly ' + klass.send(method_name, *args, &block) + '!'
|
70
|
+
end
|
71
|
+
|
72
|
+
proxy_method(:save) do |object, method_name, *args, &block|
|
73
|
+
'indirectly ' + object.send(method_name, *args, &block) + '!'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
65
77
|
class ProxyMethodTest < MiniTest::Test
|
66
78
|
describe "proxying class methods" do
|
67
79
|
it "does not allow original method name to be called" do
|
@@ -94,6 +106,10 @@ class ProxyMethodTest < MiniTest::Test
|
|
94
106
|
assert_equal "Disabled by proxy_method", exception.message
|
95
107
|
end
|
96
108
|
|
109
|
+
it "allows an alternate block to be run instead of an exception" do
|
110
|
+
assert_equal 'indirectly created!', MethodicalMeerkat.create
|
111
|
+
end
|
112
|
+
|
97
113
|
it "allows for multiple methods to be proxied in one call" do
|
98
114
|
exception = assert_raises StandardError do
|
99
115
|
MultiMonkey.create
|
@@ -190,6 +206,10 @@ class ProxyMethodTest < MiniTest::Test
|
|
190
206
|
assert_equal "Disabled by proxy_method", exception.message
|
191
207
|
end
|
192
208
|
|
209
|
+
it "allows an alternate block to be run instead of an exception" do
|
210
|
+
assert_equal 'indirectly saved!', MethodicalMeerkat.new.save
|
211
|
+
end
|
212
|
+
|
193
213
|
it "allows for multiple methods to be proxied in one call" do
|
194
214
|
exception = assert_raises StandardError do
|
195
215
|
MultiMonkey.new.save
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proxy_method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jaime Bellmyer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
The purpose of this gem is to prevent directly running the inherited
|