Class: Goodcheck::Config

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

Constant Summary collapse

DEFAULT_EXCLUDE_BINARY =
false
BINARY_MIME_TYPES =
%w[
  audio
  font
  image
  model
  multipart
  video
].to_set.freeze
BINARY_MIME_FULLTYPES =
%w[
  application/gzip
  application/illustrator
  application/pdf
  application/zip
].to_set.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules:, exclude_paths:, exclude_binary: DEFAULT_EXCLUDE_BINARY, severity: nil) ⇒ Config

Returns a new instance of Config.



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

def initialize(rules:, exclude_paths:, exclude_binary: DEFAULT_EXCLUDE_BINARY, severity: nil)
  @rules = rules
  @exclude_paths = exclude_paths
  @exclude_binary = exclude_binary || DEFAULT_EXCLUDE_BINARY
  severity ||= {}
  @allowed_severities = Set.new(severity.fetch(:allow, []))
  @severity_required = severity.fetch(:required, false)
end

Instance Attribute Details

#allowed_severitiesObject (readonly)

Returns the value of attribute allowed_severities.



25
26
27
# File 'lib/goodcheck/config.rb', line 25

def allowed_severities
  @allowed_severities
end

#exclude_binaryObject (readonly) Also known as: exclude_binary?

Returns the value of attribute exclude_binary.



23
24
25
# File 'lib/goodcheck/config.rb', line 23

def exclude_binary
  @exclude_binary
end

#exclude_pathsObject (readonly)

Returns the value of attribute exclude_paths.



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

def exclude_paths
  @exclude_paths
end

#rulesObject (readonly)

Returns the value of attribute rules.



21
22
23
# File 'lib/goodcheck/config.rb', line 21

def rules
  @rules
end

#severity_requiredObject (readonly) Also known as: severity_required?

Returns the value of attribute severity_required.



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

def severity_required
  @severity_required
end

Instance Method Details

#each_rule(filter:, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/goodcheck/config.rb', line 43

def each_rule(filter:, &block)
  if block_given?
    if filter.empty?
      rules.each(&block)
    else
      rules.each do |rule|
        if filter.any? {|rule_id| rule.id == rule_id || rule.id.start_with?("#{rule_id}.") }
          yield rule
        end
      end
    end
  else
    enum_for :each_rule, filter: filter
  end
end

#exclude_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/goodcheck/config.rb', line 80

def exclude_path?(path)
  excluded = exclude_paths.any? do |pattern|
    path.fnmatch?(pattern, File::FNM_PATHNAME | File::FNM_EXTGLOB)
  end

  return true if excluded
  return excluded unless exclude_binary?
  return excluded unless path.file?

  exclude_file_by_mime_type?(path)
end

#rules_for_path(path, rules_filter:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/goodcheck/config.rb', line 59

def rules_for_path(path, rules_filter:)
  if block_given?
    each_rule(filter: rules_filter).map do |rule|
      rule.triggers.each do |trigger|
        globs = trigger.globs

        if globs.empty?
          yield [rule, nil, trigger]
        else
          glob = globs.find {|glob| glob.test(path) }
          if glob
            yield [rule, glob, trigger]
          end
        end
      end
    end
  else
    enum_for(:rules_for_path, path, rules_filter: rules_filter)
  end
end

#severity_allowed?(severity) ⇒ Boolean

Returns:

  • (Boolean)


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

def severity_allowed?(severity)
  return true if allowed_severities.empty?
  allowed_severities.include?(severity)
end