Class: Pubid::W3c::Identifier

Inherits:
Identifier
  • Object
show all
Defined in:
lib/pubid/w3c/identifier.rb

Overview

Base class for every W3C identifier AND the flavor's parse/create entry point. Concrete identifiers under Pubid::W3c::Identifiers descend from this class (one per W3C maturity level), so a parsed W3C id is an instance of Pubid::W3c::Identifier.

W3C identifiers are flat: a slug code, an optional publication date (kept verbatim as a string — width varies: 8-digit YYYYMMDD, or legacy 6-digit YYMMDD / 4-digit MMDD), and a document-type carried by the concrete class (its type_prefix returns the printed token, e.g. "WD").

Constant Summary collapse

W3C_TYPE_MAP =

Polymorphic type map for lutaml::Model key_value (de)serialization: maps each subclass's polymorphic_name to its class name so a stored hash rebuilds the correct identifier type via from_hash.

{
  "pubid:w3c:standard" => "Pubid::W3c::Identifiers::Standard",
  "pubid:w3c:note" => "Pubid::W3c::Identifiers::Note",
  "pubid:w3c:draft-note" => "Pubid::W3c::Identifiers::DraftNote",
  "pubid:w3c:working-draft" => "Pubid::W3c::Identifiers::WorkingDraft",
  "pubid:w3c:candidate-recommendation" =>
    "Pubid::W3c::Identifiers::CandidateRecommendation",
  "pubid:w3c:candidate-recommendation-draft" =>
    "Pubid::W3c::Identifiers::CandidateRecommendationDraft",
  "pubid:w3c:recommendation" =>
    "Pubid::W3c::Identifiers::Recommendation",
  "pubid:w3c:proposed-recommendation" =>
    "Pubid::W3c::Identifiers::ProposedRecommendation",
  "pubid:w3c:proposed-edited-recommendation" =>
    "Pubid::W3c::Identifiers::ProposedEditedRecommendation",
  "pubid:w3c:superseded-recommendation" =>
    "Pubid::W3c::Identifiers::SupersededRecommendation",
  "pubid:w3c:obsolete-recommendation" =>
    "Pubid::W3c::Identifiers::ObsoleteRecommendation",
}.freeze
PUBLISHER =

Publisher is always "W3C". A plain constant (not a publisher method) so it doesn't shadow the inherited lutaml publisher attribute, which would otherwise fail serialization type validation.

"W3C"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Identifier

apply_mappings, #base, #base_document, concrete_class_for, #dated_version_of?, #draft_of?, #drop_supplements, #edition_of?, #eql?, #exclude, from_hash, #has_supplement?, #hash, #includes?, #initialize, #matches?, #mr_all_parts, #mr_edition, #mr_languages, #mr_number, #mr_number_with_part, #mr_part, #mr_publisher, #mr_subpart, #mr_supplement_suffix, #mr_type, #mr_year, #new_edition_of?, polymorphic_name, polymorphic_type_map, #related_to?, #render, #resolve_urn_generator, #root, #sibling_of?, #supplement_of?, #to_hash, #to_mr_string, #to_slug, #to_supplement_s, #to_urn, #urn_supplement_type, #urn_type_code

Constructor Details

This class inherits a constructor from Pubid::Identifier

Instance Attribute Details

#with_publisherObject (readonly)

Returns the value of attribute with_publisher.



64
65
66
# File 'lib/pubid/w3c/identifier.rb', line 64

def with_publisher
  @with_publisher
end

Class Method Details

.parse(identifier) ⇒ Identifier

Parse a W3C identifier string into an identifier object

Parameters:

  • identifier (String)

    The W3C identifier string to parse

Returns:

  • (Identifier)

    The appropriate identifier object

Raises:

  • (RuntimeError)

    If parsing fails



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pubid/w3c/identifier.rb', line 87

def self.parse(identifier)
  # Reject pathological inputs before they reach the parser
  # (CodeQL rb/polynomial-redos barrier — inline .length by design).
  if identifier.length > Pubid::MAX_INPUT_LENGTH
    raise ArgumentError, Pubid::INPUT_TOO_LONG_MESSAGE
  end

  parsed = Parser.parse(identifier)
  Builder.build(parsed)
rescue Parslet::ParseFailed => e
  raise "Failed to parse W3C identifier '#{identifier}': #{e.message}"
end

Instance Method Details

#to_s(with_publisher: true, **opts) ⇒ Object



75
76
77
78
# File 'lib/pubid/w3c/identifier.rb', line 75

def to_s(with_publisher: true, **opts)
  @with_publisher = with_publisher
  render(format: :human, **opts)
end

#type_prefixObject

The printed document-type token (e.g. "WD"). nil for the bare-code Standard form; concrete typed subclasses override it.



60
61
62
# File 'lib/pubid/w3c/identifier.rb', line 60

def type_prefix
  nil
end

#yearObject

Publication year derived from an 8-digit YYYYMMDD date, else nil. date is a plain string here (not a Components::Date), so the inherited year — which calls date&.year — would break; override it.



69
70
71
72
73
# File 'lib/pubid/w3c/identifier.rb', line 69

def year
  return nil unless date.is_a?(::String) && date.length == 8

  date[0, 4]
end