Class: Opencdd::Parcel::WorkbookReader

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

Overview

Reads a single Parcel workbook file (.xlsx, .xlsm, .xltx, or legacy single-sheet .xls) and produces a Opencdd::Parcel::Workbook.

This is the canonical IEC 62656-1 Parcel container: one file with Project / sheetmap / pcls_LOCAL / data sheets. Use Opencdd::Parcel::FlatDirReader for the legacy 6-file-per-directory layout, and Opencdd::Parcel::ShardedDirReader for the per-class sharded layout.

Defined Under Namespace

Classes: RooSource, SpreadsheetSource

Constant Summary collapse

LEGACY_TYPE_BY_PREFIX =
{
  "CLASS"      => :class,
  "PROPERTY"   => :property,
  "RELATION"   => :relation,
  "UNIT"       => :unit,
  "VALUELIST"  => :value_list,
  "VALUETERMS" => :value_term,
}.freeze
LEGACY_TYPE_TO_PARCEL_NAME =
{
  class:       "CLASS",
  property:    "PROPERTY",
  value_list:  "ENUM",
  value_term:  "TERMINOLOGY",
  unit:        "UoM",
  relation:    "RELATION",
}.freeze
META_CLASS_BY_LEGACY_TYPE =
Opencdd::MetaClasses::TYPE_BY_META_CLASS.invert.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ WorkbookReader

Returns a new instance of WorkbookReader.



36
37
38
# File 'lib/opencdd/parcel/workbook_reader.rb', line 36

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



34
35
36
# File 'lib/opencdd/parcel/workbook_reader.rb', line 34

def path
  @path
end

Instance Method Details

#load_into(database) ⇒ Object



90
91
92
93
94
95
# File 'lib/opencdd/parcel/workbook_reader.rb', line 90

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

#read_workbookObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/opencdd/parcel/workbook_reader.rb', line 40

def read_workbook
  sheets = []
  sheetmap = []
  project = nil

  workbook = open_workbook

  if workbook.sheet_names.include?("sheetmap")
    sheetmap = parse_sheetmap(workbook.rows_for("sheetmap"))
  end

  if workbook.sheet_names.include?("Project")
    project = parse_project(workbook.rows_for("Project"))
  end

  workbook.sheet_names.each do |sheet_name|
    next if ["Project", "sheetmap", "pcls_LOCAL"].include?(sheet_name)
    sheet = Opencdd::Parcel::Sheet.from_rows(workbook.rows_for(sheet_name).each, name: sheet_name)
    sheets << sheet if sheet
  end

  if sheets.empty? && legacy?
    sheets = [Opencdd::Parcel::Sheet.from_rows(
      workbook.rows_for(workbook.sheet_names.first).each,
      name: File.basename(@path.to_s, ".*"),
    )].compact
    sheetmap = sheets.map { |s|
      type = s.type
      next unless type
      Workbook::SheetMapEntry.new(
        project_id: project&.project_id || "LOCAL",
        parcel_id:  project&.parcel_id,
        class_irdi: Opencdd::IRDI.parse("0112/2///62656_1##{META_CLASS_BY_LEGACY_TYPE[type]}"),
        content_no: 0,
        sheet_no:   nil,
        sheet_name: s.name,
        type:       LEGACY_TYPE_TO_PARCEL_NAME[type] || type.to_s.upcase,
        target:     "",
      )
    }.compact
  end

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