Sha256: b94abe14d2ebd983927699b43aedb53ea04aacd0b98a850549975ff8b9a39504

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'erb'
require_relative 'args'
require_relative 'database_config'
require_relative 'deep'

# App config including database configs.
class SqluiConfig
  attr_reader :name, :port, :environment, :list_url_path, :database_configs

  def initialize(filename, overrides = {})
    config = YAML.safe_load(ERB.new(File.read(filename)).result, aliases: true).deep_merge!(overrides)
    config.deep_symbolize_keys!
    @name = Args.fetch_non_empty_string(config, :name).strip
    @port = Args.fetch_non_empty_int(config, :port)
    @environment = Args.fetch_non_empty_string(config, :environment).strip
    @list_url_path = Args.fetch_non_empty_string(config, :list_url_path).strip
    raise ArgumentError, 'list_url_path should start with a /' unless @list_url_path.start_with?('/')
    if @list_url_path.length > 1 && @list_url_path.end_with?('/')
      raise ArgumentError, 'list_url_path should not end with a /'
    end

    databases = Args.fetch_non_empty_hash(config, :databases)
    @database_configs = databases.map do |_, current|
      DatabaseConfig.new(current)
    end
  end

  def database_config_for(url_path:)
    config = @database_configs.find { |database| database.url_path == url_path }
    raise ArgumentError, "no config found for path #{url_path}" unless config

    config
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sqlui-0.1.49 app/sqlui_config.rb
sqlui-0.1.48 app/sqlui_config.rb
sqlui-0.1.47 app/sqlui_config.rb
sqlui-0.1.46 app/sqlui_config.rb
sqlui-0.1.45 app/sqlui_config.rb
sqlui-0.1.44 app/sqlui_config.rb
sqlui-0.1.43 app/sqlui_config.rb
sqlui-0.1.42 app/sqlui_config.rb
sqlui-0.1.41 app/sqlui_config.rb