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.



174
175
176
177
178
179
# File 'lib/scss_lint/config.rb', line 174

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

  validate_linters
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/scss_lint/config.rb', line 9

def options
  @options
end

#warningsObject (readonly)

Returns the value of attribute warnings.



9
10
11
# File 'lib/scss_lint/config.rb', line 9

def warnings
  @warnings
end

Class Method Details

.defaultObject



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

def default
  load(DEFAULT_FILE, merge_with_default: false)
end

.linter_name(linter) ⇒ Object



36
37
38
39
# File 'lib/scss_lint/config.rb', line 36

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.



18
19
20
21
22
23
24
25
26
# File 'lib/scss_lint/config.rb', line 18

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

.user_fileObject

Returns the location of the user-wide scss-lint configuration.

This needs to be a method instead of a constant so that we can change the user’s home directory in tests.



32
33
34
# File 'lib/scss_lint/config.rb', line 32

def user_file
  File.join(Dir.home, FILE_NAME)
end

Instance Method Details

#disable_all_lintersObject



199
200
201
202
203
# File 'lib/scss_lint/config.rb', line 199

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

#disable_linter(linter) ⇒ Object



195
196
197
# File 'lib/scss_lint/config.rb', line 195

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

#enable_linter(linter) ⇒ Object



191
192
193
# File 'lib/scss_lint/config.rb', line 191

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

#enabled_lintersObject



181
182
183
184
185
# File 'lib/scss_lint/config.rb', line 181

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

#exclude_file(file_path) ⇒ Object



229
230
231
232
233
234
# File 'lib/scss_lint/config.rb', line 229

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

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

#exclude_patternsObject



217
218
219
# File 'lib/scss_lint/config.rb', line 217

def exclude_patterns
  @options.fetch('exclude', [])
end

#excluded_file?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
212
213
214
215
# File 'lib/scss_lint/config.rb', line 209

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)


221
222
223
224
225
226
227
# File 'lib/scss_lint/config.rb', line 221

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)


187
188
189
# File 'lib/scss_lint/config.rb', line 187

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

#linter_options(linter) ⇒ Object



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

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

#scss_filesObject

Returns Array.

Returns:

  • Array



237
238
239
240
241
242
243
# File 'lib/scss_lint/config.rb', line 237

def scss_files
  if path = @options['scss_files']
    Dir[path]
  else
    []
  end
end