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]

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.



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

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

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



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

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

.find(root, needle = DOTFILE) ⇒ Object



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

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



26
27
28
# File 'lib/theme_check/config.rb', line 26

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

.from_path(path) ⇒ Object



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

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



22
23
24
# File 'lib/theme_check/config.rb', line 22

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

.load_file(absolute_path) ⇒ Object



38
39
40
# File 'lib/theme_check/config.rb', line 38

def load_file(absolute_path)
  YAML.load_file(absolute_path)
end

Instance Method Details

#[](name) ⇒ Object



68
69
70
# File 'lib/theme_check/config.rb', line 68

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

#check_configurationsObject



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

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

#enabled_checksObject



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

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 exclude_categories.include?(check_class.category)
    next if only_categories.any? && !only_categories.include?(check_class.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



97
98
99
# File 'lib/theme_check/config.rb', line 97

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

#to_hObject



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

def to_h
  @configuration
end