Bläddra i källkod

Use inheritance instead of Module#alias_method_chain for
ActiveResource::Connection extensions

Daniel Vartanov 11 år sedan
förälder
incheckning
aba0fbe603
1 ändrade filer med 20 tillägg och 19 borttagningar
  1. 20 19
      lib/active_resource/connection_ext.rb

+ 20 - 19
lib/active_resource/connection_ext.rb

@@ -1,31 +1,32 @@
-require 'active_support/core_ext/module/aliasing'
-
 module ActiveResource
   class Connection
-
     attr_reader :response
 
-    def handle_response_with_response_capture(response)
-      @response = handle_response_without_response_capture(response)
+    module ResponseCapture
+      def handle_response(response)
+        @response = super
+      end
     end
 
-    def request_with_detailed_log_subscriber(method, path, *arguments)
-      result = request_without_detailed_log_subscriber(method, path, *arguments)
-      detailed_log_subscriber(result, arguments)
-      result
-    rescue => e
-      detailed_log_subscriber(e.response, arguments) if e.respond_to?(:response)
-      raise
-    end
+    module RequestNotification
+      def request(method, path, *arguments)
+        super.tap do |response|
+          notify_about_request(response, arguments)
+        end
+      rescue => e
+        notify_about_request(e.response, arguments) if e.respond_to?(:response)
+        raise
+      end
 
-    def detailed_log_subscriber(response, arguments)
-      ActiveSupport::Notifications.instrument("request.active_resource_detailed") do |payload|
-        payload[:response] = response
-        payload[:data]     = arguments
+      def notify_about_request(response, arguments)
+        ActiveSupport::Notifications.instrument("request.active_resource_detailed") do |payload|
+          payload[:response] = response
+          payload[:data]     = arguments
+        end
       end
     end
 
-    alias_method_chain :handle_response, :response_capture
-    alias_method_chain :request, :detailed_log_subscriber
+    prepend ResponseCapture
+    prepend RequestNotification
   end
 end