Class: Gemstash::Configuration

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

Overview

:nodoc:

Defined Under Namespace

Classes: MissingFileError

Constant Summary collapse

DEFAULTS =
{
  cache_type: "memory",
  base_path: File.expand_path("~/.gemstash"),
  db_adapter: "sqlite3",
  bind: "tcp://0.0.0.0:9292",
  rubygems_url: "https://rubygems.org",
  ignore_gemfile_source: false,
  protected_fetch: false,
  fetch_timeout: 20,
  # Actual default for db_connection_options is dynamic based on the adapter
  db_connection_options: {},
  puma_threads: 16,
  puma_workers: 1,
  cache_expiration: 30 * 60,
  cache_max_size: 500
}.freeze
DEFAULT_FILE =
File.expand_path("~/.gemstash/config.yml").freeze

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, config: nil) ⇒ Configuration

Returns a new instance of Configuration.

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#[](key) ⇒ Object



59
60
61
# File 'lib/gemstash/configuration.rb', line 59

def [](key)
  @config[key]
end

#database_connection_configHash

Returns Sequel connection configuration hash.

Returns:

  • (Hash)

    Sequel connection configuration hash



64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/gemstash/configuration.rb', line 55

def default?(key)
  @config[key] == DEFAULTS[key]
end