@@ -151,37 +151,51 @@ def generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil
151
151
152
152
def generate_method_missing ( owner , target , allow_nil : nil )
153
153
target = target . to_s
154
- target = "self.#{ target } " if RESERVED_METHOD_NAMES . include? ( target )
154
+ target = "self.#{ target } " if RESERVED_METHOD_NAMES . include? ( target ) || target == "__target"
155
155
156
- owner . module_eval <<-RUBY , __FILE__ , __LINE__ + 1
157
- def respond_to_missing?(name, include_private = false)
158
- # It may look like an oversight, but we deliberately do not pass
159
- # +include_private+, because they do not get delegated.
156
+ if allow_nil
157
+ owner . module_eval <<~RUBY , __FILE__ , __LINE__ + 1
158
+ def respond_to_missing?(name, include_private = false)
159
+ # It may look like an oversight, but we deliberately do not pass
160
+ # +include_private+, because they do not get delegated.
160
161
161
- return false if name == :marshal_dump || name == :_dump
162
- #{ target } .respond_to?(name) || super
163
- end
162
+ return false if name == :marshal_dump || name == :_dump
163
+ #{ target } .respond_to?(name) || super
164
+ end
164
165
165
- def method_missing(method, ...)
166
- if #{ target } .respond_to?(method)
167
- #{ target } .public_send(method, ...)
168
- else
169
- begin
166
+ def method_missing(method, ...)
167
+ __target = #{ target }
168
+ if __target.nil? && !nil.respond_to?(method)
169
+ nil
170
+ elsif __target.respond_to?(method)
171
+ __target.public_send(method, ...)
172
+ else
170
173
super
171
- rescue NoMethodError
172
- if #{ target } .nil?
173
- if #{ allow_nil == true }
174
- nil
175
- else
176
- raise ::ActiveSupport::DelegationError.nil_target(method, :'#{ target } ')
177
- end
178
- else
179
- raise
180
- end
181
174
end
182
175
end
183
- end
184
- RUBY
176
+ RUBY
177
+ else
178
+ owner . module_eval <<~RUBY , __FILE__ , __LINE__ + 1
179
+ def respond_to_missing?(name, include_private = false)
180
+ # It may look like an oversight, but we deliberately do not pass
181
+ # +include_private+, because they do not get delegated.
182
+
183
+ return false if name == :marshal_dump || name == :_dump
184
+ #{ target } .respond_to?(name) || super
185
+ end
186
+
187
+ def method_missing(method, ...)
188
+ __target = #{ target }
189
+ if __target.nil? && !nil.respond_to?(method)
190
+ raise ::ActiveSupport::DelegationError.nil_target(method, :'#{ target } ')
191
+ elsif __target.respond_to?(method)
192
+ __target.public_send(method, ...)
193
+ else
194
+ super
195
+ end
196
+ end
197
+ RUBY
198
+ end
185
199
end
186
200
end
187
201
end
0 commit comments