class ConfigPlus::DefaultLoaderLogic

This loader reads configuration from the specified a YAML file with its load_from method. When a directory is specified this recurses a file tree and reads all YAML files.

Attributes

extension[R]

Public Class Methods

new(extension) click to toggle source
# File lib/config_plus/default_loader_logic.rb, line 10
def initialize(extension)
  @extension = extension || [:yml, :yaml]
end

Public Instance Methods

load_from(path) click to toggle source
# File lib/config_plus/default_loader_logic.rb, line 14
def load_from(path)
  raise Errno::ENOENT, "No such file or directory: `#{path}'" unless File.exist?(path)
  return load_file(path) if File.file?(path)
  load_dir(path)
end

Private Instance Methods

load_dir(dirpath) click to toggle source
# File lib/config_plus/default_loader_logic.rb, line 29
def load_dir(dirpath)
  ext = Array(extension).join(',')
  path = File.join(dirpath, '**', "*.{#{ext}}")
  Dir.glob(path).sort.inject({}) {|h, filepath|
    Merger.merge(h, load_file(filepath))
  }
end
load_file(filepath) click to toggle source
# File lib/config_plus/default_loader_logic.rb, line 24
def load_file(filepath)
  content = open(filepath).read
  YAML.load(content).to_hash
end