class NeuroHmmerApp::Config

Capture our configuration system.

Attributes

config_file[R]
data[R]

Public Class Methods

new(data = {}) click to toggle source
# File lib/neurohmmerapp/config.rb, line 11
def initialize(data = {})
  @data = symbolise data
  @config_file = @data.delete(:config_file) || default_config_file
  @config_file = File.expand_path(@config_file)
  @data = parse_config_file.update @data
  @data = defaults.update @data
end

Public Instance Methods

[](key) click to toggle source

Get.

# File lib/neurohmmerapp/config.rb, line 22
def [](key)
  data[key]
end
[]=(key, value) click to toggle source

Set.

# File lib/neurohmmerapp/config.rb, line 27
def []=(key, value)
  data[key] = value
end
include?(key) click to toggle source

Exists?

# File lib/neurohmmerapp/config.rb, line 32
def include?(key)
  data.include? key
end
write_config_file() click to toggle source

Write config data to config file.

# File lib/neurohmmerapp/config.rb, line 37
def write_config_file
  return unless config_file

  File.open(config_file, 'w') do |f|
    f.puts(data.delete_if { |_, v| v.nil? }.to_yaml)
  end
end

Private Instance Methods

default_config_file() click to toggle source
# File lib/neurohmmerapp/config.rb, line 83
def default_config_file
  '~/.neurohmmerapp.conf'
end
defaults() click to toggle source

Default configuration data.

# File lib/neurohmmerapp/config.rb, line 73
def defaults
  {
    :num_threads    => 1,
    :port           => 4567,
    :host           => '0.0.0.0',
    :public_dir     => File.join(Dir.home, '.neurohmmerapp/'),
    :max_characters => 'undefined'
  }
end
file?(file) click to toggle source
# File lib/neurohmmerapp/config.rb, line 68
def file?(file)
  file && File.exist?(file) && File.file?(file)
end
parse_config_file() click to toggle source

Parses and returns data from config_file if it exists. Returns {} otherwise.

# File lib/neurohmmerapp/config.rb, line 56
def parse_config_file
  unless file? config_file
    logger.debug "Configuration file not found: #{config_file}"
    return {}
  end

  logger.debug "Reading configuration file: #{config_file}."
  symbolise YAML.load_file(config_file)
rescue => error
  raise CONFIG_FILE_ERROR.new(config_file, error)
end
symbolise(data) click to toggle source

Symbolizes keys. Changes ‘database` key to `database_dir`.

# File lib/neurohmmerapp/config.rb, line 48
def symbolise(data)
  return {} unless data
  # Symbolize keys.
  Hash[data.map { |k, v| [k.to_sym, v] }]
end