Module: ProxyMethod::ClassMethods
- Defined in:
- lib/proxy_method.rb
Constant Summary collapse
- DEFAULT_PROXY_MESSAGE =
'Disabled by proxy_method'- DEFAULT_PREFIX =
'unproxied_'
Instance Method Summary collapse
-
#proxied ⇒ Object
Return a proxied version of this class.
- #proxied_class_methods ⇒ Object
- #proxied_instance_methods ⇒ Object
- #proxy_class_method(*original_method_names, &proxy_block) ⇒ Object
- #proxy_class_methods_enabled? ⇒ Boolean
- #proxy_instance_method(*original_method_names, &proxy_block) ⇒ Object (also: #proxy_method)
-
#reproxy! ⇒ Object
:nodoc:.
-
#unproxied ⇒ Object
Return an unproxied version of this class.
-
#unproxy! ⇒ Object
:nodoc:.
Instance Method Details
#proxied ⇒ Object
Return a proxied version of this class.
If the class has previously been “unproxied”, this returns a copy where all proxies are re-enabled.
101 102 103 |
# File 'lib/proxy_method.rb', line 101 def proxied self.dup.reproxy! end |
#proxied_class_methods ⇒ Object
10 11 12 |
# File 'lib/proxy_method.rb', line 10 def proxied_class_methods @_proxied_class_methods ||= {} end |
#proxied_instance_methods ⇒ Object
6 7 8 |
# File 'lib/proxy_method.rb', line 6 def proxied_instance_methods @_proxied_instance_methods ||= {} end |
#proxy_class_method(*original_method_names, &proxy_block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/proxy_method.rb', line 19 def proxy_class_method(*original_method_names, &proxy_block) = if original_method_names.last.is_a?(Hash) original_method_names.pop else {} end original_method_names = Array(original_method_names).flatten = [:raise] || DEFAULT_PROXY_MESSAGE prefix = [:prefix] || DEFAULT_PREFIX original_method_names.each do |original_method_name| self.proxied_class_methods.merge!(original_method_name => prefix) new_method_name = :"#{prefix}#{original_method_name}" self.singleton_class.send(:alias_method, new_method_name, original_method_name) define_singleton_method(original_method_name) do |*args, &block| if proxy_class_methods_enabled? if proxy_block proxy_block.call(self.unproxied, original_method_name, *args, &block) else raise end else send(new_method_name, *args, &block) end end end end |
#proxy_class_methods_enabled? ⇒ Boolean
14 15 16 17 |
# File 'lib/proxy_method.rb', line 14 def proxy_class_methods_enabled? return @_proxy_class_methods_enabled if defined?(@_proxy_class_methods_enabled) @_proxy_class_methods_enabled = true end |
#proxy_instance_method(*original_method_names, &proxy_block) ⇒ Object Also known as: proxy_method
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/proxy_method.rb', line 50 def proxy_instance_method(*original_method_names, &proxy_block) = if original_method_names.last.is_a?(Hash) original_method_names.pop else {} end original_method_names = Array(original_method_names).flatten = [:raise] || DEFAULT_PROXY_MESSAGE prefix = [:prefix] || DEFAULT_PREFIX original_method_names.each do |original_method_name| self.proxied_instance_methods.merge!(original_method_name => prefix) new_method_name = :"#{prefix}#{original_method_name}" alias_method new_method_name, original_method_name define_method(original_method_name) do |*args, &block| if proxy_instance_methods_enabled? if proxy_block proxy_block.call(self.unproxied, original_method_name, *args, &block) else raise end else send(new_method_name, *args, &block) end end end end |
#reproxy! ⇒ Object
:nodoc:
110 111 112 113 |
# File 'lib/proxy_method.rb', line 110 def reproxy! # :nodoc: @_proxy_class_methods_enabled = true self end |
#unproxied ⇒ Object
Return an unproxied version of this class.
This returns a copy of the class where all proxies are disabled. This is sometimes necessary when a proxied method is being called by a different method outside your control.
91 92 93 |
# File 'lib/proxy_method.rb', line 91 def unproxied self.dup.unproxy! end |
#unproxy! ⇒ Object
:nodoc:
105 106 107 108 |
# File 'lib/proxy_method.rb', line 105 def unproxy! # :nodoc: @_proxy_class_methods_enabled = false self end |