Module: Forwardable::Extended
- Defined in:
- lib/forwardable/extended.rb,
lib/forwardable/extended/version.rb
Constant Summary collapse
- VERSION =
"2.5.1"
Class Method Summary collapse
-
.extended(klass) ⇒ Object
Make our methods private on the class, there is no reason for public.
Instance Method Summary collapse
-
#def_delegator(accessor, method, alias_ = method, **kwd) ⇒ Object
Wraps around traditional delegation and modern delegation.
-
#def_delegators(accessor, *methods) ⇒ Object
Create multiple delegates at once.
-
#def_hash_delegator(hash, method, key: method, **kwd) ⇒ Object
Delegate a method to a hash and key.
-
#def_ivar_delegator(ivar, alias_ = ivar, **kwd) ⇒ Object
Delegate a method to an instance variable.
-
#def_modern_delegator(accessor, method, alias_ = method, args: [], **kwd) ⇒ Object
method - the method you wish to delegate.
-
#rb_delegate(method, to: nil, alias_of: method, **kwd) ⇒ Object
Delegate using a Rails-like interface.
Class Method Details
.extended(klass) ⇒ Object
Make our methods private on the class, there is no reason for public. This makes it so that things are cleaner to view when inside of REPL’s/Debuggers. That way you don’t see our methods without asking to see them.
14 15 16 17 18 19 20 |
# File 'lib/forwardable/extended.rb', line 14 def self.extended(klass) instance_methods.each do |method| klass.private_class_method( method ) end end |
Instance Method Details
#def_delegator(accessor, method, alias_ = method, **kwd) ⇒ Object
Wraps around traditional delegation and modern delegation.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/forwardable/extended.rb', line 129 def def_delegator(accessor, method, alias_ = method, **kwd) kwd, alias_ = alias_, method if alias_.is_a?(Hash) && !kwd.any? if alias_.is_a?(Hash) || !kwd.any? Forwardable.instance_method(:def_delegator).bind(self) \ .call(accessor, method, alias_) elsif !kwd[:type] def_modern_delegator( accessor, method, alias_, **kwd ) else raise ArgumentError, "Alias not supported." if alias_ != method send("def_#{kwd[:type]}_delegator", accessor, method, **kwd.tap do |obj| obj.delete(:type) end) end end |
#def_delegators(accessor, *methods) ⇒ Object
Create multiple delegates at once.
150 151 152 153 154 155 156 157 158 |
# File 'lib/forwardable/extended.rb', line 150 def def_delegators(accessor, *methods) kwd = methods.shift if methods.first.is_a?(Hash) kwd = methods.pop if methods. last.is_a?(Hash) kwd = {} unless kwd methods.each do |method| def_delegator accessor, method, **kwd end end |
#def_hash_delegator(hash, method, key: method, **kwd) ⇒ Object
Delegate a method to a hash and key. key - the key to use if the method is not the key. method - the name of the method you wish to create. hash - the hash you are delegating to.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/forwardable/extended.rb', line 37 def def_hash_delegator(hash, method, key: method, **kwd) prefix, suffix, wrap = prepare_delegate(**kwd) if suffix method = method.to_s.gsub( /\?$/, "" ) end class_eval <<-STR, __FILE__, __LINE__ - 9 def #{method}#{suffix}(*args) #{wrap}( #{prefix}#{hash}[#{key.inspect}] ) rescue Exception unless Forwardable.debug [email protected]_if do |source| source =~ %r"#{Regexp.escape(__FILE__)}"o end end raise end STR end |
#def_ivar_delegator(ivar, alias_ = ivar, **kwd) ⇒ Object
Delegate a method to an instance variable. ivar - the instance variable you are aliasing. alias_ - alias it to another method name. Note: If you are not doing booleans then don’t bother with this.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/forwardable/extended.rb', line 68 def def_ivar_delegator(ivar, alias_ = ivar, **kwd) prefix, suffix, wrap = prepare_delegate(**kwd) if suffix alias_ = alias_.to_s.gsub( /\?$/, "" ) end class_eval <<-STR, __FILE__, __LINE__ - 9 def #{alias_.to_s.gsub(/\A@/, "")}#{suffix} #{wrap}( #{prefix}#{ivar} ) rescue Exception unless Forwardable.debug [email protected]_if do |source| source =~ %r"#{Regexp.escape(__FILE__)}"o end end raise end STR end |
#def_modern_delegator(accessor, method, alias_ = method, args: [], **kwd) ⇒ Object
method - the method you wish to delegate. alias_ - the name of the method on the current object. args - arguments to pass to the method. Note: these are inspected. Like def_delegator but allows you to send args and do other stuff. accessor - the object you wish to delegate to.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/forwardable/extended.rb', line 100 def def_modern_delegator(accessor, method, alias_ = method, args: [], **kwd) args = [args].flatten.compact.map(&:to_s).unshift("").join(", ") prefix, suffix, wrap = prepare_delegate(**kwd) if suffix alias_ = alias_.to_s.gsub( /\?$/, "" ) end class_eval <<-STR, __FILE__, __LINE__ - 10 def #{alias_}#{suffix}(*args, &block) #{wrap}(#{prefix}#{accessor}.send( #{method.inspect}#{args}, *args, &block )) rescue Exception unless Forwardable.debug [email protected]_if do |source| source =~ %r"#{Regexp.escape(__FILE__)}"o end end raise end STR end |
#rb_delegate(method, to: nil, alias_of: method, **kwd) ⇒ Object
Delegate using a Rails-like interface. to - the class (object) you are delegating to. alias_of - the method of the class (by default it’s the method.) method - the method you are forwarding.
26 27 28 29 30 31 |
# File 'lib/forwardable/extended.rb', line 26 def rb_delegate(method, to: nil, alias_of: method, **kwd) raise ArgumentError, "to must be provided" unless to def_delegator( to, alias_of, method, **kwd ) end |