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"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, configuration = nil) ⇒ Config

Returns a new instance of Config.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/theme_check/config.rb', line 34

def initialize(root, configuration = nil)
  @configuration = configuration || {}
  @checks = @configuration.dup
  @root = Pathname.new(root)
  if @checks.key?("root")
    @root = @root.join(@checks.delete("root"))
  end
  @only_categories = []
  @exclude_categories = []
  @auto_correct = false
  resolve_requires
end

Instance Attribute Details

#auto_correctObject

Returns the value of attribute auto_correct.



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

def auto_correct
  @auto_correct
end

#exclude_categoriesObject

Returns the value of attribute exclude_categories.



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

def exclude_categories
  @exclude_categories
end

#only_categoriesObject

Returns the value of attribute only_categories.



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

def only_categories
  @only_categories
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Class Method Details

.find(root, needle = DOTFILE) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/theme_check/config.rb', line 21

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_path(path) ⇒ Object



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

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

.load_file(absolute_path) ⇒ Object



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

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

Instance Method Details

#enabled_checksObject



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

def enabled_checks
  checks = []

  default_configuration.merge(@checks).each do |check_name, properties|
    if @checks[check_name] && !default_configuration[check_name].nil?
      valid_properties = valid_check_configuration(check_name)
      properties = properties.merge(valid_properties)
    end

    next if properties.delete('enabled') == false

    options = properties.transform_keys(&:to_sym)
    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)

    check = check_class.new(**options)
    check.options = options
    checks << check
  end

  checks
end

#to_hObject



47
48
49
# File 'lib/theme_check/config.rb', line 47

def to_h
  @configuration
end