Skip to content

Commit d62a168

Browse files
committed
cleanup code based on several rubocop rules
1 parent c2a1238 commit d62a168

39 files changed

Lines changed: 649 additions & 580 deletions

.simplecov

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
SimpleCov.configure do
24
enable_coverage :branch
35
add_filter '/spec/'

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in schema_dev.gemspec

Rakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require "bundler/gem_tasks"
1+
# frozen_string_literal: true
2+
3+
require 'bundler/gem_tasks'
24

35
require 'rspec/core/rake_task'
46
RSpec::Core::RakeTask.new(:spec)

bin/schema_dev

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require 'active_support/core_ext/hash'
45
require 'thor'
6+
require 'English'
57
require_relative '../lib/schema_dev/config'
68
require_relative '../lib/schema_dev/gem'
79
require_relative '../lib/schema_dev/runner'
810
require_relative '../lib/schema_dev/version'
911

1012
def runner
11-
$config ||= SchemaDev::Config.load
12-
$runner ||= SchemaDev::Runner.new($config)
13+
config = SchemaDev::Config.load rescue nil
14+
@runner ||= SchemaDev::Runner.new(config)
1315
end
1416

15-
$cmd = File.basename $0
17+
def cmd
18+
@cmd ||= File.basename $PROGRAM_NAME
19+
end
1620

1721
class CLI < Thor
18-
1922
def self.matrix_options
20-
method_option :dry_run, aliases: "-n", type: :boolean, desc: "Show what the commands would be without running them"
21-
method_option :quick, type: :boolean, desc: "Only execute on the 'quick' choice: #{$config ? $config.quick.inspect : "[from schema_dev.yml]"}"
22-
method_option :ruby, type: :string, desc: "Only execute for the specified version of ruby"
23-
method_option :activerecord, aliases: "--ar", type: :string, desc: "Only execute for the specified version of activerecord"
24-
method_option :db, type: :string, desc: "Only execute for the specified database"
23+
method_option :dry_run, aliases: '-n', type: :boolean, desc: 'Show what the commands would be without running them'
24+
method_option :quick, type: :boolean, desc: "Only execute on the 'quick' choice: #{runner.config ? runner.config.quick.inspect : '[from schema_dev.yml]'}"
25+
method_option :ruby, type: :string, desc: 'Only execute for the specified version of ruby'
26+
method_option :activerecord, aliases: '--ar', type: :string, desc: 'Only execute for the specified version of activerecord'
27+
method_option :db, type: :string, desc: 'Only execute for the specified database'
2528
end
2629

27-
desc "freshen", "update files that depend on schema_dev.yml: .github/worksflows/prs.yml, gemfiles/, README.md"
30+
desc 'freshen', 'update files that depend on schema_dev.yml: .github/worksflows/prs.yml, gemfiles/, README.md'
2831
def freshen
29-
runner.freshen
32+
runner.freshen
3033
end
3134

32-
desc "matrix", "run a command over the matrix"
35+
desc 'matrix', 'run a command over the matrix'
3336
matrix_options
3437
def matrix(*args)
3538
runner.run(args, **options.to_h.symbolize_keys)
3639
end
3740

38-
desc "bundle", "shorthand for '#{$cmd} matrix bundle ...'"
41+
desc 'bundle', "shorthand for '#{cmd} matrix bundle ...'"
3942
matrix_options
4043
def bundle(*args)
4144
runner.run('bundle', args, **options.to_h.symbolize_keys)
4245
end
4346

44-
desc "rake", "shorthand for '#{$cmd} matrix bundle exec rake ...'"
47+
desc 'rake', "shorthand for '#{cmd} matrix bundle exec rake ...'"
4548
matrix_options
4649
def rake(*args)
4750
runner.run('bundle', 'exec', 'rake', args, **options.to_h.symbolize_keys)
4851
end
4952

50-
desc "rspec", "shorthand for '#{$cmd} bundle exec rspec ...'"
53+
desc 'rspec', "shorthand for '#{cmd} bundle exec rspec ...'"
5154
matrix_options
5255
def rspec(*args)
5356
runner.run('bundle', 'exec', 'rspec', args, **options.to_h.symbolize_keys)
5457
end
5558

56-
desc "gem", "create a new SchemaPlus gem"
59+
desc 'gem', 'create a new SchemaPlus gem'
5760
def gem(name)
58-
SchemaDev::Gem.build(name)
61+
SchemaDev::Gem.build(name)
5962
end
60-
6163
end
6264

63-
case
64-
when ARGV[0] == "--version" then puts "SchemaDev #{SchemaDev::VERSION}"
65-
else CLI.start(ARGV)
65+
case ARGV[0]
66+
when '--version'
67+
puts "SchemaDev #{SchemaDev::VERSION}"
68+
else
69+
CLI.start(ARGV)
6670
end

lib/schema_dev.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
require "schema_dev/version"
1+
# frozen_string_literal: true
2+
3+
require 'schema_dev/version'

lib/schema_dev/config.rb

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1+
# frozen_string_literal: true
2+
13
require 'active_support/core_ext/hash'
2-
require 'enumerator'
34
require 'pathname'
45
require 'yaml'
56
require 'rubygems/version'
67

78
module SchemaDev
8-
CONFIG_FILE = "schema_dev.yml"
9+
CONFIG_FILE = 'schema_dev.yml'
910

1011
class Config
11-
1212
attr_accessor :quick, :db, :dbversions, :ruby, :activerecord, :exclude
1313

14-
def self._reset ; @config = nil end # for use by rspec
14+
# for use by rspec
15+
def self._reset
16+
@load = nil
17+
end
1518

1619
def self.read
17-
new(**(YAML.load Pathname.new(CONFIG_FILE).read).symbolize_keys)
20+
new(**YAML.safe_load(Pathname.new(CONFIG_FILE).read, [Symbol]).symbolize_keys)
1821
end
1922

2023
def self.load
21-
@config ||= read
24+
@load ||= read
2225
end
2326

2427
def initialize(ruby:, activerecord:, db:, dbversions: nil, exclude: nil, notify: nil, quick: nil)
2528
@ruby = Array.wrap(ruby).map(&:to_s)
2629
@activerecord = Array.wrap(activerecord).map(&:to_s)
2730
@db = Array.wrap(db)
2831
@dbversions = (dbversions || {}).symbolize_keys
29-
@exclude = Array.wrap(exclude).map(&:symbolize_keys).map {|tuple| Tuple.new(**tuple.transform_values(&:to_s))}
32+
@exclude = Array.wrap(exclude).map(&:symbolize_keys).map { |tuple| Tuple.new(**tuple.transform_values(&:to_s)) }
3033
if @activerecord.include?('5.2')
3134
ruby3 = ::Gem::Version.new('3.0')
3235

@@ -35,18 +38,18 @@ def initialize(ruby:, activerecord:, db:, dbversions: nil, exclude: nil, notify:
3538
end
3639
end
3740
unless notify.nil?
38-
warn "Notify is no longer supported"
41+
warn 'Notify is no longer supported'
3942
end
40-
@quick = Array.wrap(quick || {ruby: @ruby.last, activerecord: @activerecord.last, db: @db.last})
43+
@quick = Array.wrap(quick || { ruby: @ruby.last, activerecord: @activerecord.last, db: @db.last })
4144
end
4245

4346
def dbms
44-
@dbms ||= [:postgresql, :mysql].select{|dbm| @db.grep(/^#{dbm}/).any?}
47+
@dbms ||= %i[postgresql mysql].select { |dbm| @db.grep(/^#{dbm}/).any? }
4548
end
4649

4750
DB_VERSION_DEFAULTS = {
4851
postgresql: ['9.6']
49-
}
52+
}.freeze
5053

5154
def db_versions_for(db)
5255
@dbversions.fetch(db.to_sym, DB_VERSION_DEFAULTS.fetch(db.to_sym, [])).map(&:to_s)
@@ -57,9 +60,9 @@ def matrix(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil, w
5760
use_activerecord = @activerecord
5861
use_db = @db
5962
if quick
60-
use_ruby = @quick.map{|q| q[:ruby]}
61-
use_activerecord = @quick.map{|q| q[:activerecord]}
62-
use_db = @quick.map{|q| q[:db]}
63+
use_ruby = @quick.map { |q| q[:ruby] }
64+
use_activerecord = @quick.map { |q| q[:activerecord] }
65+
use_db = @quick.map { |q| q[:db] }
6366
end
6467
use_ruby = Array.wrap(ruby) if ruby
6568
use_activerecord = Array.wrap(activerecord) if activerecord
@@ -70,13 +73,13 @@ def matrix(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil, w
7073
use_db = [nil] unless use_db.any?
7174

7275
m = use_ruby.product(use_activerecord, use_db)
73-
m = m.flat_map { |_ruby, _activerecord, _db|
74-
if with_dbversion && !(dbversions = db_versions_for(_db)).empty?
75-
dbversions.map { |v| Tuple.new(ruby: _ruby, activerecord: _activerecord, db: _db, dbversion: v) }
76+
m = m.flat_map do |loop_ruby, loop_activerecord, loop_db|
77+
if with_dbversion && !(dbversions = db_versions_for(loop_db)).empty?
78+
dbversions.map { |v| Tuple.new(ruby: loop_ruby, activerecord: loop_activerecord, db: loop_db, dbversion: v) }
7679
else
77-
[Tuple.new(ruby: _ruby, activerecord: _activerecord, db: _db)]
80+
[Tuple.new(ruby: loop_ruby, activerecord: loop_activerecord, db: loop_db)]
7881
end
79-
}.compact
82+
end.compact
8083
m = m.reject { |r| r.match_any?(@exclude) } unless excluded == :none
8184
m = m.map(&:to_hash)
8285

@@ -89,15 +92,16 @@ def matrix(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil, w
8992

9093
Tuple = Struct.new(:ruby, :activerecord, :db, :dbversion, keyword_init: true) do
9194
def match?(other)
92-
return false if self.ruby and other.ruby and self.ruby != other.ruby
93-
return false if self.activerecord and other.activerecord and self.activerecord != other.activerecord
94-
return false if self.db and other.db and self.db != other.db
95-
return false if self.dbversion and other.dbversion and self.dbversion != other.dbversion
95+
return false if ruby and other.ruby and ruby != other.ruby
96+
return false if activerecord and other.activerecord and activerecord != other.activerecord
97+
return false if db and other.db and db != other.db
98+
return false if dbversion and other.dbversion and dbversion != other.dbversion
99+
96100
true
97101
end
98102

99103
def match_any?(others)
100-
others.any?{|other| self.match? other}
104+
others.any? { |other| match? other }
101105
end
102106

103107
def to_hash

lib/schema_dev/executor.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
# frozen_string_literal: true
2+
13
require 'json'
24
require 'open3'
35

4-
require_relative "ruby_selector"
5-
require_relative "gemfile_selector"
6+
require_relative 'ruby_selector'
7+
require_relative 'gemfile_selector'
68

79
module SchemaDev
810
class Executor
9-
1011
attr_reader :ruby, :activerecord, :db, :error
1112

1213
def initialize(ruby:, activerecord:, db:)
@@ -15,20 +16,20 @@ def initialize(ruby:, activerecord:, db:)
1516
end
1617

1718
def run(cmd, dry_run: false)
18-
fullcommand = ["/usr/bin/env", @gemfile_selector, @ruby_selector, cmd].compact.join(' ')
19+
fullcommand = ['/usr/bin/env', @gemfile_selector, @ruby_selector, cmd].compact.join(' ')
1920
puts "* #{fullcommand}"
2021
return true if dry_run
2122

2223
@error = false
23-
Open3.popen2e(fullcommand) do |i, oe, t|
24-
oe.each {|line|
24+
Open3.popen2e(fullcommand) do |_i, oe, t|
25+
oe.each do |line|
2526
puts line
2627
@error ||= (line =~ /(^Failed examples)|(rake aborted)|(LoadError)/)
27-
}
28+
end
2829
@error ||= !t.value.success?
2930
end
3031

31-
return !@error
32+
!@error
3233
end
3334
end
3435
end

0 commit comments

Comments
 (0)