proxy_method 1.2.3 → 1.2.5
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/lib/proxy_method/version.rb +1 -1
- data/lib/proxy_method.rb +90 -2
- 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: 30dca7d6e6868dad470c1c00056727bfc78c0c1ec457d95876c0fd42cd2b8bb5
|
4
|
+
data.tar.gz: d805267647962b76433660771e691090fd94b0c23643ae704916b11aaaa5fa65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a5f7ab8a5f2d0e03064d80635e2857588ba3f983a2de14f9acbf62d37736ec9dd835217304aa7c5414ab4be44a2408cc83771ccfd94a82dcdb2e697ed5a0e80
|
7
|
+
data.tar.gz: 547f5b2e3886c9b9841339aec6751393807375c772e7d2175c9617422c65eb2811fe2c7f88768fa19339e586bad1ba578ff67646131d0880b3afc1779a8f9201
|
data/lib/proxy_method/version.rb
CHANGED
data/lib/proxy_method.rb
CHANGED
@@ -3,6 +3,81 @@ module ProxyMethod
|
|
3
3
|
DEFAULT_PROXY_MESSAGE = 'Disabled by proxy_method'
|
4
4
|
DEFAULT_PREFIX = 'unproxied_'
|
5
5
|
|
6
|
+
##
|
7
|
+
# Proxy one or more inherited class methods, so that they are not used
|
8
|
+
# directly. Given this base class:
|
9
|
+
#
|
10
|
+
# class Animal
|
11
|
+
# def self.create
|
12
|
+
# 'created'
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# def destroy
|
16
|
+
# 'destroyed'
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# The simplest implementation is to pass just a single method name:
|
21
|
+
#
|
22
|
+
# class Dog < Animal
|
23
|
+
# proxy_class_method :create
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# Dog.create
|
27
|
+
# # => RuntimeError: Disabled by proxy_method
|
28
|
+
#
|
29
|
+
# Dog.destroy
|
30
|
+
# # 'destroyed'
|
31
|
+
#
|
32
|
+
# Or multiple method names:
|
33
|
+
#
|
34
|
+
# class Dog < Animal
|
35
|
+
# proxy_class_method :create, :destroy
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# Dog.create
|
39
|
+
# # => RuntimeError: Disabled by proxy_method
|
40
|
+
#
|
41
|
+
# Dog.destroy
|
42
|
+
# # => RuntimeError: Disabled by proxy_method
|
43
|
+
#
|
44
|
+
# With a custom error message:
|
45
|
+
#
|
46
|
+
# class Dog < Animal
|
47
|
+
# proxy_class_method :create, raise: 'Disabled!'
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# Dog.create
|
51
|
+
# # => RuntimeError: Disabled!
|
52
|
+
#
|
53
|
+
# You can still access the unproxied version by prefixing 'unproxied'
|
54
|
+
# to the method name:
|
55
|
+
#
|
56
|
+
# Dog.unproxied_create
|
57
|
+
# # => 'created'
|
58
|
+
#
|
59
|
+
# And you can change the prefix for unproxied versions:
|
60
|
+
#
|
61
|
+
# class Dog < Animal
|
62
|
+
# proxy_class_method :create, prefix: 'original_'
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# Dog.original_create
|
66
|
+
# # => 'created'
|
67
|
+
#
|
68
|
+
# Finally, you can actually *proxy* the method, by providing an
|
69
|
+
# alternative block of code to run:
|
70
|
+
#
|
71
|
+
# class Dog < Animal
|
72
|
+
# proxy_class_method(:create) do |object, method_name, *args, &block|
|
73
|
+
# "indirectly #{object.send(method_name)}"
|
74
|
+
# end
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# Dog.original_create
|
78
|
+
# # => 'indirectly created'
|
79
|
+
|
80
|
+
|
6
81
|
def proxy_class_method(*original_method_names, &proxy_block)
|
7
82
|
options = if original_method_names.last.is_a?(Hash)
|
8
83
|
original_method_names.pop
|
@@ -104,12 +179,12 @@ module ProxyMethod
|
|
104
179
|
@_proxied_class_methods ||= {}
|
105
180
|
end
|
106
181
|
|
107
|
-
def unproxy!
|
182
|
+
def unproxy!
|
108
183
|
@_proxy_class_methods_enabled = false
|
109
184
|
self
|
110
185
|
end
|
111
186
|
|
112
|
-
def reproxy!
|
187
|
+
def reproxy!
|
113
188
|
@_proxy_class_methods_enabled = true
|
114
189
|
self
|
115
190
|
end
|
@@ -119,10 +194,23 @@ module ProxyMethod
|
|
119
194
|
base.extend ClassMethods
|
120
195
|
end
|
121
196
|
|
197
|
+
##
|
198
|
+
# Return an unproxied version of this instance.
|
199
|
+
#
|
200
|
+
# This returns a copy of the instance where all proxies are disabled. This is
|
201
|
+
# sometimes necessary when a proxied method is being called by a different
|
202
|
+
# method outside your control.
|
203
|
+
|
122
204
|
def unproxied
|
123
205
|
self.dup.send(:unproxy!)
|
124
206
|
end
|
125
207
|
|
208
|
+
##
|
209
|
+
# Return a proxied version of this instance.
|
210
|
+
#
|
211
|
+
# If the instance has previously been "unproxied", this returns a
|
212
|
+
# copy where all proxies are re-enabled.
|
213
|
+
|
126
214
|
def proxied
|
127
215
|
self.dup.send(:reproxy!)
|
128
216
|
end
|
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.2.
|
4
|
+
version: 1.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jaime Bellmyer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-06 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
|