Class: Opencdd::Reader
- Inherits:
-
Object
- Object
- Opencdd::Reader
- Defined in:
- lib/opencdd/reader.rb
Constant Summary collapse
- XLSX_EXTENSIONS =
%w[.xlsx .xlsm .xltx].freeze
- SHARDED_CLASS_CODE =
Layout detection is owned by Opencdd::Parcel::LayoutDetector (single source of truth). These regexes are exposed here as backward-compat constants for external callers; new code should call LayoutDetector directly.
Opencdd::Parcel::LayoutDetector::CLASS_CODE_PATTERN
- UNID_PATTERN =
Opencdd::Parcel::LayoutDetector::UNID_PATTERN
- LEGACY_XLS_RE =
Opencdd::Parcel::LayoutDetector::FILE_PATTERN
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .detect(path) ⇒ Object
- .load_database(path) ⇒ Object
-
.sharded_class_subdir?(path) ⇒ Boolean
A class-code subdir is "sharded" if it contains export_.xls directly (legacy flat layout) OR exposes the per-version layout (+entity.json+ pointing at a UNID subfolder, or a single UNID subfolder holding export.xls).
Instance Method Summary collapse
-
#initialize(path) ⇒ Reader
constructor
A new instance of Reader.
- #load ⇒ Object
- #load_into(db) ⇒ Object
Constructor Details
#initialize(path) ⇒ Reader
Returns a new instance of Reader.
79 80 81 |
# File 'lib/opencdd/reader.rb', line 79 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
77 78 79 |
# File 'lib/opencdd/reader.rb', line 77 def path @path end |
Class Method Details
.detect(path) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/opencdd/reader.rb', line 20 def detect(path) case File.basename(path) when LEGACY_XLS_RE then :legacy_single else if File.directory?(path) children = Dir.children(path) if children.any? { |f| f =~ LEGACY_XLS_RE } :legacy_dir elsif children.any? { |f| sharded_class_subdir?(File.join(path, f)) } :sharded_dir else :unknown_dir end elsif XLSX_EXTENSIONS.include?(File.extname(path).downcase) :xlsx elsif File.extname(path).downcase == ".xls" :legacy_single else :unknown end end end |
.load_database(path) ⇒ Object
16 17 18 |
# File 'lib/opencdd/reader.rb', line 16 def load_database(path) new(path).load end |
.sharded_class_subdir?(path) ⇒ Boolean
A class-code subdir is "sharded" if it contains export_.xls directly (legacy flat layout) OR exposes the per-version layout (+entity.json+ pointing at a UNID subfolder, or a single UNID subfolder holding export.xls).
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 |
# File 'lib/opencdd/reader.rb', line 48 def self.sharded_class_subdir?(path) return false unless File.directory?(path) return false unless File.basename(path) =~ SHARDED_CLASS_CODE children = Dir.children(path) return true if children.any? { |f| f =~ LEGACY_XLS_RE } # Per-version layout: _entity.json names a UNID subfolder. entity_idx = File.join(path, "_entity.json") if File.file?(entity_idx) require "json" begin data = JSON.parse(File.read(entity_idx)) current = data["current_version_dir"] return true if current && current =~ UNID_PATTERN && File.directory?(File.join(path, current)) rescue JSON::ParserError # fall through end end # Per-version layout without _entity.json: a UNID subfolder holds XLS. unid_subdirs = children.select { |n| n =~ UNID_PATTERN } .map { |n| File.join(path, n) } unid_subdirs.any? do |p| File.directory?(p) && Dir.children(p).any? { |f| f =~ LEGACY_XLS_RE } end end |
Instance Method Details
#load ⇒ Object
83 84 85 86 87 88 |
# File 'lib/opencdd/reader.rb', line 83 def load db = Opencdd::Database.new load_into(db) db.finalize! db end |
#load_into(db) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/opencdd/reader.rb', line 90 def load_into(db) case Opencdd::Reader.detect(@path) when :xlsx, :legacy_single Opencdd::Parcel::WorkbookReader.new(@path).load_into(db) when :legacy_dir Opencdd::Parcel::FlatDirReader.new(@path).load_into(db) when :sharded_dir Opencdd::Parcel::ShardedDirReader.new(@path).load_into(db) else raise ArgumentError, "Cannot detect Parcel/Excel format at #{@path.inspect}" end end |