Skip to content
This repository
Browse code

Minor refactoring of Oink::Middleware internal methods

  • Loading branch information...
commit 9a9140aa40643357f1ce1eaf826a42b6c7ac8c59 1 parent 0ebe10e
authored April 01, 2011

Showing 1 changed file with 34 additions and 25 deletions. Show diff stats Hide diff stats

  1. 59  lib/oink/middleware.rb
59  lib/oink/middleware.rb
@@ -8,51 +8,60 @@ class Middleware
8 8
     DEFAULT_LOG_PATH = "log/oink.log"
9 9
 
10 10
     def initialize(app, options = {})
11  
-      @options = options
12  
-      @options[:log_path] ||= DEFAULT_LOG_PATH
13  
-      @options[:instruments] ||= [:memory, :activerecord]
14  
-      ActiveRecord::Base.send(:include, Oink::Instrumentation::ActiveRecord) if instrumented?(:activerecord)
15  
-      @logger = Hodel3000CompliantLogger.new(@options[:log_path])
16  
-      @app = app
  11
+      @app         = app
  12
+      @log_path    = options[:log_path]    || DEFAULT_LOG_PATH
  13
+      @instruments = options[:instruments] || [:memory, :activerecord]
  14
+
  15
+      ActiveRecord::Base.send(:include, Oink::Instrumentation::ActiveRecord) if @instruments.include?(:activerecord)
17 16
     end
18 17
 
19 18
     def call(env)
20 19
       status, headers, body = @app.call(env)
21  
-      log_routing_information(env)
22  
-      @logger.info("Completed in")
23  
-      log_memory_snapshot if instrumented?(:memory)
24  
-      log_objects_instantiated if instrumented?(:activerecord)
  20
+
  21
+      log_routing(env)
  22
+      log_completed
  23
+      log_memory
  24
+      log_activerecord
  25
+
25 26
       reset_objects_instantiated
26 27
       [status, headers, body]
27 28
     end
28 29
 
29  
-    def log_routing_information(env)
  30
+    def log_completed
  31
+      logger.info("Completed in")
  32
+    end
  33
+
  34
+    def log_routing(env)
30 35
       if env.has_key?('action_dispatch.request.parameters')
31 36
         controller = env['action_dispatch.request.parameters']['controller']
32  
-        action = env['action_dispatch.request.parameters']['action']
33  
-        @logger.info "Processing #{controller}##{action}"
  37
+        action     = env['action_dispatch.request.parameters']['action']
  38
+        logger.info("Processing #{controller}##{action}")
34 39
       end
35 40
     end
36 41
 
37  
-    def log_memory_snapshot
38  
-      memory = Oink::Instrumentation::MemorySnapshot.memory
39  
-      @logger.info("Memory usage: #{memory} | PID: #{$$}")
  42
+    def log_memory
  43
+      if @instruments.include?(:memory)
  44
+        memory = Oink::Instrumentation::MemorySnapshot.memory
  45
+        logger.info("Memory usage: #{memory} | PID: #{$$}")
  46
+      end
40 47
     end
41 48
 
42  
-    def log_objects_instantiated
43  
-      sorted_list = Oink::HashUtils.to_sorted_array(ActiveRecord::Base.instantiated_hash)
44  
-      sorted_list.unshift("Total: #{ActiveRecord::Base.total_objects_instantiated}")
45  
-      @logger.info("Instantiation Breakdown: #{sorted_list.join(' | ')}")
  49
+    def log_activerecord
  50
+      if @instruments.include?(:activerecord)
  51
+        sorted_list = Oink::HashUtils.to_sorted_array(ActiveRecord::Base.instantiated_hash)
  52
+        sorted_list.unshift("Total: #{ActiveRecord::Base.total_objects_instantiated}")
  53
+        logger.info("Instantiation Breakdown: #{sorted_list.join(' | ')}")
  54
+      end
46 55
     end
47 56
 
  57
+  private
  58
+
48 59
     def reset_objects_instantiated
49 60
       ActiveRecord::Base.reset_instance_type_count
50 61
     end
51 62
 
52  
-  private
53  
-
54  
-    def instrumented?(instrument)
55  
-      @options[:instruments].include?(instrument)
  63
+    def logger
  64
+      @logger ||= Hodel3000CompliantLogger.new(@log_path)
56 65
     end
57 66
   end
58  
-end
  67
+end

0 notes on commit 9a9140a

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