class Modulorails::Validators::DatabaseConfiguration
Author: Matthieu 'ciappa_m' Ciappara This holds the rules to configure the database by respecting Modulotech's norms.
Public Class Methods
call()
click to toggle source
# File lib/modulorails/validators/database_configuration.rb, line 28 def self.call new.call end
new()
click to toggle source
# File lib/modulorails/validators/database_configuration.rb, line 6 def initialize # All rules are invalid by default @rules = { standard_config_file_location: false, test_database_not_equals_dev_database: false, development: { configurable_username: false, configurable_password: false, configurable_database: false, configurable_host: false, configurable_port: false }, test: { configurable_username: false, configurable_password: false, configurable_database: false, configurable_host: false, configurable_port: false } } end
Public Instance Methods
call()
click to toggle source
# File lib/modulorails/validators/database_configuration.rb, line 32 def call database_configuration = check_standard_config_file_location return false unless database_configuration check_test_database_not_equals_dev_database(database_configuration) check_rules_for_environment(database_configuration, :development) check_rules_for_environment(database_configuration, :test) get_invalid_rules end
Private Instance Methods
check_configurable_key_for_environment(config, env, key)
click to toggle source
Check if the given key is configurable for the given environment
# File lib/modulorails/validators/database_configuration.rb, line 84 def check_configurable_key_for_environment(config, env, key) valid_rule = config[env.to_s][key] =~ /<%=\s*ENV\.fetch\(\S+,\s*\S+\)\s*%>/ valid_rule ||= config[env.to_s][key] =~ /<%=\s*ENV\.fetch\(.+\)\s*\{\s*\S+\s*\}\s*%>/ # Use of `!!` to convert `nil` to `false` and `0` to `true` @rules[env][:"configurable_#{key}"] = !!valid_rule end
check_rules_for_environment(config, env)
click to toggle source
Check all rules for an environment
# File lib/modulorails/validators/database_configuration.rb, line 76 def check_rules_for_environment(config, env) @rules[env].keys.each do |rule| key = rule.to_s.gsub(/configurable_/, '') check_configurable_key_for_environment(config, env, key) end end
check_standard_config_file_location()
click to toggle source
# File lib/modulorails/validators/database_configuration.rb, line 53 def check_standard_config_file_location # Load the configuration config = Psych.load_file(Rails.root.join('config/database.yml')) # If no exception was raised, then the database configuration file is at standard location @rules[:standard_config_file_location] = true config rescue StandardError =>e # An exception was raised, either the file is not a the standard location, either it just # cannot be read. Either way, we consider the config as invalid @rules[:standard_config_file_location] = false nil end
check_test_database_not_equals_dev_database(config)
click to toggle source
The database for tests MUST NOT be the same as the development database since the test database is rewritten each time the tests are launched
# File lib/modulorails/validators/database_configuration.rb, line 70 def check_test_database_not_equals_dev_database(config) test_eq_database = config['test']['database'] != config['development']['database'] @rules[:test_database_not_equals_dev_database] = test_eq_database end
get_invalid_rules()
click to toggle source
# File lib/modulorails/validators/database_configuration.rb, line 45 def get_invalid_rules dev = @rules[:development].select { |_k, v| v == false }.keys.map { |k| "development.#{k}" } test = @rules[:test].select { |_k, v| v == false }.keys.map { |k| "test.#{k}" } general = @rules.select { |_k, v| v == false }.keys general + dev + test end