Class: Pubid::Core::UpdateCodes

Inherits:
Object
  • Object
show all
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").expand_path

Class Method Summary collapse

Class Method Details

.apply(code, flavor) ⇒ String

Apply update_codes substitutions for the given flavor.

Examples:

UpdateCodes.apply("IEC 60285-/1:1989", :iec)
# => "IEC 60285-1:1989"

Parameters:

  • code (String)

    identifier string to normalize

  • flavor (Symbol, String)

    flavor name (e.g., :iso, :iec, :ieee, :nist)

Returns:

  • (String)

    normalized identifier string



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

.flavorsArray<Symbol>

Available flavors that have update_codes files.

Returns:

  • (Array<Symbol>)

    list of flavors with data/flavor/update_codes.yaml



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).

Parameters:

  • flavor (Symbol, String)

    flavor name

Returns:

  • (Hash, nil)

    the YAML hash, or nil if file doesn’t exist



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