Class: Texas::Build::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/texas/build/config_loader.rb

Overview

This class looks for a given filename in the current and all parent directories.

# Given the following directory structure:

~/
  projects/
    some_texas_project/
      .texasrc
  .texasrc

In the above case, it would find and load ~/.texasrc and ~/projects/some_texas_project/.texasrc with the latter one overriding settings in the former one.

Instance Method Summary collapse

Constructor Details

#initialize(start_dir, filename) ⇒ ConfigLoader

Returns a new instance of ConfigLoader.



18
19
20
21
# File 'lib/texas/build/config_loader.rb', line 18

def initialize(start_dir, filename)
  @start_dir = start_dir
  @filename = filename
end

Instance Method Details

#all_config_filesObject

Returns all found files with the given filename in the current and all parent directories.

Example:

# Given the following directory structure:

~/
  projects/
    some_texas_project/
      .texasrc
  .texasrc

all_config_files
# => ["~/.texasrc", "~/projects/some_texas_project/.texasrc"]


37
38
39
40
41
42
43
44
# File 'lib/texas/build/config_loader.rb', line 37

def all_config_files
  found_files = []
  each_parent_dir(@start_dir) do |dir|
    filename = File.join(dir, @filename)
    found_files.unshift filename if File.exist?(filename)
  end
  found_files
end

#each_parent_dir(dir) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/texas/build/config_loader.rb', line 46

def each_parent_dir(dir)
  old_length = nil
  while dir != '.' && dir.length != old_length
    yield dir
    old_length = dir.length
    dir = File.dirname(dir)
  end
end

#to_hashObject

Returns a hash of all the found config files.

Example:

# Given the following directory structure:

~/
  projects/
    some_texas_project/
      .texasrc
  .texasrc

# ~/.texasrc

document:
  author: "John Doe"
  some_value: 42

# ~/projects/some_texas_project/.texasrc

document:
  title: "My Document"
  some_value: 123

to_hash
# => {:document => {
       :author => "John Doe", 
       :title => "My Document", 
       :some_value => 123}}


84
85
86
87
88
89
90
# File 'lib/texas/build/config_loader.rb', line 84

def to_hash
  hash = {}
  all_config_files.each do |filename|
    hash.deep_merge! YAML.load_file(filename) 
  end
  hash
end