Class: SlimLint::Configuration

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

Overview

Stores runtime configuration for the application.

The purpose of this class is to validate and ensure all configurations satisfy some basic pre-conditions so other parts of the application don’t have to check the configuration for errors. It should have no knowledge of how these configuration values are ultimately used.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Creates a configuration from the given options hash.

Parameters:

  • options (Hash)


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

def initialize(options)
  @hash = options
  validate
end

Instance Attribute Details

#hashObject (readonly)

Internal hash storing the configuration.



12
13
14
# File 'lib/slim_lint/configuration.rb', line 12

def hash
  @hash
end

Instance Method Details

#==(other) ⇒ true, false

Compares this configuration with another.

Parameters:

Returns:

  • (true, false)

    whether the given configuration is equivalent



34
35
36
# File 'lib/slim_lint/configuration.rb', line 34

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

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

Access the configuration as if it were a hash.

Parameters:

  • key (String)

Returns:

  • (Array, Hash, Number, String)


26
27
28
# File 'lib/slim_lint/configuration.rb', line 26

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

#for_linter(linter) ⇒ Object

Returns a non-modifiable configuration for the specified linter.

Parameters:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/slim_lint/configuration.rb', line 41

def for_linter(linter)
  linter_name =
    case linter
    when Class
      linter.name.split('::').last
    when SlimLint::Linter
      linter.name
    end

  @hash['linters'].fetch(linter_name, {}).dup.freeze
end

#merge(config) ⇒ Object

Merges the given configuration with this one, returning a new SlimLint::Configuration. The provided configuration will either add to or replace any options defined in this configuration.

Parameters:



58
59
60
# File 'lib/slim_lint/configuration.rb', line 58

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