Module: Pubid::IdentifierFacade

Included in:
Ashrae::Identifier, Bsi::Identifier, CenCenelec::Identifier, Pubid::Iho::Identifier, Nist::Identifier
Defined in:
lib/pubid/identifier_facade.rb

Overview

Mixin for the per-flavor ‘Identifier` module facades (NIST, IHO, BSI, CEN/CENELEC, ASHRAE). The class-pattern flavors (ISO, IEC, JIS, …) expose their root identifier as a real class, so `id.is_a?(Pubid::Iso::Identifier)` and `Pubid::Iso::Identifier.from_hash` already work natively. The module-pattern flavors keep `Identifier` as a thin parse/factory module; this mixin gives that module the same two capabilities a consumer relies on when it is handed the module as its identifier class (e.g. relaton-index’s ‘pubid_class`):

* `from_hash` — polymorphic deserialization that dispatches on the
  `_type` discriminator to the correct concrete `Identifiers::*` class,
  reusing the flavor's own `identifier_types` registry rather than a
  hand-maintained map.
* identity — `extend`ing this mixin into the `Identifier` module is paired
  with `include`-ing that module into the flavor's `Identifiers::Base`, so
  every concrete identifier descends from it. That makes
  `id.is_a?(Pubid::Nist::Identifier)` and `Pubid::Nist::Identifier === id`
  both true (`Module#===` delegates to `is_a?`) — the test a consumer
  holding the module needs to ask "is this one of my identifiers?".

The flavor namespace, the ‘Identifiers` namespace, and the fallback `Base` class are all derived from the extending module’s own name by convention (‘Pubid::<Flavor>::Identifier`), so a flavor opts in with a single `extend Pubid::IdentifierFacade`.

Opt-in is deliberately GATED to flavors whose ‘to_hash`/`from_hash` round-trips cleanly. Some module-pattern flavors (AMCA, ITU, PLATEAU, IEEE) have a broken `to_hash` (publisher stored as a String) or a lossy `from_hash`; for those the facade’s identity check would route ids through a consumer’s ‘to_hash` and crash or silently corrupt them, so they intentionally do not opt in (see the NOTE in each of their identifier.rb).

Instance Method Summary collapse

Instance Method Details

#from_hash(data, options = {}) ⇒ Pubid::Identifier

Reconstruct a concrete identifier from its serialized key_value hash by dispatching on the ‘_type` discriminator. Falls back to the flavor’s ‘Identifiers::Base` for an absent/unknown type, matching lutaml’s own non-polymorphic default.

Parameters:

  • data (Hash)

    the serialized identifier hash (string or symbol keys)

Returns:



43
44
45
46
47
# File 'lib/pubid/identifier_facade.rb', line 43

def from_hash(data, options = {})
  type = data && (data["_type"] || data[:_type])
  klass = polymorphic_type_map[type] || identifier_base_class
  klass.from_hash(data, options)
end

#polymorphic_type_mapHash{String => Class}

Map of polymorphic_name (“pubid:nist:interagency-report”) => concrete class, built once from the flavor’s identifier-type registry so it tracks newly registered types automatically.

Returns:

  • (Hash{String => Class})


53
54
55
56
57
58
59
60
# File 'lib/pubid/identifier_facade.rb', line 53

def polymorphic_type_map
  @polymorphic_type_map ||=
    (identifier_registry_classes + [identifier_base_class]).uniq
      .each_with_object({}) do |klass, map|
        poly_name = klass.polymorphic_name
        map[poly_name] = klass if poly_name
      end
end