Module: Unitsdb::Database::LookupStrategies

Defined in:
lib/unitsdb/database/reference_validator.rb

Overview

Lookup strategies. Each is a proc that takes (pair, ref_type, registry) and returns true if the reference is valid under that strategy. Composed in ALL and tried in order by ReferenceChecker.

Constant Summary collapse

COMPOSITE_KEY =

Exact "#type:#id" match.

->(pair, ref_type, registry) do
  registry.key?(ref_type) && registry[ref_type].key?("#{pair.type}:#{pair.id}")
end
BARE_ID =

Bare-id match (any type). For backward compatibility with databases that only stored the id without the type.

->(pair, ref_type, registry) do
  registry.key?(ref_type) && registry[ref_type].key?(pair.id)
end
UNIT_SYSTEM_ALTERNATE =

Unit-system alternate IDs. The UnitsML unitsml authority uses kebab-case (si-base) while NIST uses snake-case (SI_base). Accept either form when resolving unit_system references.

->(pair, ref_type, registry) do
  next false unless ref_type == "unit_systems" && pair.type == "unitsml"
  next false unless registry.key?(ref_type)

  alternates = []
  alternates << pair.id
  alternates << "SI_#{pair.id.sub('si-', '')}" if pair.id.start_with?("si-")
  alternates << "non-SI_#{pair.id.sub('nonsi-', '')}" if pair.id.start_with?("nonsi-")

  keys = registry[ref_type].keys
  alternates.any? { |alt| keys.any? { |k| k.end_with?(":#{alt}") } }
end
ALL =
[COMPOSITE_KEY, BARE_ID, UNIT_SYSTEM_ALTERNATE].freeze