Module: Appfuel::Config::FileLoader

Included in:
DefinitionDsl
Defined in:
lib/appfuel/config/file_loader.rb

Overview

Handle loading files and parsing them correctly based on their type. The file loader used for loading configuration data into a definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_moduleObject



8
9
10
# File 'lib/appfuel/config/file_loader.rb', line 8

def file_module
  @file_module ||= ::File
end

#json_moduleObject



12
13
14
# File 'lib/appfuel/config/file_loader.rb', line 12

def json_module
  @json_module || ::JSON
end

#yaml_moduleObject



16
17
18
# File 'lib/appfuel/config/file_loader.rb', line 16

def yaml_module
  @yaml_module ||= YAML
end

Instance Method Details

#load_file(definition) ⇒ Hash

Load file will search through a configuration’s definition file paths and use the first on that exists. It parse it based on the file type.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/appfuel/config/file_loader.rb', line 42

def load_file(definition)
  paths = definition.file

  paths.each do |path|
    ext = file_module.extname(path).strip.downcase[1..-1]
    parse_method = "parse_#{ext}"
    unless respond_to?(parse_method)
      fail "extension (#{ext}), for (#{definition.key}: #{path}) " +
           "is not valid, only yaml and json are supported"
    end

    if file_module.exists?(path)
      config = public_send(parse_method, path)
      unless config.is_a?(Hash)
        fail "[config #{parse_method}] config must be a hash"
      end
      config.deep_symbolize_keys!
      return config[definition.key]
    end
  end

  list = paths.join(',')
  fail "none of :#{definition.key} config files exist at (#{list})"
end

#parse_json(path) ⇒ Hash



22
23
24
25
# File 'lib/appfuel/config/file_loader.rb', line 22

def parse_json(path)
  file = file_module.read(path)
  json_module.parse(file)
end

#parse_yaml(path) ⇒ Hash Also known as: parse_yml



29
30
31
# File 'lib/appfuel/config/file_loader.rb', line 29

def parse_yaml(path)
  yaml_module.load_file(path)
end