class Mysticonfig::Loader

Configuration loader.

Public Class Methods

new(appname, default_config = {}) click to toggle source
# File lib/mysticonfig.rb, line 9
def initialize(appname, default_config = {})
  @filenames = Utils.generate_config_filenames appname
  @default_config = default_config
end

Public Instance Methods

load() click to toggle source

Find and load config file.

# File lib/mysticonfig.rb, line 16
def load
  config_file = find_file @filenames

  config = Utils.load_auto config_file
  config.empty? ? @default_config : @default_config.merge(config)
end
load_json() click to toggle source

Find and load JSON config file.

# File lib/mysticonfig.rb, line 25
def load_json
  json_config_file = Utils.lookup_file @filenames[:json]

  config = Utils.load_json json_config_file
  config.empty? ? @default_config : @default_config.merge(config)
end
load_yaml() click to toggle source

Find and load YAML config file.

# File lib/mysticonfig.rb, line 34
def load_yaml
  yaml_config_files = @filenames[:yaml]
  yaml_config_file = nil

  yaml_config_files.each do |file|
    yaml_config_file = Utils.lookup_file file
    unless yaml_config_file.nil?
      config = Utils.load_yaml(yaml_config_file)
      return config.empty? ? @default_config : @default_config.merge(config)
    end
  end

  @default_config # Return default config when can't load config file
end

Private Instance Methods

find_file(filenames) click to toggle source

Find an existent config file from all posible files.

# File lib/mysticonfig.rb, line 55
def find_file(filenames)
  return nil if filenames.nil?

  filenames.each_value do |value|
    if value.is_a? Array
      value.each do |file|
        file_path = Utils.lookup_file file
        return file_path unless file_path.nil?
      end
    else
      file_path = Utils.lookup_file value
      return file_path unless file_path.nil?
    end
  end

  nil
end