class Gemstash::Configuration

Constants

DEFAULTS
DEFAULT_FILE

Public Class Methods

new(file: nil, config: nil) click to toggle source
# File lib/gemstash/configuration.rb, line 36
def initialize(file: nil, config: nil)
  if config
    @config = DEFAULTS.merge(config).freeze
    return
  end

  raise MissingFileError, file if file && !File.exist?(file)

  file ||= default_file

  if File.exist?(file)
    @config = parse_config(file)
    @config = DEFAULTS.merge(@config)
    @config.freeze
  else
    @config = DEFAULTS
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/gemstash/configuration.rb, line 59
def [](key)
  @config[key]
end
database_connection_config() click to toggle source

@return [Hash] Sequel connection configuration hash

# File lib/gemstash/configuration.rb, line 64
def database_connection_config
  case self[:db_adapter]
  when "sqlite3"
    { max_connections: 1 }.merge(self[:db_connection_options])
  when "postgres", "mysql", "mysql2"
    { max_connections: (self[:puma_workers] * self[:puma_threads]) + 1 }.merge(self[:db_connection_options])
  else
    raise "Unsupported DB adapter: '#{self[:db_adapter]}'"
  end
end
default?(key) click to toggle source
# File lib/gemstash/configuration.rb, line 55
def default?(key)
  @config[key] == DEFAULTS[key]
end

Private Instance Methods

default_file() click to toggle source
# File lib/gemstash/configuration.rb, line 77
def default_file
  # Support the config file being specified via environment variable
  gemstash_config = ENV["GEMSTASH_CONFIG"]
  return gemstash_config if gemstash_config

  # If no environment variable is used, fall back to the normal defaults
  File.exist?("#{DEFAULT_FILE}.erb") ? "#{DEFAULT_FILE}.erb" : DEFAULT_FILE
end
parse_config(file) click to toggle source
# File lib/gemstash/configuration.rb, line 86
def parse_config(file)
  if file.end_with?(".erb")
    YAML.load(ERB.new(File.read(file)).result) || {}
  else
    YAML.load_file(file) || {}
  end
end