Add Rubocop and Enable Performance Cops (#364)

Add Rubocop and Enable Performance Cops (#364)

Found 2 offenses on first run:

$ rubocop --auto-correct
Inspecting 40 files
...........C.......................C....

Offenses:

lib/patches/db/nobrainer.rb:20:36: C: [Corrected] Performance/StringReplacement: Use delete instead of gsub.
      query << env[:query].inspect.gsub(/\n/, '').gsub(/ +/, ' ') + " "
                                   ^^^^^^^^^^^^^^
lib/mini_profiler/storage/file_store.rb:33:48: C: [Corrected] Performance/StringReplacement: Use tr instead of gsub.
            @path + "/" + @prefix  + "_" + key.gsub(/:/, '_')
                                               ^^^^^^^^^^^^^^

40 files inspected, 2 offenses detected, 2 offenses corrected
From d1dc6a4b2d185ecbe6ae76b7cecdf2bfe0e0df26 Mon Sep 17 00:00:00 2001
From: Richard Schneeman <richard.schneeman+no-recruiters@gmail.com>
Date: Thu, 13 Sep 2018 21:58:20 -0500
Subject: [PATCH] Add Rubocop and Enable Performance Cops (#364)

Found 2 offenses on first run:

`‍``
$ rubocop --auto-correct
Inspecting 40 files
...........C.......................C....

Offenses:

lib/patches/db/nobrainer.rb:20:36: C: [Corrected] Performance/StringReplacement: Use delete instead of gsub.
      query << env[:query].inspect.gsub(/\n/, '').gsub(/ +/, ' ') + " "
                                   ^^^^^^^^^^^^^^
lib/mini_profiler/storage/file_store.rb:33:48: C: [Corrected] Performance/StringReplacement: Use tr instead of gsub.
            @path + "/" + @prefix  + "_" + key.gsub(/:/, '_')
                                               ^^^^^^^^^^^^^^

40 files inspected, 2 offenses detected, 2 offenses corrected
`‍``

diff --git a/.rubocop.yml b/.rubocop.yml
new file mode 100644
index 0000000..8706ce1
--- /dev/null
+++ b/.rubocop.yml
@@ -0,0 +1,8 @@
+AllCops:
+  DisabledByDefault: true
+  TargetRubyVersion: 2.2
+  Include:
+    - 'lib/**/*.rb'
+
+Performance:
+  Enabled: true
diff --git a/Rakefile b/Rakefile
index 295b1dc..6302a93 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,11 @@ require 'bundler/gem_tasks'
 
 Bundler.setup(:default, :test)
 
-task :default => [:spec]
+require 'rubocop/rake_task'
+
+RuboCop::RakeTask.new
+
+task :default => [:rubocop, :spec]
 
 require 'rspec/core'
 require 'rspec/core/rake_task'
diff --git a/lib/mini_profiler/storage/file_store.rb b/lib/mini_profiler/storage/file_store.rb
index 304a513..4c4f118 100644
--- a/lib/mini_profiler/storage/file_store.rb
+++ b/lib/mini_profiler/storage/file_store.rb
@@ -33,7 +33,7 @@ module Rack
         private
         if RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/
           def path(key)
-            @path + "/" + @prefix  + "_" + key.gsub(/:/, '_')
+            @path + "/" + @prefix  + "_" + key.tr(':', '_')
           end
         else
           def path(key)
diff --git a/lib/patches/db/nobrainer.rb b/lib/patches/db/nobrainer.rb
index 70106e0..2ffc73b 100644
--- a/lib/patches/db/nobrainer.rb
+++ b/lib/patches/db/nobrainer.rb
@@ -17,7 +17,7 @@ class Rack::MiniProfiler::NoBrainerProfiler
       # query << "(#{NoBrainer::RQL.type_of(env[:query]).to_s}) "
 
       query << "NOT USING INDEX: " if not_indexed
-      query << env[:query].inspect.gsub(/\n/, '').gsub(/ +/, ' ') + " "
+      query << env[:query].inspect.delete("\n").gsub(/ +/, ' ') + " "
 
       if env[:exception]
         query << "exception: #{env[:exception].class} #{env[:exception].message.split("\n").first} "
diff --git a/rack-mini-profiler.gemspec b/rack-mini-profiler.gemspec
index a8d13de..9d5b404 100644
--- a/rack-mini-profiler.gemspec
+++ b/rack-mini-profiler.gemspec
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
   s.add_development_dependency 'redis'
   s.add_development_dependency 'sass'
   s.add_development_dependency 'flamegraph'
+  s.add_development_dependency 'rubocop'
 
   s.require_paths = ["lib"]
 end

GitHub