Class: DatabaseConsistency::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/database_consistency/configuration.rb

Overview

The class to access configuration options

Constant Summary collapse

DEFAULT_PATH =
'.database_consistency.yml'

Instance Method Summary collapse

Constructor Details

#initialize(file_paths = DEFAULT_PATH) ⇒ Configuration

Returns a new instance of Configuration.



10
11
12
13
14
15
16
17
18
19
# File 'lib/database_consistency/configuration.rb', line 10

def initialize(file_paths = DEFAULT_PATH)
  @configuration = existing_configurations(file_paths).then do |existing_paths|
    if existing_paths.any?
      puts "Loaded configurations: #{existing_paths.join(', ')}"
    else
      puts 'No configurations were provided'
    end
    extract_configurations(existing_paths)
  end
end

Instance Method Details

#colored?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/database_consistency/configuration.rb', line 25

def colored?
  if ENV.key?('COLOR')
    ENV['COLOR'].match?(/1|true|yes/)
  else
    settings && settings['color']
  end
end

#database_enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/database_consistency/configuration.rb', line 55

def database_enabled?(name)
  value = configuration.dig('DatabaseConsistencyDatabases', name, 'enabled')

  value.nil? ? true : value
end

#debug?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/database_consistency/configuration.rb', line 21

def debug?
  log_level.to_s.match?(/DEBUG/i)
end

#enabled?(*path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/database_consistency/configuration.rb', line 34

def enabled?(*path)
  current = configuration

  value = global_enabling

  path.each do |key|
    current = find(key.to_s, current)
    return value unless current.is_a?(Hash)

    next if current['enabled'].nil?

    value = current['enabled']
  end

  value
end

#model_enabled?(model) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/database_consistency/configuration.rb', line 51

def model_enabled?(model)
  database_enabled?(Helper.database_name(model)) && enabled?(model.name.to_s)
end