Module: PolyDelegate
- Defined in:
- lib/poly_delegate/method.rb,
lib/poly_delegate/call.rb,
lib/poly_delegate/version.rb,
lib/poly_delegate/delegated.rb,
lib/poly_delegate/delegator.rb,
lib/poly_delegate/classic_access.rb
Overview
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Defined Under Namespace
Modules: ClassicAttributeAccess, Delegated
Classes: Delegator
Constant Summary
collapse
- VERSION =
'1.2.2'.freeze
Class Method Summary
collapse
-
.caller
-
.callers(num)
-
.create_delegated_method(klass, name)
-
.delegate_call(obj, method, *args, &block)
-
.distinguish_delegator(obj, *args)
-
.method_visibility(obj, method)
-
.redefine_method(obj, name, &block)
-
.set_method_visibility(obj, method, visibility)
Class Method Details
.caller
9
10
11
|
# File 'lib/poly_delegate/method.rb', line 9
def self.caller
callers(2).last
end
|
.callers(num)
13
14
15
|
# File 'lib/poly_delegate/method.rb', line 13
def self.callers(num)
caller_locations(2, num).map(&:label).map(&:to_sym)
end
|
.create_delegated_method(klass, name)
29
30
31
32
33
34
35
|
# File 'lib/poly_delegate/call.rb', line 29
def self.create_delegated_method(klass, name)
method = klass.instance_method(name)
redefine_method(klass, name) do |*args, &block|
PolyDelegate.delegate_call(self, method, *args, &block)
end
end
|
.delegate_call(obj, method, *args, &block)
23
24
25
26
27
|
# File 'lib/poly_delegate/call.rb', line 23
def self.delegate_call(obj, method, *args, &block)
delegator, args = distinguish_delegator(obj, *args)
bound = method.force_bind(delegator)
bound.call(*args, &block)
end
|
.distinguish_delegator(obj, *args)
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/poly_delegate/call.rb', line 12
def self.distinguish_delegator(obj, *args)
delegator =
if args.empty? || !args.first.is_a?(Delegator)
obj
else
args.shift
end
[delegator, args]
end
|
.method_visibility(obj, method)
17
18
19
20
21
22
23
24
25
|
# File 'lib/poly_delegate/method.rb', line 17
def self.method_visibility(obj, method)
if obj.public_method_defined? method
:public
elsif obj.private_method_defined? method
:private
elsif obj.protected_method_defined? method
:protected
end
end
|
.redefine_method(obj, name, &block)
31
32
33
34
35
36
|
# File 'lib/poly_delegate/method.rb', line 31
def self.redefine_method(obj, name, &block)
visibility = method_visibility(obj, name)
obj.__send__(:undef_method, name)
obj.__send__(:define_method, name, &block)
set_method_visibility(obj, name, visibility)
end
|
.set_method_visibility(obj, method, visibility)
27
28
29
|
# File 'lib/poly_delegate/method.rb', line 27
def self.set_method_visibility(obj, method, visibility)
obj.__send__ visibility, method
end
|