Class: ThemeCheck::Config

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

Constant Summary collapse

DOTFILE =
'.theme-check.yml'
DEFAULT_CONFIG =
"#{__dir__}/../../config/default.yml"
BOOLEAN =
[true, false]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: nil, configuration: nil, should_resolve_requires: true) ⇒ Config

Returns a new instance of Config.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/theme_check/config.rb', line 50

def initialize(root: nil, configuration: nil, should_resolve_requires: true)
  @configuration = if configuration
    validate_configuration(configuration)
  else
    {}
  end
  merge_with_default_configuration!(@configuration)

  @root = if root && @configuration.key?("root")
    Pathname.new(root).join(@configuration["root"])
  elsif root
    Pathname.new(root)
  end

  @only_categories = []
  @exclude_categories = []
  @auto_correct = false

  resolve_requires if @root && should_resolve_requires
end

Class Attribute Details

.last_loaded_configObject (readonly)

Returns the value of attribute last_loaded_config.



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

def last_loaded_config
  @last_loaded_config
end

Instance Attribute Details

#auto_correctObject

Returns the value of attribute auto_correct.



10
11
12
# File 'lib/theme_check/config.rb', line 10

def auto_correct
  @auto_correct
end

#exclude_categoriesObject

Returns the value of attribute exclude_categories.



10
11
12
# File 'lib/theme_check/config.rb', line 10

def exclude_categories
  @exclude_categories
end

#only_categoriesObject

Returns the value of attribute only_categories.



10
11
12
# File 'lib/theme_check/config.rb', line 10

def only_categories
  @only_categories
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Class Method Details

.defaultObject



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

def default
  @default ||= load_file(DEFAULT_CONFIG)
end

.find(root, needle = DOTFILE) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/theme_check/config.rb', line 32

def find(root, needle = DOTFILE)
  Pathname.new(root).descend.reverse_each do |path|
    pathname = path.join(needle)
    return pathname if pathname.exist?
  end
  nil
end

.from_hash(config) ⇒ Object



28
29
30
# File 'lib/theme_check/config.rb', line 28

def from_hash(config)
  new(configuration: config, should_resolve_requires: false)
end

.from_path(path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/theme_check/config.rb', line 15

def from_path(path)
  if (filename = find(path))
    new(root: filename.dirname, configuration: load_file(filename))
  else
    # No configuration file
    new(root: path)
  end
end

.from_string(config) ⇒ Object



24
25
26
# File 'lib/theme_check/config.rb', line 24

def from_string(config)
  new(configuration: YAML.load(config), should_resolve_requires: false)
end

.load_file(absolute_path) ⇒ Object



40
41
42
43
# File 'lib/theme_check/config.rb', line 40

def load_file(absolute_path)
  @last_loaded_config = absolute_path
  YAML.load_file(absolute_path)
end

Instance Method Details

#[](name) ⇒ Object



71
72
73
# File 'lib/theme_check/config.rb', line 71

def [](name)
  @configuration[name]
end

#check_configurationsObject



79
80
81
# File 'lib/theme_check/config.rb', line 79

def check_configurations
  @check_configurations ||= @configuration.select { |name, _| check_name?(name) }
end

#enabled_checksObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/theme_check/config.rb', line 83

def enabled_checks
  @enabled_checks ||= check_configurations.map do |check_name, options|
    next unless options["enabled"]

    check_class = ThemeCheck.const_get(check_name)

    next if check_class.categories.any? { |category| exclude_categories.include?(category) }
    next if only_categories.any? && check_class.categories.none? { |category| only_categories.include?(category) }

    options_for_check = options.transform_keys(&:to_sym)
    options_for_check.delete(:enabled)
    check = check_class.new(**options_for_check)
    check.options = options_for_check
    check
  end.compact
end

#ignored_patternsObject



100
101
102
# File 'lib/theme_check/config.rb', line 100

def ignored_patterns
  self["ignore"] || []
end

#to_hObject



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

def to_h
  @configuration
end