Sha256: a9e0014445c780d9a5e5834556562819d9741108340ca8af9a4b909190d22242

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

require 'yaml'

module Oct

  class Settings

    def initialize(working_dir, options={})
      @working_dir = working_dir
      @options = options
      configure
    end

    def options
      @options
    end

  private

    # read options from YAML config
    def configure

      # config file default options
      configuration = {
                        :options => {
                          :verbose => false,
                          :color => 'AUTO'
                        }
                      }

      # set default config if not given on command line
      config = @options[:config]
      unless config
        config = [
                   File.join(@working_dir, "oct.conf"),
                   File.join(@working_dir, ".oct.conf"),
                   File.join(@working_dir, "config", "oct.conf"),
                   File.expand_path(File.join("~", ".oct.conf"))
                 ].detect { |filename| File.exists?(filename) }
      end

      if config && File.exists?(config)
        # rewrite options full path for config for later use
        @options[:config] = config

        # load options from the config file, overwriting hard-coded defaults
        config_contents = YAML::load(File.open(config))
        configuration.merge!(config_contents.symbolize_keys!) if config_contents && config_contents.is_a?(Hash)
      else
        # user specified a config file?, no error if user did not specify config file
        raise "config file not found" if @options[:config]
      end

      # the command line options override options read from the config file
      @options = configuration[:options].symbolize_keys!.merge!(@options)

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oct-0.3.3 lib/oct/settings.rb
oct-0.3.2 lib/oct/settings.rb
oct-0.3.1 lib/oct/settings.rb