Class: Opencdd::Parcel::VersionedReader

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

Overview

Reads historical versions of entities from a sharded per-version Parcel layout.

The sharded layout stores every version of an entity in a per-UNID subfolder: <entity_dir>/<UNID>/export_*.xls. The _entity.json sidecar lists every version and which UNID is current. ShardedDirReader reads only the current version; VersionedReader exposes the full history.

Delegates to:

- +Opencdd::Parcel::EntityManifest+ for version metadata.
- +Opencdd::Parcel::FlatDirReader+ for xls parsing of one
version's subfolder.

Example:

reader = Opencdd::Parcel::VersionedReader.new("downloads/iec63213")
reader.versions_for("KEA012")    # => 3 VersionHistory::Entry
reader.load_version("KEA012", "ABC123...") # => Database with v002 content

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ VersionedReader

Returns a new instance of VersionedReader.



26
27
28
# File 'lib/opencdd/parcel/versioned_reader.rb', line 26

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



24
25
26
# File 'lib/opencdd/parcel/versioned_reader.rb', line 24

def path
  @path
end

Instance Method Details

#entity_dir_for(code) ⇒ Object

The entity subdirectory for code. Tries both the legacy <path>/<code> layout and the manifest-driven <path>/_entities/<code> layout.



62
63
64
65
66
67
68
# File 'lib/opencdd/parcel/versioned_reader.rb', line 62

def entity_dir_for(code)
  flat = File.join(@path, code)
  return flat if File.directory?(flat)
  sharded = File.join(@path, "_entities", code)
  return sharded if File.directory?(sharded)
  nil
end

#load_version(code, unid) ⇒ Object

Load a single historical version's content as a standalone Database. The unid identifies which version's subfolder to read.

Returns nil if the version subfolder doesn't exist or has no .xls files. The returned database is finalized but not cross-linked to other entities (historical context only).



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opencdd/parcel/versioned_reader.rb', line 46

def load_version(code, unid)
  version_dir = version_dir_for(code, unid)
  return nil unless version_dir && File.directory?(version_dir)
  return nil unless Dir.children(version_dir).any? { |f| Opencdd::Parcel::LayoutDetector.legacy_export?(f) }

  workbook = Opencdd::Parcel::FlatDirReader.new(version_dir).read_workbook
  database = Opencdd::Database.new
  database.add_workbook(workbook)
  attach_version_history(database, code)
  database.finalize!
  database
end

#versions_for(code) ⇒ Object

All versions recorded for code, as Opencdd::Entity::VersionHistory::Entry instances. Empty VersionHistory if code has no manifest.



33
34
35
36
37
# File 'lib/opencdd/parcel/versioned_reader.rb', line 33

def versions_for(code)
  manifest = manifest_for(code)
  return Opencdd::Entity::VersionHistory.new unless manifest
  manifest.version_history
end