Class: Ibex::Configuration::Input
- Inherits:
-
Object
- Object
- Ibex::Configuration::Input
- Defined in:
- lib/ibex/configuration/explanation.rb,
sig/ibex/configuration/explanation.rbs
Overview
Canonical, static input facts used to build a configuration report.
Constant Summary collapse
- KINDS =
%i[grammar_source grammar_ir].freeze
Instance Attribute Summary collapse
- #evidence ⇒ Hash[String, Array[Evidence]] readonly
- #files ⇒ Array[String] readonly
- #grammar_locations ⇒ Hash[String, Location] readonly
- #grammar_values ⇒ Hash[String, config_value] readonly
- #kind ⇒ Symbol readonly
- #path ⇒ String readonly
- #recordings ⇒ Hash[String, Recording] readonly
- #schema_version ⇒ Integer? readonly
Instance Method Summary collapse
- #assign_input(kind, path, files, schema_version, grammar_values, grammar_locations, evidence, recordings) ⇒ void
- #immutable_values(values) ⇒ Hash[String, config_value]
-
#initialize(kind:, path:, files: [path], schema_version: nil, grammar_values: {}, grammar_locations: {}, evidence: {}, recordings: {}) ⇒ Input
constructor
A new instance of Input.
- #to_h ⇒ Hash[String, json_value]
- #validate_entries!(values, locations, evidence, recordings) ⇒ void
- #validate_evidence!(evidence) ⇒ void
- #validate_fact_hashes!(*values) ⇒ void
- #validate_files!(files, path) ⇒ void
- #validate_locations!(values, locations) ⇒ void
- #validate_recordings!(recordings) ⇒ void
- #validate_schema_version!(kind, schema_version) ⇒ void
Constructor Details
#initialize(kind:, path:, files: [path], schema_version: nil, grammar_values: {}, grammar_locations: {}, evidence: {}, recordings: {}) ⇒ Input
Returns a new instance of Input.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ibex/configuration/explanation.rb', line 98 def initialize(kind:, path:, files: [path], schema_version: nil, grammar_values: {}, grammar_locations: {}, evidence: {}, recordings: {}) raise ArgumentError, "unknown configuration input kind: #{kind.inspect}" unless KINDS.include?(kind) unless path.is_a?(String) && !path.empty? raise ArgumentError, "configuration input path must be a non-empty String" end validate_files!(files, path) validate_schema_version!(kind, schema_version) validate_entries!(grammar_values, grammar_locations, evidence, recordings) assign_input( kind, path, files, schema_version, grammar_values, grammar_locations, evidence, recordings ) freeze end |
Instance Attribute Details
#evidence ⇒ Hash[String, Array[Evidence]] (readonly)
92 93 94 |
# File 'lib/ibex/configuration/explanation.rb', line 92 def evidence @evidence end |
#files ⇒ Array[String] (readonly)
88 89 90 |
# File 'lib/ibex/configuration/explanation.rb', line 88 def files @files end |
#grammar_locations ⇒ Hash[String, Location] (readonly)
91 92 93 |
# File 'lib/ibex/configuration/explanation.rb', line 91 def grammar_locations @grammar_locations end |
#grammar_values ⇒ Hash[String, config_value] (readonly)
90 91 92 |
# File 'lib/ibex/configuration/explanation.rb', line 90 def grammar_values @grammar_values end |
#kind ⇒ Symbol (readonly)
86 87 88 |
# File 'lib/ibex/configuration/explanation.rb', line 86 def kind @kind end |
#path ⇒ String (readonly)
87 88 89 |
# File 'lib/ibex/configuration/explanation.rb', line 87 def path @path end |
#recordings ⇒ Hash[String, Recording] (readonly)
93 94 95 |
# File 'lib/ibex/configuration/explanation.rb', line 93 def recordings @recordings end |
#schema_version ⇒ Integer? (readonly)
89 90 91 |
# File 'lib/ibex/configuration/explanation.rb', line 89 def schema_version @schema_version end |
Instance Method Details
#assign_input(kind, path, files, schema_version, grammar_values, grammar_locations, evidence, recordings) ⇒ void
This method returns an undefined value.
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ibex/configuration/explanation.rb', line 130 def assign_input(kind, path, files, schema_version, grammar_values, grammar_locations, evidence, recordings) @kind = kind @path = path.dup.freeze @files = files.map { |file| file.dup.freeze }.freeze @schema_version = schema_version @grammar_values = immutable_values(grammar_values) @grammar_locations = grammar_locations.to_h { |key, location| [key.dup.freeze, location] }.freeze @evidence = evidence.to_h { |key, entries| [key.dup.freeze, entries.dup.freeze] }.freeze @recordings = recordings.to_h { |key, recording| [key.dup.freeze, recording] }.freeze end |
#immutable_values(values) ⇒ Hash[String, config_value]
209 210 211 212 213 |
# File 'lib/ibex/configuration/explanation.rb', line 209 def immutable_values(values) values.to_h do |name, value| [name.dup.freeze, Registry.fetch(name).validate(value)] end.freeze end |
#to_h ⇒ Hash[String, json_value]
115 116 117 118 119 120 121 122 123 |
# File 'lib/ibex/configuration/explanation.rb', line 115 def to_h result = { "kind" => @kind.to_s.tr("_", "-"), "path" => @path, "files" => @files } #: Hash[String, json_value] result["schema_version"] = @schema_version if @schema_version result end |
#validate_entries!(values, locations, evidence, recordings) ⇒ void
This method returns an undefined value.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ibex/configuration/explanation.rb', line 161 def validate_entries!(values, locations, evidence, recordings) validate_fact_hashes!(values, locations, evidence, recordings) names = values.keys | locations.keys | evidence.keys | recordings.keys names.each do |name| raise ArgumentError, "configuration input keys must be Strings" unless name.is_a?(String) Registry.fetch(name) end validate_locations!(values, locations) validate_evidence!(evidence) validate_recordings!(recordings) end |
#validate_evidence!(evidence) ⇒ void
This method returns an undefined value.
190 191 192 193 194 195 196 197 |
# File 'lib/ibex/configuration/explanation.rb', line 190 def validate_evidence!(evidence) evidence.each do |name, entries| key = Registry.fetch(name) unless entries.is_a?(Array) && entries.all? { |entry| entry.is_a?(Evidence) && entry.key.equal?(key) } raise ArgumentError, "configuration evidence for #{name} has the wrong key" end end end |
#validate_fact_hashes!(*values) ⇒ void
This method returns an undefined value.
175 176 177 178 179 |
# File 'lib/ibex/configuration/explanation.rb', line 175 def validate_fact_hashes!(*values) return if values.all?(Hash) raise ArgumentError, "configuration input facts must be Hash values" end |
#validate_files!(files, path) ⇒ void
This method returns an undefined value.
142 143 144 145 146 147 148 |
# File 'lib/ibex/configuration/explanation.rb', line 142 def validate_files!(files, path) unless files.is_a?(Array) && !files.empty? && files.all? { |file| file.is_a?(String) && !file.empty? } raise ArgumentError, "configuration input files must be non-empty Strings" end raise ArgumentError, "configuration input files must be unique" unless files.uniq.length == files.length raise ArgumentError, "configuration input path must be the first file" unless files.first == path end |
#validate_locations!(values, locations) ⇒ void
This method returns an undefined value.
182 183 184 185 186 187 |
# File 'lib/ibex/configuration/explanation.rb', line 182 def validate_locations!(values, locations) locations.each do |name, location| raise ArgumentError, "configuration location for #{name} has no grammar value" unless values.key?(name) raise ArgumentError, "configuration location must be an Ibex::Location" unless location.is_a?(Location) end end |
#validate_recordings!(recordings) ⇒ void
This method returns an undefined value.
200 201 202 203 204 205 206 |
# File 'lib/ibex/configuration/explanation.rb', line 200 def validate_recordings!(recordings) recordings.each do |name, recording| next if recording.is_a?(Recording) raise ArgumentError, "configuration recording for #{name} must be a Recording" end end |
#validate_schema_version!(kind, schema_version) ⇒ void
This method returns an undefined value.
151 152 153 154 155 156 157 |
# File 'lib/ibex/configuration/explanation.rb', line 151 def validate_schema_version!(kind, schema_version) if kind == :grammar_source raise ArgumentError, "grammar source input cannot have a schema version" unless schema_version.nil? elsif !schema_version.is_a?(Integer) || !schema_version.positive? raise ArgumentError, "Grammar IR input requires a positive schema version" end end |