Class: Opencdd::Parcel::FlatDirReader

Inherits:
Object
  • Object
show all
Defined in:
lib/opencdd/parcel/flat_dir_reader.rb

Overview

Reads the legacy "flat directory" Parcel layout: one directory containing 1-6 +export_(CLASS|PROPERTY|RELATION|UNIT|VALUELIST| VALUETERMS)_*.xls+ files at the top level. This is the format cdd.iec.ch's "EXCEL format" export produces when downloaded per-type without sharding by class.

For per-class sharded subdirs see Opencdd::Parcel::ShardedDirReader. For single .xlsx workbooks see Opencdd::Parcel::WorkbookReader.

Constant Summary collapse

FILE_PATTERN =

FILE_PATTERN moved to Opencdd::Parcel::LayoutDetector (SSOT). Kept here as an alias for back-compat with callers that reference FlatDirReader::FILE_PATTERN directly.

Opencdd::Parcel::LayoutDetector::FILE_PATTERN
TYPE_BY_PREFIX =
{
  "CLASS"      => :class,
  "PROPERTY"   => :property,
  "RELATION"   => :relation,
  "UNIT"       => :unit,
  "VALUELIST"  => :value_list,
  "VALUETERMS" => :value_term,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FlatDirReader

Returns a new instance of FlatDirReader.



30
31
32
# File 'lib/opencdd/parcel/flat_dir_reader.rb', line 30

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



28
29
30
# File 'lib/opencdd/parcel/flat_dir_reader.rb', line 28

def path
  @path
end

Instance Method Details

#load_into(database) ⇒ Object



57
58
59
60
61
62
# File 'lib/opencdd/parcel/flat_dir_reader.rb', line 57

def load_into(database)
  workbook = read_workbook
  database.add_workbook(workbook)
  database.finalize!
  database
end

#read_workbookObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/opencdd/parcel/flat_dir_reader.rb', line 34

def read_workbook
  files = legacy_files
  raise "No legacy export_*.xls files found in #{@path.inspect}" if files.empty?

  sheets = files.map { |f| read_one(f) }.compact
  sheetmap = files.map { |f| sheetmap_entry_for(f) }.compact

  parcel_id = files.first && File.basename(files.first).split("_").last&.sub(/\.\w+\z/, "")
  project = Opencdd::Parcel::Workbook::ProjectInfo.new(
    project_id: "LOCAL",
    parcel_id: parcel_id,
    multi_language: "",
    base_language: "en",
  )

  Opencdd::Parcel::Workbook.new(
    sheets: sheets,
    sheetmap: sheetmap,
    project: project,
    source_path: @path.to_s,
  )
end