Class: Capsium::Package::Dataset

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

Instance Method Summary collapse

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

#configDatasetConfig (readonly)

Returns the value of attribute config.

Returns:



16
17
18
# File 'lib/capsium/package/dataset.rb', line 16

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.

Returns:

  • (Object)


16
17
18
# File 'lib/capsium/package/dataset.rb', line 16

def data
  @data
end

#nameString (readonly)

Returns the value of attribute name.

Returns:

  • (String)


16
17
18
# File 'lib/capsium/package/dataset.rb', line 16

def name
  @name
end

#package_pathString (readonly)

Returns the value of attribute package_path.

Returns:

  • (String)


16
17
18
# File 'lib/capsium/package/dataset.rb', line 16

def package_path
  @package_path
end

Instance Method Details

#load_dataObject

Returns:

  • (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_pathString?

Returns:

  • (String, nil)


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_pathString

Returns:

  • (String)


29
30
31
# File 'lib/capsium/package/dataset.rb', line 29

def source_path
  File.join(@package_path, @config.backing_file)
end

#to_jsonString

Parameters:

  • args (Object)

Returns:

  • (String)


25
# File 'sig/capsium/package/dataset.rbs', line 25

def to_json: (*untyped args) -> String

#validateBoolean

Returns:

  • (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_errorsArray[String]

File-existence and schema validations; empty when valid.

Returns:

  • (Array[String])


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