Class: RailVerdict::Configuration

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

Constant Summary collapse

DEFAULT_FILENAME =
".railverdict.yml"
CONFIGURATION_VERSION =
1.4
SUPPORTED_VERSIONS =
[1, 1.1, 1.2, 1.3, 1.4].freeze
UTF8_BOM =
"\xEF\xBB\xBF".b

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, source_path, bytes) ⇒ Configuration

Returns a new instance of Configuration.



78
79
80
81
82
83
84
85
86
# File 'lib/rail_verdict/configuration.rb', line 78

def initialize(data, source_path, bytes)
  @version = data.fetch("version")
  @mode = data.fetch("mode").freeze
  @analyzers = deep_freeze(data.fetch("analyzers"))
  @raw_data = deep_freeze(data.dup)
  @source_path = source_path.dup.freeze
  @digest = Digest::SHA256.hexdigest(bytes).freeze
  freeze
end

Instance Attribute Details

#analyzersObject (readonly)

Returns the value of attribute analyzers.



12
13
14
# File 'lib/rail_verdict/configuration.rb', line 12

def analyzers
  @analyzers
end

#digestObject (readonly)

Returns the value of attribute digest.



12
13
14
# File 'lib/rail_verdict/configuration.rb', line 12

def digest
  @digest
end

#modeObject (readonly)

Returns the value of attribute mode.



12
13
14
# File 'lib/rail_verdict/configuration.rb', line 12

def mode
  @mode
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



12
13
14
# File 'lib/rail_verdict/configuration.rb', line 12

def source_path
  @source_path
end

#versionObject (readonly)

Returns the value of attribute version.



12
13
14
# File 'lib/rail_verdict/configuration.rb', line 12

def version
  @version
end

Class Method Details

.decode_utf8(bytes, source_path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rail_verdict/configuration.rb', line 30

def self.decode_utf8(bytes, source_path)
  if bytes.start_with?(UTF8_BOM)
    raise ConfigurationError.new(
      "#{source_path}: configuration must be UTF-8 without a byte order mark",
      source_path: source_path
    )
  end

  text = bytes.dup.force_encoding(Encoding::UTF_8)
  unless text.valid_encoding?
    raise ConfigurationError.new(
      "#{source_path}: configuration must be valid UTF-8",
      source_path: source_path
    )
  end

  text
end

.load(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rail_verdict/configuration.rb', line 14

def self.load(path)
  source_path = path.to_s
  unless File.file?(source_path)
    raise ConfigurationError.new(
      "configuration file not found: #{source_path}",
      source_path: source_path
    )
  end

  bytes = File.binread(source_path)
  text = decode_utf8(bytes, source_path)
  data = load_strict_yaml(text, source_path)
  validate_schema(data, source_path)
  new(data, source_path, bytes)
end

.load_strict_yaml(text, source_path) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/rail_verdict/configuration.rb', line 49

def self.load_strict_yaml(text, source_path)
  StrictYaml.parse(text, source_path)
rescue StrictYaml::Error => error
  raise ConfigurationError.new(
    "#{error.message}",
    source_path: source_path,
    property_path: error.property_path
  )
end

.validate_schema(data, source_path) ⇒ Object

Raises:



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

def self.validate_schema(data, source_path)
  unless data.is_a?(Hash)
    raise ConfigurationError.new(
      "#{source_path}: $: configuration must be a mapping",
      source_path: source_path,
      property_path: "$"
    )
  end

  errors = SchemaValidator.validate_configuration(data)
  return if errors.empty?

  raise ConfigurationError.new(
    "invalid configuration #{source_path}: #{errors.join('; ')}",
    source_path: source_path,
    property_path: errors.first.to_s.split(":").first
  )
end

Instance Method Details

#ai_budgetsObject



139
140
141
# File 'lib/rail_verdict/configuration.rb', line 139

def ai_budgets
  ai_config&.fetch("budgets", {}) || {}
end

#ai_cache_enabled?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/rail_verdict/configuration.rb', line 143

def ai_cache_enabled?
  ai_config&.dig("cache", "enabled") == true
end

#ai_configObject



119
120
121
# File 'lib/rail_verdict/configuration.rb', line 119

def ai_config
  @raw_data&.dig("ai")
end

#ai_enabled?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/rail_verdict/configuration.rb', line 123

def ai_enabled?
  ai_config&.fetch("enabled", false) == true
end

#ai_modeObject



127
128
129
# File 'lib/rail_verdict/configuration.rb', line 127

def ai_mode
  ai_config&.fetch("mode", "off")
end

#ai_remote_enabled?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/rail_verdict/configuration.rb', line 131

def ai_remote_enabled?
  ai_enabled? && ai_config&.dig("remote", "enabled") == true
end

#ai_remote_trustObject



135
136
137
# File 'lib/rail_verdict/configuration.rb', line 135

def ai_remote_trust
  ai_config&.dig("remote", "trust") || "redacted"
end

#analyzer_enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
# File 'lib/rail_verdict/configuration.rb', line 92

def analyzer_enabled?(name)
  selection = @analyzers[name]
  return false if selection.nil?

  selection.fetch("enabled")
end

#analyzer_present?(name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rail_verdict/configuration.rb', line 103

def analyzer_present?(name)
  @analyzers.key?(name)
end

#analyzer_required?(name) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rail_verdict/configuration.rb', line 99

def analyzer_required?(name)
  @analyzers.fetch(name).fetch("required")
end

#analyzer_selection(name) ⇒ Object



88
89
90
# File 'lib/rail_verdict/configuration.rb', line 88

def analyzer_selection(name)
  @analyzers.fetch(name)
end

#baseline_pathObject



107
108
109
# File 'lib/rail_verdict/configuration.rb', line 107

def baseline_path
  @raw_data&.dig("baseline", "path")
end

#git_baseObject



115
116
117
# File 'lib/rail_verdict/configuration.rb', line 115

def git_base
  @raw_data&.dig("git", "base")
end

#waivers_pathObject



111
112
113
# File 'lib/rail_verdict/configuration.rb', line 111

def waivers_path
  @raw_data&.dig("waivers", "path")
end