Class: FiberAudit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_audit/configuration.rb

Constant Summary collapse

KNOWN_TOP_LEVEL_KEYS =
%w[static rules report].freeze
KNOWN_STATIC_KEYS =
%w[include exclude suppressions_path].freeze
KNOWN_REPORT_KEYS =
%w[formats min_severity].freeze
KNOWN_RULE_KEYS =
%w[enabled severity].freeze
VALID_FORMATS =
%w[text json].freeze
DEFAULT_STATIC_INCLUDE =
%w[
  app/**/*.rb
  lib/**/*.rb
  config/**/*.rb
  config/initializers/**/*.rb
].freeze
DEFAULT_STATIC_EXCLUDE =
%w[
  vendor/**/*
  tmp/**/*
  node_modules/**/*
  db/schema.rb
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(static_include: DEFAULT_STATIC_INCLUDE, static_exclude: DEFAULT_STATIC_EXCLUDE, rules_config: {}, report_formats: %w[text],, min_severity: :low, suppressions_path: nil) ⇒ Configuration

Returns a new instance of Configuration.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fiber_audit/configuration.rb', line 33

def initialize(
  static_include: DEFAULT_STATIC_INCLUDE,
  static_exclude: DEFAULT_STATIC_EXCLUDE,
  rules_config: {},
  report_formats: %w[text],
  min_severity: :low,
  suppressions_path: nil
)
  validate_types!(
    static_include, static_exclude, rules_config,
    report_formats, min_severity, suppressions_path
  )

  @static_include = static_include
  @static_exclude = static_exclude
  @rules_config = rules_config
  @report_formats = report_formats
  @min_severity = coerce_severity(min_severity, 'report.min_severity')
  @suppressions_path = suppressions_path
end

Instance Attribute Details

#min_severityObject (readonly)

Returns the value of attribute min_severity.



30
31
32
# File 'lib/fiber_audit/configuration.rb', line 30

def min_severity
  @min_severity
end

#report_formatsObject (readonly)

Returns the value of attribute report_formats.



30
31
32
# File 'lib/fiber_audit/configuration.rb', line 30

def report_formats
  @report_formats
end

#rules_configObject (readonly)

Returns the value of attribute rules_config.



30
31
32
# File 'lib/fiber_audit/configuration.rb', line 30

def rules_config
  @rules_config
end

#static_excludeObject (readonly)

Returns the value of attribute static_exclude.



30
31
32
# File 'lib/fiber_audit/configuration.rb', line 30

def static_exclude
  @static_exclude
end

#static_includeObject (readonly)

Returns the value of attribute static_include.



30
31
32
# File 'lib/fiber_audit/configuration.rb', line 30

def static_include
  @static_include
end

#suppressions_pathObject (readonly)

Returns the value of attribute suppressions_path.



30
31
32
# File 'lib/fiber_audit/configuration.rb', line 30

def suppressions_path
  @suppressions_path
end

Class Method Details

.load(path = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fiber_audit/configuration.rb', line 70

def load(path = nil)
  return new unless path && File.exist?(path)

  yaml = YAML.safe_load_file(path) || {}
  validate_yaml_structure!(yaml)

  static = yaml['static'] || {}
  rules = yaml['rules'] || {}
  report = yaml['report'] || {}

  new(
    static_include: static['include'] || DEFAULT_STATIC_INCLUDE,
    static_exclude: static['exclude'] || DEFAULT_STATIC_EXCLUDE,
    rules_config: rules,
    report_formats: report['formats'] || %w[text],
    min_severity: report.fetch('min_severity', :low),
    suppressions_path: static['suppressions_path']
  )
end

Instance Method Details

#rule_enabled?(rule_id) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/fiber_audit/configuration.rb', line 54

def rule_enabled?(rule_id)
  entry = @rules_config[rule_id] || {}
  entry.fetch('enabled', true)
end

#severity_override(rule_id) ⇒ Object

Returns the overridden severity for a rule as a validated Symbol, or nil when no override is configured.



61
62
63
64
65
66
67
# File 'lib/fiber_audit/configuration.rb', line 61

def severity_override(rule_id)
  entry = @rules_config[rule_id] || {}
  sev = entry['severity']
  return nil unless sev

  coerce_severity(sev, "rules.#{rule_id}.severity")
end