Class: Figgy::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/figgy/finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Finder

Returns a new instance of Finder.



3
4
5
# File 'lib/figgy/finder.rb', line 3

def initialize(config)
  @config = config
end

Instance Method Details

#all_key_namesArray<String>

Returns the names of all unique configuration keys.

Returns:

  • (Array<String>)

    the names of all unique configuration keys



44
45
46
# File 'lib/figgy/finder.rb', line 44

def all_key_names
  Dir[*file_globs].map { |file| File.basename(file).sub(/\..+$/, '') }.uniq
end

#files_for(name) ⇒ Array<String>

Returns the paths to all files to load for configuration key name.

Parameters:

  • name (String)

    the configuration key to search for

Returns:

  • (Array<String>)

    the paths to all files to load for configuration key name



39
40
41
# File 'lib/figgy/finder.rb', line 39

def files_for(name)
  Dir[*file_globs(name)]
end

#load(name) ⇒ Object

Searches for files defining the configuration key name, merging each instance found with the previous. In this way, the overlay configuration at production/foo.yml can override values in foo.yml.

If the contents of the file were a Hash, Figgy will translate it into a Figgy::Hash and perform deep-merging for all overlays. This allows you to override only a single key deep within the configuration, and to access it using dot-notation, symbol keys or string keys.

Parameters:

  • name (String)

    the configuration file to load

Returns:

  • Whatever was in the config file loaded

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/figgy/finder.rb', line 19

def load(name)
  files = files_for(name)
  if files.empty?
    raise(Figgy::FileNotFound, "Can't find config files for key: #{name.inspect}")
  end

  final_result = files.reduce(nil) do |result, file|
    object = @config.handler_for(file).call(File.read(file))
    if result && result.respond_to?(:merge)
      deep_merge(result, object)
    else
      object
    end
  end

  deep_freeze(to_figgy_hash(final_result))
end