Class: RailVerdict::Configuration
- Inherits:
-
Object
- Object
- RailVerdict::Configuration
- Defined in:
- lib/rail_verdict/configuration.rb
Constant Summary collapse
- DEFAULT_FILENAME =
".railverdict.yml"- CONFIGURATION_VERSION =
1.5- SUPPORTED_VERSIONS =
[1, 1.1, 1.2, 1.3, 1.4, 1.5].freeze
- DEFAULT_ANALYZER_TIMEOUT_SECONDS =
30- UTF8_BOM =
"\xEF\xBB\xBF".b
Instance Attribute Summary collapse
-
#analyzers ⇒ Object
readonly
Returns the value of attribute analyzers.
-
#digest ⇒ Object
readonly
Returns the value of attribute digest.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
- .decode_utf8(bytes, source_path) ⇒ Object
- .load(path) ⇒ Object
- .load_strict_yaml(text, source_path) ⇒ Object
- .validate_schema(data, source_path) ⇒ Object
Instance Method Summary collapse
- #ai_budgets ⇒ Object
- #ai_cache_enabled? ⇒ Boolean
- #ai_config ⇒ Object
- #ai_enabled? ⇒ Boolean
- #ai_mode ⇒ Object
- #ai_remote_enabled? ⇒ Boolean
- #ai_remote_trust ⇒ Object
- #analyzer_enabled?(name) ⇒ Boolean
- #analyzer_present?(name) ⇒ Boolean
- #analyzer_required?(name) ⇒ Boolean
- #analyzer_selection(name) ⇒ Object
- #analyzer_timeout_seconds(name) ⇒ Object
- #baseline_path ⇒ Object
- #git_base ⇒ Object
-
#initialize(data, source_path, bytes) ⇒ Configuration
constructor
A new instance of Configuration.
- #waivers_path ⇒ Object
Constructor Details
#initialize(data, source_path, bytes) ⇒ Configuration
Returns a new instance of Configuration.
79 80 81 82 83 84 85 86 87 |
# File 'lib/rail_verdict/configuration.rb', line 79 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
#analyzers ⇒ Object (readonly)
Returns the value of attribute analyzers.
13 14 15 |
# File 'lib/rail_verdict/configuration.rb', line 13 def analyzers @analyzers end |
#digest ⇒ Object (readonly)
Returns the value of attribute digest.
13 14 15 |
# File 'lib/rail_verdict/configuration.rb', line 13 def digest @digest end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
13 14 15 |
# File 'lib/rail_verdict/configuration.rb', line 13 def mode @mode end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
13 14 15 |
# File 'lib/rail_verdict/configuration.rb', line 13 def source_path @source_path end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
13 14 15 |
# File 'lib/rail_verdict/configuration.rb', line 13 def version @version end |
Class Method Details
.decode_utf8(bytes, source_path) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rail_verdict/configuration.rb', line 31 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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rail_verdict/configuration.rb', line 15 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
50 51 52 53 54 55 56 57 58 |
# File 'lib/rail_verdict/configuration.rb', line 50 def self.load_strict_yaml(text, source_path) StrictYaml.parse(text, source_path) rescue StrictYaml::Error => error raise ConfigurationError.new( "#{error.}", source_path: source_path, property_path: error.property_path ) end |
.validate_schema(data, source_path) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rail_verdict/configuration.rb', line 60 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_budgets ⇒ Object
150 151 152 |
# File 'lib/rail_verdict/configuration.rb', line 150 def ai_budgets ai_config&.fetch("budgets", {}) || {} end |
#ai_cache_enabled? ⇒ Boolean
154 155 156 |
# File 'lib/rail_verdict/configuration.rb', line 154 def ai_cache_enabled? ai_config&.dig("cache", "enabled") == true end |
#ai_config ⇒ Object
130 131 132 |
# File 'lib/rail_verdict/configuration.rb', line 130 def ai_config @raw_data&.dig("ai") end |
#ai_enabled? ⇒ Boolean
134 135 136 |
# File 'lib/rail_verdict/configuration.rb', line 134 def ai_enabled? ai_config&.fetch("enabled", false) == true end |
#ai_mode ⇒ Object
138 139 140 |
# File 'lib/rail_verdict/configuration.rb', line 138 def ai_mode ai_config&.fetch("mode", "off") end |
#ai_remote_enabled? ⇒ Boolean
142 143 144 |
# File 'lib/rail_verdict/configuration.rb', line 142 def ai_remote_enabled? ai_enabled? && ai_config&.dig("remote", "enabled") == true end |
#ai_remote_trust ⇒ Object
146 147 148 |
# File 'lib/rail_verdict/configuration.rb', line 146 def ai_remote_trust ai_config&.dig("remote", "trust") || "redacted" end |
#analyzer_enabled?(name) ⇒ Boolean
93 94 95 96 97 98 |
# File 'lib/rail_verdict/configuration.rb', line 93 def analyzer_enabled?(name) selection = @analyzers[name] return false if selection.nil? selection.fetch("enabled") end |
#analyzer_present?(name) ⇒ Boolean
104 105 106 |
# File 'lib/rail_verdict/configuration.rb', line 104 def analyzer_present?(name) @analyzers.key?(name) end |
#analyzer_required?(name) ⇒ Boolean
100 101 102 |
# File 'lib/rail_verdict/configuration.rb', line 100 def analyzer_required?(name) @analyzers.fetch(name).fetch("required") end |
#analyzer_selection(name) ⇒ Object
89 90 91 |
# File 'lib/rail_verdict/configuration.rb', line 89 def analyzer_selection(name) @analyzers.fetch(name) end |
#analyzer_timeout_seconds(name) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/rail_verdict/configuration.rb', line 116 def analyzer_timeout_seconds(name) selection = @analyzers[name.to_s] return DEFAULT_ANALYZER_TIMEOUT_SECONDS unless selection value = selection["timeout_seconds"] return DEFAULT_ANALYZER_TIMEOUT_SECONDS if value.nil? Integer(value) end |
#baseline_path ⇒ Object
108 109 110 |
# File 'lib/rail_verdict/configuration.rb', line 108 def baseline_path @raw_data&.dig("baseline", "path") end |
#git_base ⇒ Object
126 127 128 |
# File 'lib/rail_verdict/configuration.rb', line 126 def git_base @raw_data&.dig("git", "base") end |
#waivers_path ⇒ Object
112 113 114 |
# File 'lib/rail_verdict/configuration.rb', line 112 def waivers_path @raw_data&.dig("waivers", "path") end |