Class: Ibex::Configuration::Input

Inherits:
Object
  • Object
show all
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 =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
%i[grammar_source grammar_ir].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, path:, files: [path], schema_version: nil, grammar_values: {}, grammar_locations: {}, evidence: {}, recordings: {}) ⇒ Input

Returns a new instance of Input.

RBS:

  • (kind: Symbol, path: String, ?files: Array[String], ?schema_version: Integer?, ?grammar_values: Hash[String, config_value], ?grammar_locations: Hash[String, Location], ?evidence: Hash[String, Array[Evidence]], ?recordings: Hash[String, Recording]) -> void

Parameters:

  • kind: (Symbol)
  • path: (String)
  • files: (Array[String]) (defaults to: [path])
  • schema_version: (Integer, nil) (defaults to: nil)
  • grammar_values: (Hash[String, config_value]) (defaults to: {})
  • grammar_locations: (Hash[String, Location]) (defaults to: {})
  • evidence: (Hash[String, Array[Evidence]]) (defaults to: {})
  • recordings: (Hash[String, Recording]) (defaults to: {})


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

#evidenceHash[String, Array[Evidence]] (readonly)

Signature:

  • Hash[String, Array[Evidence]]

Returns:



92
93
94
# File 'lib/ibex/configuration/explanation.rb', line 92

def evidence
  @evidence
end

#filesArray[String] (readonly)

Signature:

  • Array[String]

Returns:

  • (Array[String])


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

def files
  @files
end

#grammar_locationsHash[String, Location] (readonly)

Signature:

  • Hash[String, Location]

Returns:



91
92
93
# File 'lib/ibex/configuration/explanation.rb', line 91

def grammar_locations
  @grammar_locations
end

#grammar_valuesHash[String, config_value] (readonly)

Signature:

  • Hash[String, config_value]

Returns:

  • (Hash[String, config_value])


90
91
92
# File 'lib/ibex/configuration/explanation.rb', line 90

def grammar_values
  @grammar_values
end

#kindSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


86
87
88
# File 'lib/ibex/configuration/explanation.rb', line 86

def kind
  @kind
end

#pathString (readonly)

Signature:

  • String

Returns:

  • (String)


87
88
89
# File 'lib/ibex/configuration/explanation.rb', line 87

def path
  @path
end

#recordingsHash[String, Recording] (readonly)

Signature:

  • Hash[String, Recording]

Returns:



93
94
95
# File 'lib/ibex/configuration/explanation.rb', line 93

def recordings
  @recordings
end

#schema_versionInteger? (readonly)

Signature:

  • Integer?

Returns:

  • (Integer, nil)


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.

RBS:

  • (Symbol kind, String path, Array[String] files, Integer? schema_version, Hash[String, config_value] grammar_values, Hash[String, Location] grammar_locations, Hash[String, Array[Evidence]] evidence, Hash[String, Recording] recordings) -> void

Parameters:

  • kind (Symbol)
  • path (String)
  • files (Array[String])
  • schema_version (Integer, nil)
  • grammar_values (Hash[String, config_value])
  • grammar_locations (Hash[String, Location])
  • evidence (Hash[String, Array[Evidence]])
  • recordings (Hash[String, Recording])


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]

RBS:

  • (Hash[String, config_value] values) -> Hash[String, config_value]

Parameters:

  • values (Hash[String, config_value])

Returns:

  • (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_hHash[String, json_value]

RBS:

  • () -> Hash[String, json_value]

Returns:

  • (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.

RBS:

  • (Hash[String, config_value] values, Hash[String, Location] locations, Hash[String, Array[Evidence]] evidence, Hash[String, Recording] recordings) -> void

Parameters:

  • values (Hash[String, config_value])
  • locations (Hash[String, Location])
  • evidence (Hash[String, Array[Evidence]])
  • recordings (Hash[String, Recording])


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.

RBS:

  • (Hash[String, Array[Evidence]] evidence) -> void

Parameters:

  • evidence (Hash[String, Array[Evidence]])


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.

RBS:

  • (*Hash[Object?, Object?] values) -> void

Parameters:

  • values (Hash[Object?, Object?])


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.

RBS:

  • (Array[String] files, String path) -> void

Parameters:

  • files (Array[String])
  • path (String)


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.

RBS:

  • (Hash[String, config_value] values, Hash[String, Location] locations) -> void

Parameters:

  • values (Hash[String, config_value])
  • locations (Hash[String, Location])


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.

RBS:

  • (Hash[String, Recording] recordings) -> void

Parameters:



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.

RBS:

  • (Symbol kind, Integer? schema_version) -> void

Parameters:

  • kind (Symbol)
  • schema_version (Integer, nil)


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