Skip to content
This repository
Browse code

define the delegate methods on one line. fixes #13724

sup haters
  • Loading branch information...
commit 44c51fc9cc7a0cdcf8b657e1900eb141083ef683 1 parent 73ba2c1
Aaron Patterson authored January 16, 2014
42  activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -178,30 +178,32 @@ def delegate(*methods)
178 178
       # whereas conceptually, from the user point of view, the delegator should
179 179
       # be doing one call.
180 180
       if allow_nil
181  
-        module_eval(<<-EOS, file, line - 3)
182  
-          def #{method_prefix}#{method}(#{definition})        # def customer_name(*args, &block)
183  
-            _ = #{to}                                         #   _ = client
184  
-            if !_.nil? || nil.respond_to?(:#{method})         #   if !_.nil? || nil.respond_to?(:name)
185  
-              _.#{method}(#{definition})                      #     _.name(*args, &block)
186  
-            end                                               #   end
187  
-          end                                                 # end
188  
-        EOS
  181
+        method_def = [
  182
+          "def #{method_prefix}#{method}(#{definition})",  # def customer_name(*args, &block)
  183
+          "_ = #{to}",                                     #   _ = client
  184
+          "if !_.nil? || nil.respond_to?(:#{method})",     #   if !_.nil? || nil.respond_to?(:name)
  185
+          "  _.#{method}(#{definition})",                  #     _.name(*args, &block)
  186
+          "end",                                           #   end
  187
+        "end"                                              # end
  188
+        ].join ';'
189 189
       else
190 190
         exception = %(raise DelegationError, "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
191 191
 
192  
-        module_eval(<<-EOS, file, line - 2)
193  
-          def #{method_prefix}#{method}(#{definition})                                          # def customer_name(*args, &block)
194  
-            _ = #{to}                                                                           #   _ = client
195  
-            _.#{method}(#{definition})                                                          #   _.name(*args, &block)
196  
-          rescue NoMethodError => e                                                             # rescue NoMethodError => e
197  
-            if _.nil? && e.name == :#{method}                                                   #   if _.nil? && e.name == :name
198  
-              #{exception}                                                                      #     # add helpful message to the exception
199  
-            else                                                                                #   else
200  
-              raise                                                                             #     raise
201  
-            end                                                                                 #   end
202  
-          end                                                                                   # end
203  
-        EOS
  192
+        method_def = [
  193
+          "def #{method_prefix}#{method}(#{definition})",  # def customer_name(*args, &block)
  194
+          " _ = #{to}",                                    #   _ = client
  195
+          "  _.#{method}(#{definition})",                  #   _.name(*args, &block)
  196
+          "rescue NoMethodError => e",                     # rescue NoMethodError => e
  197
+          "  if _.nil? && e.name == :#{method}",           #   if _.nil? && e.name == :name
  198
+          "    #{exception}",                              #     # add helpful message to the exception
  199
+          "  else",                                        #   else
  200
+          "    raise",                                     #     raise
  201
+          "  end",                                         #   end
  202
+          "end"                                            # end
  203
+        ].join ';'
204 204
       end
  205
+
  206
+      module_eval(method_def, file, line)
205 207
     end
206 208
   end
207 209
 end
10  activesupport/test/core_ext/module_test.rb
@@ -245,6 +245,16 @@ class << self
245 245
     end
246 246
   end
247 247
 
  248
+  def test_delegation_line_number
  249
+    file, line = Someone.instance_method(:foo).source_location
  250
+    assert_equal Someone::FAILED_DELEGATE_LINE, line
  251
+  end
  252
+
  253
+  def test_delegate_line_with_nil
  254
+    file, line = Someone.instance_method(:bar).source_location
  255
+    assert_equal Someone::FAILED_DELEGATE_LINE_2, line
  256
+  end
  257
+
248 258
   def test_delegation_exception_backtrace
249 259
     someone = Someone.new("foo", "bar")
250 260
     someone.foo

1 note on commit 44c51fc

Adrian Mugnolo

@tenderlove Hey, so long. I know indentation doesn't make a lot of sense in this context -other than documentation- but, these two cases seem inconsistent between them. But again, maybe I'm missing something.

Please sign in to comment.
Something went wrong with that request. Please try again.