class Capsium::Package::Dataset
A loaded dataset. “source”/“schemaFile”/“databaseFile” are package-relative paths (ARCHITECTURE.md section 5), resolved against the package directory.
Attributes
data
[R]
untyped
name
[R]
String
package_path
[R]
String
Public Class Methods
(name: String name, config: DatasetConfig config, package_path: String package_path) → void
Source
# File lib/capsium/package/dataset.rb, line 22 def initialize(name:, config:, package_path:) @name = name @config = config @package_path = package_path @data = load_data end
Public Instance Methods
() → Hash[String, untyped]?
Source
# File lib/capsium/package/dataset.rb, line 72 def json_schema return nil unless @config.schema_file && @config.schema_type == "json-schema" return nil unless File.file?(schema_path) load_schema end
The parsed JSON schema for this dataset, or nil when none is declared or the file is unreadable.
() → untyped
Source
# File lib/capsium/package/dataset.rb, line 39 def load_data return nil unless File.file?(source_path) case @config.format when "yaml" then YAML.load_file(source_path) when "json" then JSON.parse(File.read(source_path)) when "csv" then CSV.read(source_path, headers: true) when "tsv" then CSV.read(source_path, col_sep: "\t", headers: true) when "sqlite" then load_sqlite_data else raise Error, "Unsupported data file type: #{@config.format}" end end
(untyped data) → Array[String]
Source
# File lib/capsium/package/dataset.rb, line 83 def schema_errors_for(data) return [] unless @config.schema_file && @config.schema_type == "json-schema" return [] unless File.file?(schema_path) JSON::Validator.fully_validate(load_schema, JSON.parse(JSON.generate(data))) end
Schema validation of an arbitrary candidate document (used by the reactor’s writable overlay before persisting a mutation): human-readable schema problems, empty when valid or when the dataset has no JSON schema.
() → String?
Source
# File lib/capsium/package/dataset.rb, line 33 def schema_path return unless @config.schema_file File.join(@package_path, @config.schema_file) end
() → String
Source
# File lib/capsium/package/dataset.rb, line 29 def source_path File.join(@package_path, @config.backing_file) end
() → bool
Source
# File lib/capsium/package/dataset.rb, line 53 def validate return true unless @config.schema_file && @config.schema_type == "json-schema" JSON::Validator.validate!(load_schema, @data.to_json) end
() → Array[String]
Source
# File lib/capsium/package/dataset.rb, line 61 def validation_errors problems = [] unless File.file?(source_path) problems << "dataset source missing on disk: #{@config.backing_file}" end problems.concat(schema_validation_errors) problems end
File-existence and schema validations for this dataset. Returns a list of human-readable problems; empty when valid.