Class: Capsium::Package::Storage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capsium/package/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Storage

Returns a new instance of Storage.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capsium/package/storage.rb', line 19

def initialize(path)
  @path = path
  @dir = File.dirname(path)
  @datasets_path = File.join(@dir, DATA_DIR)
  @config = if File.exist?(path)
              StorageConfig.from_json(File.read(path))
            else
              StorageConfig.new(datasets: generate_datasets)
            end
  @datasets = load_datasets || generate_datasets
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/capsium/package/storage.rb', line 13

def config
  @config
end

#datasetsObject (readonly)

Returns the value of attribute datasets.



17
18
19
# File 'lib/capsium/package/storage.rb', line 17

def datasets
  @datasets
end

Instance Method Details

#generate_datasetsObject



47
48
49
50
51
52
53
54
55
# File 'lib/capsium/package/storage.rb', line 47

def generate_datasets
  paths = File.join(@datasets_path, "*.{yaml,yml,json,csv,tsv,sqlite,db}")
  Dir.glob(paths).map do |file_path|
    Dataset.new(config: DatasetConfig.new(
      name: File.basename(file_path,
                          ".*"), source: file_path, format: detect_format(file_path)
    ))
  end
end

#load_datasetsObject



31
32
33
34
35
36
37
38
# File 'lib/capsium/package/storage.rb', line 31

def load_datasets
  if File.exist?(@path)
    storage_data = StorageConfig.from_json(File.read(@path))
    storage_data.datasets.map do |dataset_config|
      dataset_config.to_dataset(@datasets_path)
    end
  end
end

#save_to_file(output_path = @path) ⇒ Object



40
41
42
43
44
45
# File 'lib/capsium/package/storage.rb', line 40

def save_to_file(output_path = @path)
  storage_config = StorageConfig.new(datasets: @datasets.map do |dataset|
                                                 DatasetConfig.from_dataset(dataset)
                                               end)
  File.write(output_path, storage_config.to_json)
end