1+ # frozen_string_literal: true
2+
13require 'active_support/core_ext/hash'
2- require 'enumerator'
34require 'pathname'
45require 'yaml'
56require 'rubygems/version'
67
78module 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
0 commit comments