Module: ClosureForwardable
- Defined in:
- lib/closure_forwardable.rb,
lib/closure_forwardable/version.rb
Overview
The ClosureForwardable module provides delegation of specified methods to a designated object, using the methods #def_delegator and #def_delegators.
This module is intended to be used very similar to the Forwardable module in
the Ruby standard library. For basic usage guidelines, see there. Generally,
you should use the simple Forwardable module if possible as method calls
will be slightly faster while providing the same functionality.
Use ClosureForwardable if you need to forward methods to a receiver that is not available by the including module itself. Using ClosureForwardable, you can forward methods to arbitrary objects.
Defined Under Namespace
Modules: Version
Constant Summary collapse
- FILE_REGEXP =
A regular expression matching the current file so that we can filter backtraces
Regexp.new(Regexp.escape(__FILE__))
- VERSION =
The ClosureForwardable version as a semver-compliant string
Version::STRING
Class Attribute Summary collapse
-
.debug ⇒ Object
If true,
__FILE__will remain in the backtrace in the event an exception is raised.
Instance Method Summary collapse
-
#closure_delegate(hash) ⇒ void
(also: #delegate)
Takes a hash as its argument.
-
#def_closure_delegator(receiver, method, method_alias = method) ⇒ void
(also: #def_delegator)
Define
methodas delegator instance method with an optional alias nameali. -
#def_closure_delegators(receiver, *methods) ⇒ void
(also: #def_delegators)
Shortcut for defining multiple delegator methods, but with no provision for using a different name.
Class Attribute Details
.debug ⇒ Object
If true, __FILE__ will remain in the backtrace in the event an exception
is raised.
25 26 27 |
# File 'lib/closure_forwardable.rb', line 25 def debug @debug end |
Instance Method Details
#closure_delegate(hash) ⇒ void Also known as: delegate
This method returns an undefined value.
Takes a hash as its argument. The key is a symbol or an array of symbols. These symbols correspond to method names. The value is the receiver to which the methods will be delegated.
34 35 36 37 38 39 40 41 |
# File 'lib/closure_forwardable.rb', line 34 def closure_delegate(hash) hash.each do |methods, receiver| methods = [methods] unless methods.respond_to?(:each) methods.each do |method| def_closure_delegator(receiver, method) end end end |
#def_closure_delegator(receiver, method, method_alias = method) ⇒ void Also known as: def_delegator
This method returns an undefined value.
Define method as delegator instance method with an optional alias name
ali. Method calls to ali will be delegated to receiver.method.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/closure_forwardable.rb', line 98 def def_closure_delegator(receiver, method, method_alias = method) define_method(method_alias) do |*args, &block| begin receiver.__send__(method, *args, &block) rescue Exception unless ::ClosureForwardable.debug $ERROR_POSITION.delete_if do |error_line| ::ClosureForwardable::FILE_REGEXP =~ error_line end end ::Kernel.raise end end end |
#def_closure_delegators(receiver, *methods) ⇒ void Also known as: def_delegators
This method returns an undefined value.
Shortcut for defining multiple delegator methods, but with no provision for using a different name.
59 60 61 62 63 64 65 |
# File 'lib/closure_forwardable.rb', line 59 def def_closure_delegators(receiver, *methods) excluded_methods = ['__send__'.freeze, '__id__'.freeze] methods.each do |method| next if excluded_methods.include?(method.to_s) def_closure_delegator(receiver, method) end end |