Class: Navo::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/navo/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.

'.navo.yaml'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:, path:) ⇒ Configuration

Creates a configuration from the given options hash.

Parameters:

  • options (Hash)


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

def initialize(options:, path:)
  @options = options
  @path = path
end

Class Method Details

.from_file(config_file) ⇒ Navo::Configuration

Loads a configuration from a file.

Returns:



35
36
37
38
39
40
41
42
43
44
# File 'lib/navo/configuration.rb', line 35

def from_file(config_file)
  options =
    if yaml = YAML.load_file(config_file)
      yaml.to_hash
    else
      {}
    end

  new(options: options, path: config_file)
end

.load_applicableNavo::Configuration

Loads appropriate configuration file given the current working directory.

Returns:



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



107
108
109
# File 'lib/navo/configuration.rb', line 107

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, Symbol)


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

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

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

Set the configuration as if it were a hash.

Parameters:

  • key (String, Symbol)
  • value (Array, Hash, Number, String, Symbol)

Returns:

  • (Array, Hash, Number, String)


91
92
93
# File 'lib/navo/configuration.rb', line 91

def []=(key, value)
  @options[key.to_s] = value
end

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

Access the configuration as if it were a hash.

Parameters:

  • key (String, Symbol)

Returns:

  • (Array, Hash, Number, String)


99
100
101
# File 'lib/navo/configuration.rb', line 99

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

#repo_rootString

Returns the root of the repository to which this configuration applies.

Returns:

  • (String)


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

def repo_root
  File.dirname(@path)
end