Module: Formatron::Config::Reader

Defined in:
lib/formatron/config/reader.rb

Overview

reads config directories into config hash

Class Method Summary collapse

Class Method Details

.do_entries(entries, default_file, config) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/formatron/config/reader.rb', line 16

def self.do_entries(entries, default_file, config)
  entries.each do |entry|
    basename = File.basename(entry)
    next if ['.', '..', default_file].include?(basename)
    config[basename] = {} unless config[basename].is_a? Hash
    config[basename].deep_merge!(
      read(entry, default_file)
    ) if File.directory?(entry)
    config[basename] = File.read(entry) if File.file?(entry)
  end
end

.read(dir, default_file) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/formatron/config/reader.rb', line 8

def self.read(dir, default_file)
  default = File.join(dir, default_file)
  config = File.file?(default) ? JSON.parse(File.read(default)) : {}
  entries = Dir.glob(File.join(dir, '*'), File::FNM_DOTMATCH)
  do_entries entries, default_file, config
  config
end