Sha256: 41a530069ef721118ad67adebaa9968df308a2b73b1c856bb12bf38ba495a682

Contents?: true

Size: 937 Bytes

Versions: 2

Compression:

Stored size: 937 Bytes

Contents

module Qyu
  module Store
    module ActiveRecord
      class ConfigurationValidator
        REQUIRED_ATTRIBUTES = %i(db_type db_name db_host db_port).freeze

        attr_reader :errors

        def initialize(config)
          @config = config
          @errors = []
        end

        def valid?
          validate
          errors.empty?
        end

        def validate
          REQUIRED_ATTRIBUTES.each do |attribute|
            next if @config[attribute].present?

            @errors << "#{attribute} must be present."
          end

          validate_database_adapter
        end

        private

        def validate_database_adapter
          sample_config = { 'test' => { adapter: @config[:db_type] } }

          ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(sample_config).spec(:test)
        rescue LoadError => ex
          @errors << ex.message
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qyu-store-activerecord-1.0.1 lib/qyu/store/activerecord/configuration_validator.rb
qyu-store-activerecord-1.0.0 lib/qyu/store/activerecord/configuration_validator.rb