Class: Rubikon::Config::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/rubikon/config/factory.rb

Overview

The configuration factory is used to load one or more configuration files from different search paths and using different file formats, e.g. YAML.

Author:

  • Sebastian Staudt

Since:

  • 0.5.0

Constant Summary collapse

PROVIDERS =

Providers available for use

Since:

  • 0.5.0

[ :auto, :ini, :yaml ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, search_paths, provider = :yaml) ⇒ Factory

Creates a new factory instance with a given file name to be searched in the given paths and using the specified provider to load the configuration data from the files.

Parameters:

  • name (String)

    The name of the configuration file

  • search_paths (Array<String>)

    An array of paths to be searched for configuration files

  • provider (PROVIDERS) (defaults to: :yaml)

    The provider to use for loading configuration data from the files found

Since:

  • 0.5.0



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubikon/config/factory.rb', line 46

def initialize(name, search_paths, provider = :yaml)
  provider = :auto unless PROVIDERS.include?(provider)
  @provider = Config.const_get("#{provider.to_s.capitalize}Provider")

  @files  = []
  @config = {}
  search_paths.each do |path|
    config_file = File.join path, name
    if File.exists? config_file
      @config.merge! @provider.load_config(config_file)
      @files << config_file
    end
  end
end

Instance Attribute Details

#configHash (readonly)

Returns The configuration data loaded from the configuration files found inside the search paths.

Returns:

  • (Hash)

    The configuration data loaded from the configuration files found inside the search paths

Since:

  • 0.5.0



31
32
33
# File 'lib/rubikon/config/factory.rb', line 31

def config
  @config
end

#filesArray<String> (readonly)

Returns The paths of the configuration files found and loaded.

Returns:

  • (Array<String>)

    The paths of the configuration files found and loaded

Since:

  • 0.5.0



35
36
37
# File 'lib/rubikon/config/factory.rb', line 35

def files
  @files
end

Instance Method Details

#save_config(config, file) ⇒ Object

Save the given configuration into the specified file

Parameters:

  • The (Hash)

    configuration to save

  • The (String)

    file path where the configuration should be saved

Since:

  • 0.6.0



66
67
68
69
70
71
72
# File 'lib/rubikon/config/factory.rb', line 66

def save_config(config, file)
  unless config.is_a? Hash
    raise ArgumentError.new('Configuration has to be a Hash')
  end

  @provider.save_config config, file
end