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.
- #proxy_class_method(*original_method_names, &proxy_block) ⇒ Object
- #proxy_instance_method(*original_method_names, &proxy_block) ⇒ Object (also: #proxy_method)
-
#unproxied ⇒ Object
Return an unproxied version of this class.
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.
163 164 165 |
# File 'lib/proxy_method.rb', line 163 def proxied self.dup.send(:reproxy!) end |
#proxy_class_method(*original_method_names, &proxy_block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/proxy_method.rb', line 81 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| 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_instance_method(*original_method_names, &proxy_block) ⇒ Object Also known as: proxy_method
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/proxy_method.rb', line 112 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| 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 |
#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.
153 154 155 |
# File 'lib/proxy_method.rb', line 153 def unproxied self.dup.send(:unproxy!) end |