Class: Pubid::Core::UpdateCodes
- Inherits:
-
Object
- Object
- Pubid::Core::UpdateCodes
- Defined in:
- lib/pubid/core/update_codes.rb
Overview
Centralized update_codes loader and applier.
Reads YAML files from data/flavor/update_codes.yaml and applies substitution patterns to normalize malformed identifiers BEFORE parsing.
Each flavor’s parse entry point calls UpdateCodes.apply(code, :flavor) before any parser-specific preprocessing.
Format in YAML files:
plain string match: "IEC 60285-/1:1989" → "IEC 60285-1:1989"
regex match: "/^NBS CIRC sup$/" → "NBS CIRC 24e7sup"
Plain strings use full-line anchoring (^pattern$). Regex patterns are wrapped in /…/ slashes.
Constant Summary collapse
- DATA_DIR =
Pathname.new(__dir__).join("../../../data").
Class Method Summary collapse
-
.apply(code, flavor) ⇒ String
Apply update_codes substitutions for the given flavor.
-
.flavors ⇒ Array<Symbol>
Available flavors that have update_codes files.
-
.for_flavor(flavor) ⇒ Hash?
Get the loaded update_codes hash for a flavor (cached).
Class Method Details
.apply(code, flavor) ⇒ String
Apply update_codes substitutions for the given flavor.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pubid/core/update_codes.rb', line 35 def apply(code, flavor) codes = for_flavor(flavor) return code if codes.nil? || codes.empty? codes.each do |from, to| code = code.gsub( from.match?(%r{^/.*/$}) ? Regexp.new(from[1..-2]) : /^#{Regexp.escape(from)}$/, to ) end code end |
.flavors ⇒ Array<Symbol>
Available flavors that have update_codes files.
60 61 62 63 64 |
# File 'lib/pubid/core/update_codes.rb', line 60 def flavors @flavors ||= DATA_DIR.children.select(&:directory?).map do |dir| dir.basename.to_s.to_sym end.select { |f| (DATA_DIR / f.to_s / "update_codes.yaml").exist? } end |
.for_flavor(flavor) ⇒ Hash?
Get the loaded update_codes hash for a flavor (cached).
51 52 53 54 55 |
# File 'lib/pubid/core/update_codes.rb', line 51 def for_flavor(flavor) @cache ||= {} flavor_key = flavor.to_s.to_sym @cache[flavor_key] ||= load_yaml(flavor_key) end |