Class: Uniword::Docx::JunkClassifier
- Inherits:
-
Object
- Object
- Uniword::Docx::JunkClassifier
- Defined in:
- lib/uniword/docx/junk_classifier.rb
Overview
Classify a package-relative path as junk or legitimate.
Used by Docx::PartLoader::RawPartLoader to decide which
unclaimed ZIP entries to carry as RawPart and which to strip.
The classifier is the single source of truth for the junk
decision — adding new criteria means changing this class (or its
pattern constants), not the loader.
Classification is two-armed:
-
OS/tooling artifact — path matches a known pattern (
__MACOSX/,.DS_Store,Thumbs.db,._*,~$*). These are never legitimate document content; matched unconditionally even when a Default content type happens to apply. -
Undeclared part — no Override and no Default extension match in the loaded
ContentTypes::Types, AND no modelled relationship targets the path. This is the OPC rule: every part must have a content type declaration.
Both arms return a human-readable reason string; the loader
records it on Package#stripped_parts for caller introspection.
Constant Summary collapse
- OS_ARTIFACT_PATTERNS =
Path patterns for OS / tooling artifacts that are never legitimate document content. Append to extend; the classifier iterates them in order.
[ /\A__MACOSX\//, # macOS zip metadata directory /\A\.DS_Store\z/, # macOS Finder /Thumbs\.db\z/, # Windows Explorer /\A\._/, # macOS AppleDouble resource fork /\A~\$/, # Office lock files ].freeze
- OS_ARTIFACT_REASON =
"OS or tooling artifact"- UNDECLARED_REASON =
"No content type declaration and no referencing relationship"
Instance Method Summary collapse
-
#initialize(content_types:, relationships_by_path: nil) ⇒ JunkClassifier
constructor
A new instance of JunkClassifier.
-
#junk?(path) ⇒ Boolean
True when the path is junk.
-
#reason(path) ⇒ String?
Reason string when the path is junk; nil when it is legitimate.
Constructor Details
#initialize(content_types:, relationships_by_path: nil) ⇒ JunkClassifier
Returns a new instance of JunkClassifier.
59 60 61 62 |
# File 'lib/uniword/docx/junk_classifier.rb', line 59 def initialize(content_types:, relationships_by_path: nil) @content_types = content_types @relationships_by_path = relationships_by_path || {} end |
Instance Method Details
#junk?(path) ⇒ Boolean
Returns true when the path is junk.
73 74 75 |
# File 'lib/uniword/docx/junk_classifier.rb', line 73 def junk?(path) !reason(path).nil? end |
#reason(path) ⇒ String?
Returns reason string when the path is junk; nil when it is legitimate.
67 68 69 |
# File 'lib/uniword/docx/junk_classifier.rb', line 67 def reason(path) os_artifact_reason(path) || undeclared_reason(path) end |