Class: SchemaDev::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_dev/config.rb

Defined Under Namespace

Classes: Tuple

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

once we no longer support ruby 1.9.3, can switch to native keyword args



27
28
29
30
31
32
33
34
35
# File 'lib/schema_dev/config.rb', line 27

def initialize(opts={}) # once we no longer support ruby 1.9.3, can switch to native keyword args
  opts = opts.keyword_args(ruby: :required, activerecord: :required, db: :required, exclude: nil, notify: nil, quick: nil)
  @ruby = Array.wrap(opts.ruby)
  @activerecord = Array.wrap(opts.activerecord)
  @db = Array.wrap(opts.db)
  @exclude = Array.wrap(opts.exclude).map(&:symbolize_keys).map {|tuple| Tuple.new(tuple)}
  @notify = Array.wrap(opts.notify)
  @quick = Array.wrap(opts.quick || {ruby: @ruby.last, activerecord: @activerecord.last, db: @db.last})
end

Instance Attribute Details

#activerecordObject

Returns the value of attribute activerecord.



15
16
17
# File 'lib/schema_dev/config.rb', line 15

def activerecord
  @activerecord
end

#dbObject

Returns the value of attribute db.



15
16
17
# File 'lib/schema_dev/config.rb', line 15

def db
  @db
end

#excludeObject

Returns the value of attribute exclude.



15
16
17
# File 'lib/schema_dev/config.rb', line 15

def exclude
  @exclude
end

#notifyObject

Returns the value of attribute notify.



15
16
17
# File 'lib/schema_dev/config.rb', line 15

def notify
  @notify
end

#quickObject

Returns the value of attribute quick.



15
16
17
# File 'lib/schema_dev/config.rb', line 15

def quick
  @quick
end

#rubyObject

Returns the value of attribute ruby.



15
16
17
# File 'lib/schema_dev/config.rb', line 15

def ruby
  @ruby
end

Class Method Details

._resetObject

for use by rspec



17
# File 'lib/schema_dev/config.rb', line 17

def self._reset ; @@config = nil end

.loadObject



23
24
25
# File 'lib/schema_dev/config.rb', line 23

def self.load
  @@config ||= read
end

.readObject



19
20
21
# File 'lib/schema_dev/config.rb', line 19

def self.read
  new((YAML.load Pathname.new(CONFIG_FILE).read).symbolize_keys)
end

Instance Method Details

#dbmsObject



37
38
39
# File 'lib/schema_dev/config.rb', line 37

def dbms
  @dbms ||= [:postgresql, :mysql].select{|dbm| @db.grep(/^#{dbm}/).any?}
end

#matrix(opts = {}) ⇒ Object

once we no longer support ruby 1.9.3, can switch to native keyword args



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/schema_dev/config.rb', line 41

def matrix(opts={}) # once we no longer support ruby 1.9.3, can switch to native keyword args
  opts = opts.keyword_args(quick: false, ruby: nil, activerecord: nil, db: nil, excluded: nil)
  use_ruby = @ruby
  use_activerecord = @activerecord
  use_db = @db
  if opts.quick
    use_ruby = @quick.map{|q| q[:ruby]}
    use_activerecord = @quick.map{|q| q[:activerecord]}
    use_db = @quick.map{|q| q[:db]}
  end
  use_ruby = Array.wrap(opts.ruby) if opts.ruby
  use_activerecord = Array.wrap(opts.activerecord) if opts.activerecord
  use_db = Array.wrap(opts.db) if opts.db

  use_ruby = [nil] unless use_ruby.any?
  use_activerecord = [nil] unless use_activerecord.any?
  use_db = [nil] unless use_db.any?

  m = use_ruby.product(use_activerecord, use_db)
  m = m.map { |_ruby, _activerecord, _db| Tuple.new(ruby: _ruby, activerecord: _activerecord, db: _db) }.compact
  m = m.reject(&it.match_any?(@exclude)) unless opts.excluded == :none
  m = m.map(&:to_hash)

  if opts.excluded == :only
    return matrix(opts.merge(excluded: :none)) - m
  else
    return m
  end
end