class Gerrit::Configuration

Stores runtime configuration for the application.

This is intended to define helper methods for accessing configuration so this logic can be shared amongst the various components of the system.

Constants

FILE_NAME

Name of the configuration file.

Public Class Methods

from_file(config_file) click to toggle source

Loads a configuration from a file.

@return [Gerrit::Configuration]

# File lib/gerrit/configuration.rb, line 35
def from_file(config_file)
  options =
    if yaml = YAML.load_file(config_file)
      yaml.to_hash
    else
      {}
    end

  new(options)
end
load_applicable() click to toggle source

Loads appropriate configuration file given the current working directory.

@return [Gerrit::Configuration]

# File lib/gerrit/configuration.rb, line 18
def load_applicable
  current_directory = File.expand_path(Dir.pwd)
  config_file = applicable_config_file(current_directory)

  if config_file
    from_file(config_file)
  else
    raise Errors::ConfigurationMissingError,
          "No configuration file '#{FILE_NAME}' was found in the " \
          "current directory or any ancestor directory.\n\n" \
          "See #{REPO_URL}#configuration for instructions on setting up."
  end
end
new(options) click to toggle source

Creates a configuration from the given options hash.

@param options [Hash]

# File lib/gerrit/configuration.rb, line 66
def initialize(options)
  @options = options
end

Private Class Methods

applicable_config_file(directory) click to toggle source

Returns the first valid configuration file found, starting from the current working directory and ascending to ancestor directories.

@param directory [String] @return [String, nil]

# File lib/gerrit/configuration.rb, line 53
def applicable_config_file(directory)
  Pathname.new(directory)
          .enum_for(:ascend)
          .map { |dir| dir + FILE_NAME }
          .find do |config_file|
    config_file if config_file.exist?
  end
end

Public Instance Methods

==(other) click to toggle source

Compares this configuration with another.

@param other [HamlLint::Configuration] @return [true,false] whether the given configuration is equivalent

Calls superclass method
# File lib/gerrit/configuration.rb, line 90
def ==(other)
  super || @options == other.instance_variable_get('@options')
end
[](key) click to toggle source

Access the configuration as if it were a hash.

@param key [String, Symbol] @return [Array, Hash, Number, String]

# File lib/gerrit/configuration.rb, line 74
def [](key)
  @options[key.to_s]
end
fetch(key, *args) click to toggle source

Access the configuration as if it were a hash.

@param key [String, Symbol] @return [Array, Hash, Number, String]

# File lib/gerrit/configuration.rb, line 82
def fetch(key, *args)
  @options.fetch(key.to_s, *args)
end