Class: ActiveRecordInstanceCount::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/active-record-instance-count.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
11
12
13
14
# File 'lib/active-record-instance-count.rb', line 7

def initialize(app, options = {})
  @app         = app
  unless options[:logger]
    raise 'Missing options[:logger] argument'
  end
  @logger      = options[:logger]
  ActiveRecord::Base.send(:include, ActiveRecordInstanceCount::Instrumentation::ActiveRecord) 
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
# File 'lib/active-record-instance-count.rb', line 16

def call(env)
  status, headers, body = @app.call(env)
  log_activerecord
  [status, headers, body]
end

#log_activerecordObject



22
23
24
25
26
27
# File 'lib/active-record-instance-count.rb', line 22

def log_activerecord
  sorted_list = ActiveRecordInstanceCount::HashUtils.to_sorted_array(ActiveRecord::Base.instantiated_hash)
  sorted_list.unshift("Total: #{ActiveRecord::Base.total_objects_instantiated}")
  @logger.info("Instantiation Breakdown: #{sorted_list.join(' | ')}")
  reset_objects_instantiated
end