class Goodcheck::Config

Constants

BINARY_MIME_FULLTYPES
BINARY_MIME_TYPES

www.iana.org/assignments/media-types/media-types.xhtml

DEFAULT_EXCLUDE_BINARY

Attributes

allowed_severities[R]
exclude_binary[R]
exclude_binary?[R]
exclude_paths[R]
rules[R]
severity_required[R]
severity_required?[R]

Public Class Methods

new(rules:, exclude_paths:, exclude_binary: DEFAULT_EXCLUDE_BINARY, severity: nil) click to toggle source
# 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

Public Instance Methods

each_rule(filter:) { |rule| ... } click to toggle source
# 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) click to toggle source
# 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:) { |rule, nil, trigger| ... } click to toggle source
# 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) click to toggle source
# File lib/goodcheck/config.rb, line 38
def severity_allowed?(severity)
  return true if allowed_severities.empty?
  allowed_severities.include?(severity)
end

Private Instance Methods

exclude_file_by_mime_type?(file) click to toggle source
# File lib/goodcheck/config.rb, line 94
def exclude_file_by_mime_type?(file)
  # NOTE: Lazy load to save memory
  require "marcel"

  fulltype = Marcel::MimeType.for(file)
  type, subtype = fulltype.split("/")

  case
  when subtype.end_with?("+xml") # e.g. "image/svg+xml"
    false
  when BINARY_MIME_TYPES.include?(type)
    Goodcheck.logger.debug "Exclude file: #{file} (#{fulltype})"
    true
  when BINARY_MIME_FULLTYPES.include?(fulltype)
    Goodcheck.logger.debug "Exclude file: #{file} (#{fulltype})"
    true
  else
    false
  end
end