Skip to content

Commit e04369a

Browse files
authored
Whitespace and Rubocop improvements (#35)
* Remove unnecessary whitespace * Default rubocop -a run
1 parent b0ab983 commit e04369a

11 files changed

+112
-128
lines changed

Rakefile

+37-36
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@
2929
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3030
# POSSIBILITY OF SUCH DAMAGE.
3131

32-
3332
# Load config if present
3433

3534
config_path_ = ::File.expand_path('rakefile_config.rb', ::File.dirname(__FILE__))
36-
load(config_path_) if ::File.exists?(config_path_)
37-
RAKEFILE_CONFIG = {} unless defined?(::RAKEFILE_CONFIG)
38-
35+
load(config_path_) if ::File.exist?(config_path_)
36+
RAKEFILE_CONFIG = {}.freeze unless defined?(::RAKEFILE_CONFIG)
3937

4038
# Gemspec
4139

@@ -44,7 +42,6 @@ gemspec_ = eval(::File.read(::Dir.glob('*.gemspec').first))
4442
release_gemspec_ = eval(::File.read(::Dir.glob('*.gemspec').first))
4543
release_gemspec_.version = gemspec_.version.to_s.sub(/\.nonrelease$/, '')
4644

47-
4845
# Platform info
4946

5047
dlext_ = ::RbConfig::CONFIG['DLEXT']
@@ -74,14 +71,12 @@ platform_suffix_ =
7471
else 'unknown'
7572
end
7673

77-
7874
# Directories
7975

8076
doc_directory_ = ::RAKEFILE_CONFIG[:doc_directory] || 'doc'
8177
pkg_directory_ = ::RAKEFILE_CONFIG[:pkg_directory] || 'pkg'
8278
tmp_directory_ = ::RAKEFILE_CONFIG[:tmp_directory] || 'tmp'
8379

84-
8580
# Build tasks
8681

8782
internal_ext_info_ = gemspec_.extensions.map do |extconf_path_|
@@ -95,7 +90,7 @@ internal_ext_info_ = gemspec_.extensions.map do |extconf_path_|
9590
obj_glob: "#{source_dir_}/*.{o,dSYM}",
9691
suffix_makefile_path: "#{source_dir_}/Makefile_#{platform_suffix_}",
9792
built_lib_path: "#{source_dir_}/#{name_}.#{dlext_}",
98-
staged_lib_path: "#{source_dir_}/#{name_}_#{platform_suffix_}.#{dlext_}",
93+
staged_lib_path: "#{source_dir_}/#{name_}_#{platform_suffix_}.#{dlext_}"
9994
}
10095
end
10196
internal_ext_info_ = [] if platform_ == :jruby
@@ -118,51 +113,60 @@ internal_ext_info_.each do |info_|
118113
end
119114
end
120115

121-
task build_ext: internal_ext_info_.map{ |info_| info_[:staged_lib_path] } do
116+
task build_ext: internal_ext_info_.map { |info_| info_[:staged_lib_path] } do
122117
internal_ext_info_.each do |info_|
123118
target_prefix_ = target_name_ = nil
124119
::Dir.chdir(info_[:source_dir]) do
125120
ruby 'extconf.rb'
126121
::File.open('Makefile') do |file_|
127122
file_.each do |line_|
128123
if line_ =~ /^target_prefix\s*=\s*(\S+)\s/
129-
target_prefix_ = $1
124+
target_prefix_ = Regexp.last_match(1)
130125
elsif line_ =~ /^TARGET\s*=\s*(\S+)\s/
131-
target_name_ = $1
126+
target_name_ = Regexp.last_match(1)
132127
end
133128
end
134129
end
135130
rm 'Makefile'
136131
end
137-
raise "Could not find target_prefix in makefile for #{info_[:name]}" unless target_prefix_
138-
raise "Could not find TARGET in makefile for #{info_[:name]}" unless target_name_
132+
unless target_prefix_
133+
raise "Could not find target_prefix in makefile for #{info_[:name]}"
134+
end
135+
unless target_name_
136+
raise "Could not find TARGET in makefile for #{info_[:name]}"
137+
end
138+
139139
cp info_[:staged_lib_path], "lib#{target_prefix_}/#{target_name_}.#{dlext_}"
140140
end
141141
end
142142

143-
144143
# Clean task
145144

146145
clean_files_ = [doc_directory_, pkg_directory_, tmp_directory_] +
147-
::Dir.glob('ext/**/Makefile*') +
148-
::Dir.glob('ext/**/*.{o,class,log,dSYM}') +
149-
::Dir.glob("**/*.{bundle,so,dll,rbc,jar}") +
150-
::Dir.glob('**/.rbx') +
151-
(::RAKEFILE_CONFIG[:extra_clean_files] || [])
146+
::Dir.glob('ext/**/Makefile*') +
147+
::Dir.glob('ext/**/*.{o,class,log,dSYM}') +
148+
::Dir.glob('**/*.{bundle,so,dll,rbc,jar}') +
149+
::Dir.glob('**/.rbx') +
150+
(::RAKEFILE_CONFIG[:extra_clean_files] || [])
152151
task :clean do
153-
clean_files_.each{ |path_| rm_rf path_ }
152+
clean_files_.each { |path_| rm_rf path_ }
154153
end
155154

156-
157155
# RDoc tasks
158156

159157
task build_rdoc: "#{doc_directory_}/index.html"
160-
all_rdoc_files_ = ::Dir.glob("lib/**/*.rb") + gemspec_.extra_rdoc_files
158+
all_rdoc_files_ = ::Dir.glob('lib/**/*.rb') + gemspec_.extra_rdoc_files
161159
main_rdoc_file_ = ::RAKEFILE_CONFIG[:main_rdoc_file]
162-
main_rdoc_file_ = 'README.rdoc' if !main_rdoc_file_ && ::File.readable?('README.rdoc')
163-
main_rdoc_file_ = ::Dir.glob("*.rdoc").first unless main_rdoc_file_
160+
if !main_rdoc_file_ && ::File.readable?('README.rdoc')
161+
main_rdoc_file_ = 'README.rdoc'
162+
end
163+
main_rdoc_file_ ||= ::Dir.glob('*.rdoc').first
164164
file "#{doc_directory_}/index.html" => all_rdoc_files_ do
165-
rm_r doc_directory_ rescue nil
165+
begin
166+
rm_r doc_directory_
167+
rescue StandardError
168+
nil
169+
end
166170
args_ = []
167171
args_ << '-o' << doc_directory_
168172
args_ << '--main' << main_rdoc_file_ if main_rdoc_file_
@@ -174,7 +178,6 @@ file "#{doc_directory_}/index.html" => all_rdoc_files_ do
174178
::RDoc::RDoc.new.document(args_ + all_rdoc_files_)
175179
end
176180

177-
178181
# Gem release tasks
179182

180183
task :build_other
@@ -197,23 +200,21 @@ task release_gem: :build_release do
197200
end
198201
end
199202

200-
201203
# Unit test task
202204

203-
task test: [:build_ext, :build_other] do
204-
$:.unshift(::File.expand_path('lib', ::File.dirname(__FILE__)))
205-
if ::ENV['TESTCASE']
206-
test_files_ = ::Dir.glob("test/#{::ENV['TESTCASE']}.rb")
207-
else
208-
test_files_ = ::Dir.glob("test/**/tc_*.rb")
209-
end
205+
task test: %i[build_ext build_other] do
206+
$LOAD_PATH.unshift(::File.expand_path('lib', ::File.dirname(__FILE__)))
207+
test_files_ = if ::ENV['TESTCASE']
208+
::Dir.glob("test/#{::ENV['TESTCASE']}.rb")
209+
else
210+
::Dir.glob('test/**/tc_*.rb')
211+
end
210212
test_files_.each do |path_|
211213
load path_
212214
puts "Loaded testcase #{path_}"
213215
end
214216
end
215217

216-
217218
# Default task
218219

219-
task default: [:clean, :test]
220+
task default: %i[clean test]

activerecord-mysql2spatial-adapter.gemspec

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@
3232
::Gem::Specification.new do |s_|
3333
s_.name = 'activerecord-mysql2spatial-adapter'
3434
s_.summary = 'An ActiveRecord adapter for MySQL Spatial Extensions, based on RGeo and the mysql2 gem.'
35-
s_.description = "This is an ActiveRecord connection adapter for MySQL Spatial Extensions. It is based on the stock MySQL2 adapter, but provides built-in support for spatial columns. It uses the RGeo library to represent spatial data in Ruby."
36-
s_.version = "#{::File.read('Version').strip}"
35+
s_.description = 'This is an ActiveRecord connection adapter for MySQL Spatial Extensions. It is based on the stock MySQL2 adapter, but provides built-in support for spatial columns. It uses the RGeo library to represent spatial data in Ruby.'
36+
s_.version = ::File.read('Version').strip.to_s
3737
s_.author = 'Daniel Azuma'
3838
s_.email = '[email protected]'
3939
s_.required_ruby_version = '>= 1.9.3'
4040
s_.files =
41-
::Dir.glob("lib/**/*.rb") +
42-
::Dir.glob("test/**/*.rb") +
43-
::Dir.glob("*.rdoc") +
41+
::Dir.glob('lib/**/*.rb') +
42+
::Dir.glob('test/**/*.rb') +
43+
::Dir.glob('*.rdoc') +
4444
['Version']
45-
s_.extra_rdoc_files = ::Dir.glob("*.rdoc")
45+
s_.extra_rdoc_files = ::Dir.glob('*.rdoc')
4646
s_.platform = ::Gem::Platform::RUBY
4747
s_.add_dependency('activerecord', '>= 4.0', '< 4.2')
4848
s_.add_dependency('rgeo-activerecord', '~> 1.3')

lib/active_record/connection_adapters/mysql2spatial_adapter.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,17 @@
2929
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3030
# POSSIBILITY OF SUCH DAMAGE.
3131

32-
3332
require 'rgeo/active_record'
3433
require 'active_record/connection_adapters/mysql2_adapter'
3534

3635
# The activerecord-mysql2spatial-adapter gem installs the *mysql2spatial*
3736
# connection adapter into ActiveRecord.
3837

3938
module ActiveRecord
40-
4139
# ActiveRecord looks for the mysql2spatial_connection factory method in
4240
# this class.
4341

4442
class Base
45-
4643
# Create a mysql2spatial connection adapter.
4744

4845
def self.mysql2spatial_connection(config_)
@@ -54,18 +51,14 @@ def self.mysql2spatial_connection(config_)
5451
options_ = [config_[:host], config_[:username], config_[:password], config_[:database], config_[:port], config_[:socket], 0]
5552
::ActiveRecord::ConnectionAdapters::Mysql2SpatialAdapter::MainAdapter.new(client_, logger, options_, config_)
5653
end
57-
5854
end
5955

6056
# All ActiveRecord adapters go in this namespace.
6157
module ConnectionAdapters
62-
6358
# The Mysql2Spatial adapter
6459
module Mysql2SpatialAdapter
65-
6660
# The name returned by the adapter_name method of this adapter.
67-
ADAPTER_NAME = 'Mysql2Spatial'.freeze
68-
61+
ADAPTER_NAME = 'Mysql2Spatial'
6962
end
7063
end
7164
end

lib/active_record/connection_adapters/mysql2spatial_adapter/arel_tosql.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,35 @@
2929
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3030
# POSSIBILITY OF SUCH DAMAGE.
3131

32-
3332
# :stopdoc:
3433

3534
module Arel
3635
module Visitors
3736
class MySQL2Spatial < MySQL
38-
3937
if ::Arel::Visitors.const_defined?(:BindVisitor)
4038
include ::Arel::Visitors::BindVisitor
4139
end
4240

4341
FUNC_MAP = {
4442
'st_wkttosql' => 'GeomFromText',
4543
'st_wkbtosql' => 'GeomFromWKB',
46-
'st_length' => 'GLength',
47-
}
44+
'st_length' => 'GLength'
45+
}.freeze
4846

4947
include ::RGeo::ActiveRecord::SpatialToSql
5048

5149
def st_func(standard_name_)
5250
if (name_ = FUNC_MAP[standard_name_.downcase])
5351
name_
5452
elsif standard_name_ =~ /^st_(\w+)$/i
55-
$1
53+
Regexp.last_match(1)
5654
else
5755
standard_name_
5856
end
5957
end
60-
6158
end
6259

6360
VISITORS['mysql2spatial'] = ::Arel::Visitors::MySQL2Spatial
64-
6561
end
6662
end
6763

lib/active_record/connection_adapters/mysql2spatial_adapter/column_methods.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ module ConnectionAdapters
33
module Mysql2SpatialAdapter
44
module ColumnMethods
55
def spatial(name, options = {})
6-
raise "You must set a type. For example: 't.spatial limit: { type: 'point' }'" unless options[:limit][:type]
6+
unless options[:limit][:type]
7+
raise "You must set a type. For example: 't.spatial limit: { type: 'point' }'"
8+
end
9+
710
column(name, options[:limit][:type], options)
811
end
912

@@ -40,7 +43,7 @@ def point(name, options = {})
4043
end
4144
end
4245

43-
ConnectionAdapters::TableDefinition.send(:include, ColumnMethods)
46+
ConnectionAdapters::TableDefinition.include ColumnMethods
4447
end
4548
end
4649
end

lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb

+17-18
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@
2929
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3030
# POSSIBILITY OF SUCH DAMAGE.
3131

32-
3332
# :stopdoc:
3433

3534
module ActiveRecord
3635
module ConnectionAdapters
3736
module Mysql2SpatialAdapter
3837
class MainAdapter < ConnectionAdapters::Mysql2Adapter
39-
4038
NATIVE_DATABASE_TYPES = Mysql2Adapter::NATIVE_DATABASE_TYPES.merge(spatial: { name: 'geometry' })
4139

4240
def initialize(*args_)
@@ -63,15 +61,15 @@ def native_database_types
6361
NATIVE_DATABASE_TYPES
6462
end
6563

66-
def quote(value_, column_=nil)
64+
def quote(value_, column_ = nil)
6765
if ::RGeo::Feature::Geometry.check_type(value_)
6866
"GeomFromWKB(0x#{::RGeo::WKRep::WKBGenerator.new(hex_format: true).generate(value_)},#{value_.srid})"
6967
else
7068
super
7169
end
7270
end
7371

74-
def type_to_sql(type_, limit_=nil, precision_=nil, scale_=nil)
72+
def type_to_sql(type_, limit_ = nil, precision_ = nil, scale_ = nil)
7573
if (info_ = spatial_column_constructor(type_.to_sym))
7674
type_ = limit_[:type] || type_ if limit_.is_a?(::Hash)
7775
type_ = 'geometry' if type_.to_s == 'spatial'
@@ -80,51 +78,52 @@ def type_to_sql(type_, limit_=nil, precision_=nil, scale_=nil)
8078
super(type_, limit_, precision_, scale_)
8179
end
8280

83-
def add_index(table_name_, column_name_, options_={})
81+
def add_index(table_name_, column_name_, options_ = {})
8482
if options_[:spatial]
8583
index_name_ = index_name(table_name_, column: Array(column_name_))
86-
if ::Hash === options_
87-
index_name_ = options_[:name] || index_name_
88-
end
89-
execute "CREATE SPATIAL INDEX #{index_name_} ON #{table_name_} (#{Array(column_name_).join(", ")})"
84+
index_name_ = options_[:name] || index_name_ if ::Hash === options_
85+
execute "CREATE SPATIAL INDEX #{index_name_} ON #{table_name_} (#{Array(column_name_).join(', ')})"
9086
else
9187
super
9288
end
9389
end
9490

95-
def columns(table_name_, name_=nil)
91+
def columns(table_name_, _name_ = nil)
9692
result_ = execute("SHOW FIELDS FROM #{quote_table_name(table_name_)}", :skip_logging)
9793
columns_ = []
9894
result_.each(symbolize_keys: true, as: :hash) do |field_|
9995
columns_ << SpatialColumn.new(@rgeo_factory_settings, table_name_.to_s,
100-
field_[:Field], field_[:Default], field_[:Type], field_[:Null] == "YES")
96+
field_[:Field], field_[:Default], field_[:Type], field_[:Null] == 'YES')
10197
end
10298
columns_
10399
end
104100

105101
# Returns an array of indexes for the given table.
106-
def indexes(table_name_, name_=nil)
102+
def indexes(table_name_, name_ = nil)
107103
indexes_ = []
108104
current_index_ = nil
109105
result_ = execute("SHOW KEYS FROM #{quote_table_name(table_name_)}", name_)
110106
result_.each(symbolize_keys: true, as: :hash) do |row_|
111107
if current_index_ != row_[:Key_name]
112108
next if row_[:Key_name] == 'PRIMARY' # skip the primary key
109+
113110
current_index_ = row_[:Key_name]
114111
mysql_index_type = row_[:Index_type].downcase.to_sym
115-
index_type = INDEX_TYPES.include?(mysql_index_type) ? mysql_index_type : nil
112+
index_type = INDEX_TYPES.include?(mysql_index_type) ? mysql_index_type : nil
116113
index_using = INDEX_USINGS.include?(mysql_index_type) ? mysql_index_type : nil
117114
options = [row_[:Table], row_[:Key_name], row_[:Non_unique].to_i == 0, [], [], nil, nil, index_type, index_using]
118115
indexes_ << if mysql_index_type == :spatial
119-
options.push(true)
120-
::RGeo::ActiveRecord::SpatialIndexDefinition.new(*options)
121-
else
122-
IndexDefinition.new(*options)
116+
options.push(true)
117+
::RGeo::ActiveRecord::SpatialIndexDefinition.new(*options)
118+
else
119+
IndexDefinition.new(*options)
123120
end
124121
end
125122
last_index_ = indexes_.last
126123
last_index_.columns << row_[:Column_name]
127-
last_index_.lengths << row_[:Sub_part] unless mysql_index_type == :spatial
124+
unless mysql_index_type == :spatial
125+
last_index_.lengths << row_[:Sub_part]
126+
end
128127
end
129128
indexes_
130129
end

0 commit comments

Comments
 (0)