Class: Pubid::Itu::Identifier
- Inherits:
-
Pubid::Identifier
- Object
- Lutaml::Model::Serializable
- Pubid::Identifier
- Pubid::Itu::Identifier
- Defined in:
- lib/pubid/itu/identifiers/base.rb
Overview
Base class for all ITU identifiers. Canonical name Pubid::Itu::Identifier (Identifiers::Base is a back-compat alias).
Constant Summary collapse
- LANGUAGES =
Long-form ↔ ITU single-letter language code map. The parser produces single-letter codes (E/F/S/R/A/C); API callers (e.g. metanorma-itu) pass long-form (en/fr/es/ru/ar/zh). Storage is normalized to the single-letter form. Languages with no canonical letter (e.g. "de") pass through unchanged and produce no trailing suffix.
{ "fr" => "F", "es" => "S", "ru" => "R", "ar" => "A", "zh" => "C", "en" => "E", "F" => "F", "S" => "S", "R" => "R", "A" => "A", "C" => "C", "E" => "E" }.freeze
Class Method Summary collapse
-
.normalize_to_s_opts(opts) ⇒ Object
Translate
language:opt toi18n_lang:opt. -
.parse(identifier) ⇒ Object
Parse an ITU identifier string into an identifier object.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(**kwargs) ⇒ Identifier
constructor
A new instance of Identifier.
- #render_base(**_opts) ⇒ Object
- #render_language_suffix ⇒ Object
-
#to_s(**opts) ⇒ Object
Render identifier as a string.
Methods inherited from Pubid::Identifier
#base_identifier, #eql?, #exclude, from_hash, #hash, #mr_number, #mr_number_with_part, #mr_part, #mr_publisher, #mr_type, #mr_year, #new_edition_of?, polymorphic_name, polymorphic_type_map, #render, #resolve_urn_generator, #root, #to_hash, #to_mr_string, #to_supplement_s, #to_urn, #urn_supplement_type, #urn_type_code, #year
Constructor Details
#initialize(**kwargs) ⇒ Identifier
Returns a new instance of Identifier.
34 35 36 37 38 39 40 41 42 |
# File 'lib/pubid/itu/identifiers/base.rb', line 34 def initialize(**kwargs) if kwargs[:language] kwargs = kwargs.merge(language: normalize_language(kwargs[:language])) end super validate_ob_no_sector! end |
Class Method Details
.normalize_to_s_opts(opts) ⇒ Object
Translate language: opt to i18n_lang: opt. v1 PR #38 introduced
i18n_lang: to disambiguate "rendering language" from the document
language attribute; language: remains a deprecated alias.
102 103 104 105 106 107 108 109 110 |
# File 'lib/pubid/itu/identifiers/base.rb', line 102 def self.normalize_to_s_opts(opts) opts = opts.dup if opts.key?(:language) && !opts.key?(:i18n_lang) opts[:i18n_lang] = opts.delete(:language) else opts.delete(:language) end opts end |
.parse(identifier) ⇒ Object
Parse an ITU identifier string into an identifier object.
9 10 11 12 13 14 |
# File 'lib/pubid/itu/identifiers/base.rb', line 9 def self.parse(identifier) parsed = Parser.parse(identifier) Builder.build(parsed) rescue Parslet::ParseFailed => e raise "Failed to parse ITU identifier '#{identifier}': #{e.}" end |
Instance Method Details
#==(other) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/pubid/itu/identifiers/base.rb', line 112 def ==(other) return false unless other.is_a?(Pubid::Itu::Identifier) sector == other.sector && series == other.series && code == other.code && date == other.date && language == other.language end |
#render_base(**_opts) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/pubid/itu/identifiers/base.rb', line 67 def render_base(**_opts) result = "#{publisher}-#{sector}" # Add series and code result += if series " #{series}.#{code}" else " #{code}" end # Add date if present if date result += if date.month " (#{date.month}/#{date.year})" else " (#{date.year})" end end result end |
#render_language_suffix ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/pubid/itu/identifiers/base.rb', line 89 def render_language_suffix return "" unless language # Only render canonical single-letter ITU language codes # (e.g. "F", "S"). Languages with no mapping (e.g. "de") get # no suffix — matches v1 PR #38 render_language behavior. return "" unless LANGUAGES.value?(language) "-#{language}" end |
#to_s(**opts) ⇒ Object
Render identifier as a string.
62 63 64 65 |
# File 'lib/pubid/itu/identifiers/base.rb', line 62 def to_s(**opts) opts = self.class.normalize_to_s_opts(opts) render_base(**opts) + render_language_suffix end |