Skip to content

Commit 6d231ce

Browse files
committed
Merge pull request #596 from tamird/rubocop
Rubocop
2 parents d771ca4 + ac93121 commit 6d231ce

31 files changed

+370
-273
lines changed

.rubocop.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
inherit_from: .rubocop_todo.yml
2+
3+
AllCops:
4+
DisplayCopNames: true
5+
Exclude:
6+
- 'tmp/**/*'
7+
8+
Lint/EndAlignment:
9+
AlignWith: variable
10+
11+
Style/CaseIndentation:
12+
IndentWhenRelativeTo: end
13+
14+
Style/IndentHash:
15+
EnforcedStyle: consistent
16+
17+
Style/TrailingComma:
18+
EnforcedStyleForMultiline: consistent_comma
19+
20+
Style/TrivialAccessors:
21+
AllowPredicates: true
22+
23+
# TODO: remove when we end support for < 1.9.3
24+
25+
Style/HashSyntax:
26+
EnforcedStyle: hash_rockets
27+
28+
Style/Lambda:
29+
Enabled: false

.rubocop_todo.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2015-09-06 13:16:09 -0400 using RuboCop version 0.34.0.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 2
10+
Metrics/AbcSize:
11+
Max: 68
12+
13+
# Offense count: 1
14+
Metrics/BlockNesting:
15+
Max: 5
16+
17+
# Offense count: 2
18+
Metrics/CyclomaticComplexity:
19+
Max: 23
20+
21+
# Offense count: 290
22+
# Configuration parameters: AllowURI, URISchemes.
23+
Metrics/LineLength:
24+
Max: 232
25+
26+
# Offense count: 5
27+
# Configuration parameters: CountComments.
28+
Metrics/MethodLength:
29+
Max: 43
30+
31+
# Offense count: 1
32+
Metrics/PerceivedComplexity:
33+
Max: 22
34+
35+
# Offense count: 40
36+
# Cop supports --auto-correct.
37+
# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods.
38+
Style/BlockDelimiters:
39+
Enabled: false
40+
41+
# Offense count: 12
42+
Style/Documentation:
43+
Exclude:
44+
- 'benchmark/active_record.rb'
45+
- 'benchmark/allocations.rb'
46+
- 'benchmark/query_with_mysql_casting.rb'
47+
- 'lib/mysql2.rb'
48+
- 'lib/mysql2/client.rb'
49+
- 'lib/mysql2/em.rb'
50+
- 'lib/mysql2/error.rb'
51+
- 'lib/mysql2/field.rb'
52+
- 'lib/mysql2/result.rb'
53+
- 'lib/mysql2/statement.rb'
54+
- 'lib/mysql2/version.rb'
55+
56+
# Offense count: 9
57+
# Configuration parameters: AllowedVariables.
58+
Style/GlobalVars:
59+
Exclude:
60+
- 'ext/mysql2/extconf.rb'
61+
62+
# Offense count: 14
63+
# Cop supports --auto-correct.
64+
Style/NumericLiterals:
65+
MinDigits: 20
66+
67+
# Offense count: 680
68+
# Cop supports --auto-correct.
69+
# Configuration parameters: EnforcedStyle, SupportedStyles.
70+
Style/StringLiterals:
71+
Enabled: false

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ gem 'rake-compiler', '~> 0.9.5'
88
group :test do
99
gem 'eventmachine' unless RUBY_PLATFORM =~ /mswin|mingw/
1010
gem 'rspec', '~> 3.2'
11+
gem 'rubocop', '~> 0.34.0' unless RUBY_VERSION =~ /1.8/
1112
end
1213

1314
group :benchmarks do

Rakefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ load 'tasks/compile.rake'
88
load 'tasks/generate.rake'
99
load 'tasks/benchmarks.rake'
1010

11-
task :default => :spec
11+
# TODO: remove when we end support for < 1.9.3
12+
if RUBY_VERSION =~ /1.8/
13+
task :default => :spec
14+
else
15+
require 'rubocop/rake_task'
16+
RuboCop::RakeTask.new
17+
18+
task :default => [:spec, :rubocop]
19+
end

benchmark/allocations.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
require 'rubygems'
55
require 'active_record'
66

7-
raise Mysql2::Error.new("GC allocation benchmarks only supported on Ruby 1.9!") unless RUBY_VERSION > '1.9'
8-
97
ActiveRecord::Base.default_timezone = :local
108
ActiveRecord::Base.time_zone_aware_attributes = true
119

benchmark/query_with_mysql_casting.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ def mysql_cast(type, value)
3030
when Mysql::Field::TYPE_TIME, Mysql::Field::TYPE_DATETIME, Mysql::Field::TYPE_TIMESTAMP
3131
Time.parse(value)
3232
when Mysql::Field::TYPE_BLOB, Mysql::Field::TYPE_BIT, Mysql::Field::TYPE_STRING,
33-
Mysql::Field::TYPE_VAR_STRING, Mysql::Field::TYPE_CHAR, Mysql::Field::TYPE_SET
34-
Mysql::Field::TYPE_ENUM
33+
Mysql::Field::TYPE_VAR_STRING, Mysql::Field::TYPE_CHAR, Mysql::Field::TYPE_SET,
34+
Mysql::Field::TYPE_ENUM
3535
value
3636
else
3737
value
3838
end
3939
end
4040

41+
debug = ENV['DEBUG']
42+
4143
Benchmark.ips do |x|
4244
mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root")
4345
mysql2.query "USE #{database}"
4446
x.report "Mysql2" do
4547
mysql2_result = mysql2.query sql, :symbolize_keys => true
46-
mysql2_result.each do |res|
47-
# puts res.inspect
48-
end
48+
mysql2_result.each { |res| puts res.inspect if debug }
4949
end
5050

5151
mysql = Mysql.new("localhost", "root")
@@ -55,19 +55,17 @@ def mysql_cast(type, value)
5555
fields = mysql_result.fetch_fields
5656
mysql_result.each do |row|
5757
row_hash = row.each_with_index.each_with_object({}) do |(f, j), hash|
58-
hash[fields[j].name.to_sym] = mysql_cast(fields[j].type, row[j])
58+
hash[fields[j].name.to_sym] = mysql_cast(fields[j].type, f)
5959
end
60-
# puts row_hash.inspect
60+
puts row_hash.inspect if debug
6161
end
6262
end
6363

6464
do_mysql = DataObjects::Connection.new("mysql://localhost/#{database}")
6565
command = do_mysql.create_command sql
6666
x.report "do_mysql" do
6767
do_result = command.execute_reader
68-
do_result.each do |res|
69-
# puts res.inspect
70-
end
68+
do_result.each { |res| puts res.inspect if debug }
7169
end
7270

7371
x.compare!

benchmark/query_without_mysql_casting.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,33 @@
1010
database = 'test'
1111
sql = "SELECT * FROM mysql2_test LIMIT 100"
1212

13+
debug = ENV['DEBUG']
14+
1315
Benchmark.ips do |x|
1416
mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root")
1517
mysql2.query "USE #{database}"
1618
x.report "Mysql2 (cast: true)" do
1719
mysql2_result = mysql2.query sql, :symbolize_keys => true, :cast => true
18-
mysql2_result.each do |res|
19-
# puts res.inspect
20-
end
20+
mysql2_result.each { |res| puts res.inspect if debug }
2121
end
2222

2323
x.report "Mysql2 (cast: false)" do
2424
mysql2_result = mysql2.query sql, :symbolize_keys => true, :cast => false
25-
mysql2_result.each do |res|
26-
# puts res.inspect
27-
end
25+
mysql2_result.each { |res| puts res.inspect if debug }
2826
end
2927

3028
mysql = Mysql.new("localhost", "root")
3129
mysql.query "USE #{database}"
3230
x.report "Mysql" do
3331
mysql_result = mysql.query sql
34-
mysql_result.each_hash do |res|
35-
# puts res.inspect
36-
end
32+
mysql_result.each_hash { |res| puts res.inspect if debug }
3733
end
3834

3935
do_mysql = DataObjects::Connection.new("mysql://localhost/#{database}")
4036
command = DataObjects::Mysql::Command.new do_mysql, sql
4137
x.report "do_mysql" do
4238
do_result = command.execute_reader
43-
do_result.each do |res|
44-
# puts res.inspect
45-
end
39+
do_result.each { |res| puts res.inspect if debug }
4640
end
4741

4842
x.compare!

benchmark/setup_db.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ def insert_record(args)
8686
:medium_int_test => rand(8388607),
8787
:int_test => rand(2147483647),
8888
:big_int_test => rand(9223372036854775807),
89-
:float_test => rand(32767)/1.87,
89+
:float_test => rand(32767) / 1.87,
9090
:float_zero_test => 0.0,
91-
:double_test => rand(8388607)/1.87,
92-
:decimal_test => rand(8388607)/1.87,
91+
:double_test => rand(8388607) / 1.87,
92+
:decimal_test => rand(8388607) / 1.87,
9393
:decimal_zero_test => 0,
9494
:date_test => '2010-4-4',
9595
:date_time_test => '2010-4-4 11:44:00',
@@ -108,8 +108,8 @@ def insert_record(args)
108108
:medium_text_test => twenty5_paragraphs,
109109
:long_blob_test => twenty5_paragraphs,
110110
:long_text_test => twenty5_paragraphs,
111-
:enum_test => ['val1', 'val2'][rand(2)],
112-
:set_test => ['val1', 'val2', 'val1,val2'][rand(3)]
111+
:enum_test => %w(val1 val2).sample,
112+
:set_test => %w(val1 val2 val1,val2).sample,
113113
)
114114
if n % 100 == 0
115115
$stdout.putc '.'

examples/eventmachine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
defer2.callback do |result|
1919
puts "Result: #{result.to_a.inspect}"
2020
end
21-
end
21+
end

ext/mysql2/client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ static VALUE rb_mysql_client_abandon_results(VALUE self) {
648648
* client.query(sql, options = {})
649649
*
650650
* Query the database with +sql+, with optional +options+. For the possible
651-
* options, see @@default_query_options on the Mysql2::Client class.
651+
* options, see default_query_options on the Mysql2::Client class.
652652
*/
653653
static VALUE rb_query(VALUE self, VALUE sql, VALUE current) {
654654
#ifndef _WIN32

0 commit comments

Comments
 (0)