Class: Gerrit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/gerrit/configuration.rb

Overview

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.

Constant Summary collapse

FILE_NAME =

Name of the configuration file.

'.gerrit.yaml'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Creates a configuration from the given options hash.

Parameters:

  • options (Hash)


66
67
68
# File 'lib/gerrit/configuration.rb', line 66

def initialize(options)
  @options = options
end

Class Method Details

.from_file(config_file) ⇒ Gerrit::Configuration

Loads a configuration from a file.



35
36
37
38
39
40
41
42
43
44
# 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_applicableGerrit::Configuration

Loads appropriate configuration file given the current working directory.



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Method Details

#==(other) ⇒ true, false

Compares this configuration with another.

Parameters:

  • other (HamlLint::Configuration)

Returns:

  • (true, false)

    whether the given configuration is equivalent



90
91
92
# File 'lib/gerrit/configuration.rb', line 90

def ==(other)
  super || @options == other.instance_variable_get('@options')
end

#[](key) ⇒ Array, ...

Access the configuration as if it were a hash.

Parameters:

  • key (String, Symbol)

Returns:

  • (Array, Hash, Number, String)


74
75
76
# File 'lib/gerrit/configuration.rb', line 74

def [](key)
  @options[key.to_s]
end

#fetch(key, *args) ⇒ Array, ...

Access the configuration as if it were a hash.

Parameters:

  • key (String, Symbol)

Returns:

  • (Array, Hash, Number, String)


82
83
84
# File 'lib/gerrit/configuration.rb', line 82

def fetch(key, *args)
  @options.fetch(key.to_s, *args)
end