Class: LintTrappings::Configuration

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

Overview

Stores runtime configuration for the application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Creates a configuration from the given options hash.

Parameters:

  • options (Hash) (defaults to: {})

18
19
20
# File 'lib/lint_trappings/configuration.rb', line 18

def initialize(options = {})
  @hash = options.dup
end

Instance Attribute Details

#pathString

The path of this configuration, if it was loaded from a file.

Used in certain scenarios to determine the absolute path of a file specified in a configuration (i.e. getting the path relative to the location of the configuration file itself).

Returns:

  • (String)

13
14
15
# File 'lib/lint_trappings/configuration.rb', line 13

def path
  @path
end

Instance Method Details

#==(other) ⇒ true, false

Compares this configuration with another.

Parameters:

Returns:

  • (true, false)

    whether the given configuration is equivalent


27
28
29
# File 'lib/lint_trappings/configuration.rb', line 27

def ==(other)
  super || @hash == other.hash
end

#[](key) ⇒ Object


35
36
37
# File 'lib/lint_trappings/configuration.rb', line 35

def [](key)
  @hash[key]
end

#delete(key) ⇒ Object


39
40
41
# File 'lib/lint_trappings/configuration.rb', line 39

def delete(key)
  @hash.delete(key)
end

#fetch(*args, &block) ⇒ Object


31
32
33
# File 'lib/lint_trappings/configuration.rb', line 31

def fetch(*args, &block)
  @hash.fetch(*args, &block)
end

#for_linter(linterish) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lint_trappings/configuration.rb', line 56

def for_linter(linterish)
  linter_name =
    case linterish
    when Class, LintTrappings::Linter
      linterish.canonical_name
    else
      linterish.to_s
    end

  conf = @hash.fetch('linters', {}).fetch(linter_name, {}).dup
  conf['severity'] ||= @hash.fetch('default_severity', :error)
  conf['severity'] = conf['severity'].to_sym
  conf
end

#merge(config) ⇒ LintTrappings::Configuration

Merges the given configuration with this one.

The provided configuration will either add to or replace any options defined in this configuration.


51
52
53
54
# File 'lib/lint_trappings/configuration.rb', line 51

def merge(config)
  merged_hash = smart_merge(@hash, config.hash)
  self.class.new(merged_hash)
end