Class: Capsium::Package::Dataset
- Inherits:
-
Object
- Object
- Capsium::Package::Dataset
- Extended by:
- Forwardable
- Defined in:
- lib/capsium/package/dataset.rb,
sig/capsium/package/dataset.rbs
Overview
A loaded dataset. "source"/"schemaFile"/"databaseFile" are package-relative paths resolved against the package directory.
Instance Attribute Summary collapse
-
#config ⇒ DatasetConfig
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#name ⇒ String
readonly
Returns the value of attribute name.
-
#package_path ⇒ String
readonly
Returns the value of attribute package_path.
Instance Method Summary collapse
-
#initialize(name:, config:, package_path:) ⇒ Dataset
constructor
A new instance of Dataset.
- #load_data ⇒ Object
- #schema_path ⇒ String?
- #source_path ⇒ String
- #to_json ⇒ String
- #validate ⇒ Boolean
-
#validation_errors ⇒ Array[String]
File-existence and schema validations; empty when valid.
Constructor Details
#initialize(name:, config:, package_path:) ⇒ Dataset
Returns a new instance of Dataset.
22 23 24 25 26 27 |
# 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 |
Instance Attribute Details
#config ⇒ DatasetConfig (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/capsium/package/dataset.rb', line 16 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
16 17 18 |
# File 'lib/capsium/package/dataset.rb', line 16 def data @data end |
#name ⇒ String (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/capsium/package/dataset.rb', line 16 def name @name end |
#package_path ⇒ String (readonly)
Returns the value of attribute package_path.
16 17 18 |
# File 'lib/capsium/package/dataset.rb', line 16 def package_path @package_path end |
Instance Method Details
#load_data ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 |
#schema_path ⇒ String?
33 34 35 36 37 |
# File 'lib/capsium/package/dataset.rb', line 33 def schema_path return unless @config.schema_file File.join(@package_path, @config.schema_file) end |
#source_path ⇒ String
29 30 31 |
# File 'lib/capsium/package/dataset.rb', line 29 def source_path File.join(@package_path, @config.backing_file) end |
#to_json ⇒ String
25 |
# File 'sig/capsium/package/dataset.rbs', line 25
def to_json: (*untyped args) -> String
|
#validate ⇒ Boolean
53 54 55 56 57 |
# 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 |
#validation_errors ⇒ Array[String]
File-existence and schema validations; empty when valid.
61 62 63 64 65 66 67 68 |
# 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 |