Module: ActiveSupport::Delegation
- Defined in:
- activesupport/lib/active_support/delegation.rb
Overview
:nodoc:
Defined Under Namespace
Classes: ClassDelegator
Constant Summary collapse
- RUBY_RESERVED_KEYWORDS =
%w(__ENCODING__ __LINE__ __FILE__ alias and BEGIN begin break case class def defined? do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield)
- RESERVED_METHOD_NAMES =
(RUBY_RESERVED_KEYWORDS + %w(_ arg args block)).to_set.freeze
Class Method Summary collapse
- .DelegateClass(superclass, &block) ⇒ Object
- .generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil, nilable: true, private: nil, as: nil, signature: nil) ⇒ Object
- .generate_method_missing(owner, target, allow_nil: nil) ⇒ Object
Class Method Details
.DelegateClass(superclass, &block) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'activesupport/lib/active_support/delegation.rb', line 199 def DelegateClass(superclass, &block) klass = Class.new(ClassDelegator) ignores = [*::Delegator.public_api, :to_s, :inspect, :=~, :!~, :===] protected_instance_methods = superclass.protected_instance_methods - ignores public_instance_methods = superclass.public_instance_methods - ignores delegated_methods = public_instance_methods + protected_instance_methods method_def = [] delegated_methods.each do |method_name| if method_name.match?(/\A[a-zA-Z]\w+[!?]?\z/) parameters = superclass.instance_method(method_name).parameters signature = if parameters.empty? "" elsif parameters.all? { |type, _name| type == :req || type == :keyreq || type == :block } anonymous = 0 parameters.map do |type, name| case type when :req name || "__anonymous_arg_#{anonymous += 1}" when :keyreq "#{name}:" when :block if name == :& name else "&#{name}" end end end.join(", ") else "..." end method_def << "def #{method_name}(#{signature})" << " __getobj__.#{method_name}(#{signature})" << "end" else method_def << "def #{method_name}(...)" << " __getobj__.send(#{method_name.inspect}, ...)" << "end" end end location = caller_locations(1, 1).first file, line = location.path, location.lineno klass.module_eval(method_def.join(";"), file, line) klass.class_eval(&block) if block klass end |
.generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil, nilable: true, private: nil, as: nil, signature: nil) ⇒ Object
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 63 64 65 66 67 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 94 95 96 97 98 99 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'activesupport/lib/active_support/delegation.rb', line 38 def generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil, nilable: true, private: nil, as: nil, signature: nil) unless to raise ArgumentError, "Delegation needs a target. Supply a keyword argument 'to' (e.g. delegate :hello, to: :greeter)." end if prefix == true && /^[^a-z_]/.match?(to) raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method." end method_prefix = \ if prefix "#{prefix == true ? to : prefix}_" else "" end location ||= caller_locations(1, 1).first file, line = location.path, location.lineno receiver = if to.is_a?(Module) if to.name.nil? raise ArgumentError, "Can't delegate to anonymous class or module: #{to}" end unless Inflector.safe_constantize(to.name).equal?(to) raise ArgumentError, "Can't delegate to detached class or module: #{to.name}" end "::#{to.name}" else to.to_s end receiver = "self.#{receiver}" if RESERVED_METHOD_NAMES.include?(receiver) explicit_receiver = false receiver_class = if as explicit_receiver = true as elsif to.is_a?(Module) to.singleton_class elsif receiver == "self.class" nilable = false # self.class can't possibly be nil owner.singleton_class end method_def = [] method_names = [] method_def << "self.private" if private methods.each do |method| method_name = prefix ? "#{method_prefix}#{method}" : method method_names << method_name.to_sym # Attribute writer methods only accept one argument. Makes sure []= # methods still accept two arguments. definition = \ if signature signature elsif /[^\]]=\z/.match?(method) "arg" else method_object = if receiver_class begin receiver_class.public_instance_method(method) rescue NameError raise if explicit_receiver # Do nothing. Fall back to `"..."` end end if method_object parameters = method_object.parameters if parameters.map(&:first).intersect?([:opt, :rest, :keyreq, :key, :keyrest]) "..." else defn = parameters.filter_map { |type, arg| arg if type == :req } defn << "&" defn.join(", ") end else "..." end end # The following generated method calls the target exactly once, storing # the returned value in a dummy variable. # # Reason is twofold: On one hand doing less calls is in general better. # On the other hand it could be that the target has side-effects, # whereas conceptually, from the user point of view, the delegator should # be doing one call. if nilable == false method_def << "def #{method_name}(#{definition})" << " (#{receiver}).#{method}(#{definition})" << "end" elsif allow_nil method = method.to_s method_def << "def #{method_name}(#{definition})" << " _ = #{receiver}" << " if !_.nil? || nil.respond_to?(:#{method})" << " _.#{method}(#{definition})" << " end" << "end" else method = method.to_s method_name = method_name.to_s method_def << "def #{method_name}(#{definition})" << " _ = #{receiver}" << " _.#{method}(#{definition})" << "rescue ::NoMethodError => e" << " if _.nil? && e.name == :#{method}" << " ::Kernel.raise ::ActiveSupport::DelegationError.nil_target(:#{method_name}, :'#{receiver}')" << " else" << " ::Kernel.raise" << " end" << "end" end end owner.module_eval(method_def.join(";"), file, line) method_names end |
.generate_method_missing(owner, target, allow_nil: nil) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'activesupport/lib/active_support/delegation.rb', line 167 def generate_method_missing(owner, target, allow_nil: nil) target = target.to_s target = "self.#{target}" if RESERVED_METHOD_NAMES.include?(target) || target == "__target" nil_behavior = if allow_nil "nil" else "::Kernel.raise ::ActiveSupport::DelegationError.nil_target(method, :'#{target}')" end owner.module_eval <<~RUBY, __FILE__, __LINE__ + 1 def respond_to_missing?(name, include_private = false) # It may look like an oversight, but we deliberately do not pass # +include_private+, because they do not get delegated. return false if name == :marshal_dump || name == :_dump #{target}.respond_to?(name) || super end def method_missing(method, ...) __target = #{target} if __target.nil? && !nil.respond_to?(method) #{nil_behavior} elsif __target.respond_to?(method) __target.public_send(method, ...) else super end end RUBY end |