Class: SCSSLint::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/scss_lint/config.rb

Overview

Loads and manages application configuration.

Constant Summary collapse

FILE_NAME =
'.scss-lint.yml'
DEFAULT_FILE =
File.join(SCSS_LINT_HOME, 'config', 'default.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.



202
203
204
205
206
207
# File 'lib/scss_lint/config.rb', line 202

def initialize(options)
  @options = options
  @warnings = []

  validate_linters
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/scss_lint/config.rb', line 14

def options
  @options
end

#preferredObject

If this config should be preferred over others



13
14
15
# File 'lib/scss_lint/config.rb', line 13

def preferred
  @preferred
end

#warningsObject (readonly)

Returns the value of attribute warnings.



14
15
16
# File 'lib/scss_lint/config.rb', line 14

def warnings
  @warnings
end

Class Method Details

.defaultObject



17
18
19
# File 'lib/scss_lint/config.rb', line 17

def default
  load(DEFAULT_FILE, merge_with_default: false)
end

.for_file(file_path) ⇒ Object

Loads the configuration for a given file.



34
35
36
37
38
39
40
41
42
# File 'lib/scss_lint/config.rb', line 34

def for_file(file_path)
  directory = File.dirname(File.expand_path(file_path))
  @dir_to_config ||= {}
  @dir_to_config[directory] ||=
    begin
      config_file = possible_config_files(directory).find { |path| path.file? }
      Config.load(config_file.to_s) if config_file
    end
end

.linter_name(linter) ⇒ Object



44
45
46
47
# File 'lib/scss_lint/config.rb', line 44

def linter_name(linter)
  linter = linter.is_a?(Class) ? linter : linter.class
  linter.name.split('::')[2..-1].join('::')
end

.load(file, options = {}) ⇒ Object

Loads a configuration from a file, merging it with the default configuration.



23
24
25
26
27
28
29
30
31
# File 'lib/scss_lint/config.rb', line 23

def load(file, options = {})
  config_options = load_options_hash_from_file(file)

  if options.fetch(:merge_with_default, true)
    config_options = smart_merge(default_options_hash, config_options)
  end

  Config.new(config_options)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



209
210
211
# File 'lib/scss_lint/config.rb', line 209

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

#disable_all_lintersObject



232
233
234
235
236
# File 'lib/scss_lint/config.rb', line 232

def disable_all_linters
  @options['linters'].values.each do |linter_config|
    linter_config['enabled'] = false
  end
end

#disable_linter(linter) ⇒ Object



228
229
230
# File 'lib/scss_lint/config.rb', line 228

def disable_linter(linter)
  linter_options(linter)['enabled'] = false
end

#enable_linter(linter) ⇒ Object



224
225
226
# File 'lib/scss_lint/config.rb', line 224

def enable_linter(linter)
  linter_options(linter)['enabled'] = true
end

#enabled_lintersObject



214
215
216
217
218
# File 'lib/scss_lint/config.rb', line 214

def enabled_linters
  LinterRegistry.extract_linters_from(@options['linters'].keys).select do |linter|
    linter_options(linter)['enabled']
  end
end

#exclude_file(file_path) ⇒ Object



258
259
260
261
262
263
# File 'lib/scss_lint/config.rb', line 258

def exclude_file(file_path)
  abs_path = File.expand_path(file_path)

  @options['exclude'] ||= []
  @options['exclude'] << abs_path
end

#excluded_file?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
245
246
247
248
# File 'lib/scss_lint/config.rb', line 242

def excluded_file?(file_path)
  abs_path = File.expand_path(file_path)

  @options.fetch('exclude', []).any? do |exclusion_glob|
    File.fnmatch(exclusion_glob, abs_path)
  end
end

#excluded_file_for_linter?(file_path, linter) ⇒ Boolean

Returns:

  • (Boolean)


250
251
252
253
254
255
256
# File 'lib/scss_lint/config.rb', line 250

def excluded_file_for_linter?(file_path, linter)
  abs_path = File.expand_path(file_path)

  linter_options(linter).fetch('exclude', []).any? do |exclusion_glob|
    File.fnmatch(exclusion_glob, abs_path)
  end
end

#linter_enabled?(linter) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/scss_lint/config.rb', line 220

def linter_enabled?(linter)
  linter_options(linter)['enabled']
end

#linter_options(linter) ⇒ Object



238
239
240
# File 'lib/scss_lint/config.rb', line 238

def linter_options(linter)
  @options['linters'][self.class.linter_name(linter)]
end