Class: Yamlint::Config

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

Constant Summary collapse

DEFAULT_CONFIG_FILES =
%w[
  .yamllint
  .yamllint.yaml
  .yamllint.yml
].freeze
YAML_FILE_PATTERNS =
%w[
  *.yaml
  *.yml
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



20
21
22
23
24
25
# File 'lib/yamlint/config.rb', line 20

def initialize(options = {})
  @extends = options[:extends]
  @yaml_files = options[:'yaml-files'] || options['yaml-files'] || YAML_FILE_PATTERNS
  @ignore = options[:ignore] || options['ignore'] || []
  @rules = normalize_rules(options[:rules] || options['rules'] || {})
end

Instance Attribute Details

#extendsObject (readonly)

Returns the value of attribute extends.



18
19
20
# File 'lib/yamlint/config.rb', line 18

def extends
  @extends
end

#ignoreObject (readonly)

Returns the value of attribute ignore.



18
19
20
# File 'lib/yamlint/config.rb', line 18

def ignore
  @ignore
end

#rulesObject (readonly)

Returns the value of attribute rules.



18
19
20
# File 'lib/yamlint/config.rb', line 18

def rules
  @rules
end

#yaml_filesObject (readonly)

Returns the value of attribute yaml_files.



18
19
20
# File 'lib/yamlint/config.rb', line 18

def yaml_files
  @yaml_files
end

Class Method Details

.build_from_hash(data) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/yamlint/config.rb', line 59

def self.build_from_hash(data)
  base_config = if data['extends']
                  Presets.get(data['extends'])
                else
                  {}
                end

  merged = deep_merge(base_config, symbolize_keys(data))
  new(merged)
end

.find_config_fileObject



50
51
52
# File 'lib/yamlint/config.rb', line 50

def self.find_config_file
  DEFAULT_CONFIG_FILES.find { |f| File.exist?(f) }
end

.load(path = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/yamlint/config.rb', line 27

def self.load(path = nil)
  if path
    load_from_file(path)
  else
    load_default
  end
end

.load_defaultObject



41
42
43
44
45
46
47
48
# File 'lib/yamlint/config.rb', line 41

def self.load_default
  config_file = find_config_file
  if config_file
    load_from_file(config_file)
  else
    new_with_preset('default')
  end
end

.load_from_file(path) ⇒ Object



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

def self.load_from_file(path)
  content = File.read(path)
  data = YAML.safe_load(content, permitted_classes: [Symbol]) || {}
  build_from_hash(data)
end

.new_with_preset(preset_name) ⇒ Object



54
55
56
57
# File 'lib/yamlint/config.rb', line 54

def self.new_with_preset(preset_name)
  preset = Presets.get(preset_name)
  new(preset)
end

Instance Method Details

#rule_config(rule_id) ⇒ Object



70
71
72
# File 'lib/yamlint/config.rb', line 70

def rule_config(rule_id)
  @rules[rule_id.to_s] || @rules[rule_id.to_sym] || {}
end

#rule_enabled?(rule_id) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/yamlint/config.rb', line 74

def rule_enabled?(rule_id)
  config = rule_config(rule_id)
  config != 'disable' && config != false
end