Module: Pubid::Iso

Defined in:
lib/pubid/iso.rb,
lib/pubid/iso/parser.rb,
lib/pubid/iso/builder.rb,
lib/pubid/iso/utilities.rb,
lib/pubid/iso/components.rb,
lib/pubid/iso/identifier.rb,
lib/pubid/iso/normalizer.rb,
lib/pubid/iso/urn_parser.rb,
lib/pubid/iso/identifiers.rb,
lib/pubid/iso/urn_generator.rb,
lib/pubid/iso/components/code.rb,
lib/pubid/iso/format_resolver.rb,
lib/pubid/iso/identifiers/pas.rb,
lib/pubid/iso/rendering_style.rb,
lib/pubid/iso/identifiers/data.rb,
lib/pubid/iso/identifiers/guide.rb,
lib/pubid/iso/single_identifier.rb,
lib/pubid/iso/bundled_identifier.rb,
lib/pubid/iso/combined_identifier.rb,
lib/pubid/iso/identifiers/extract.rb,
lib/pubid/iso/components/publisher.rb,
lib/pubid/iso/identifiers/addendum.rb,
lib/pubid/iso/identifiers/amendment.rb,
lib/pubid/iso/supplement_identifier.rb,
lib/pubid/iso/identifiers/directives.rb,
lib/pubid/iso/identifiers/supplement.rb,
lib/pubid/iso/identifiers/corrigendum.rb,
lib/pubid/iso/identifiers/tc_document.rb,
lib/pubid/iso/identifiers/recommendation.rb,
lib/pubid/iso/identifiers/technical_report.rb,
lib/pubid/iso/identifiers/directives_supplement.rb,
lib/pubid/iso/identifiers/international_standard.rb,
lib/pubid/iso/identifiers/technical_specification.rb,
lib/pubid/iso/identifiers/technology_trends_assessments.rb,
lib/pubid/iso/identifiers/international_workshop_agreement.rb,
lib/pubid/iso/identifiers/international_standardized_profile.rb

Defined Under Namespace

Modules: Components, Identifiers, Utilities Classes: Builder, BundledIdentifier, CombinedIdentifier, FormatResolver, Identifier, Normalizer, Parser, RefDated, RefDatedLong, RefNumLong, RefNumShort, RefUndated, RefUndatedLong, RenderingStyle, SingleIdentifier, SupplementIdentifier, UrnGenerator, UrnParser

Class Method Summary collapse

Class Method Details

.all_typed_stagesArray<Pubid::Components::TypedStage>

Build typed stage index from identifier types

Returns:



151
152
153
154
155
156
157
158
159
# File 'lib/pubid/iso.rb', line 151

def self.all_typed_stages
  @all_typed_stages ||= identifier_types.flat_map do |klass|
    if klass.const_defined?(:TYPED_STAGES)
      klass.const_get(:TYPED_STAGES)
    else
      []
    end
  end
end

.build(type:, publisher: nil, copublishers: nil, number: nil, year: nil, **attrs) ⇒ Pubid::Iso::Identifier

Build an ISO identifier from attributes.

This is the public seam: callers in other flavors should never reach into ‘Pubid::Iso::Identifiers::*` or `Pubid::Iso::Components::*` directly — go through this method so the internal class registry can change without breaking callers.

Accepts either raw values (Strings/Integers) or pre-built components. Raw values are wrapped in the appropriate ISO component internally.

Parameters:

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pubid/iso.rb', line 57

def self.build(type:, publisher: nil, copublishers: nil, number: nil,
               year: nil, **attrs)
  klass = locate_type(type) || raise(ArgumentError, "unknown ISO type: #{type.inspect}")

  copub_list = Array(copublishers)
  attrs[:publisher] = build_publisher(publisher, copub_list) if publisher || copub_list.any?
  attrs[:copublishers] = build_copublishers(copub_list) if copub_list.any?
  attrs[:number] = build_code(number) if number
  attrs[:date] = build_date(year) if year

  klass.new(attrs)
end

.build_from_parse(hash) ⇒ Pubid::Iso::Identifier

Build an ISO identifier from a Parslet parse-tree hash (used by other flavors when parsing joint identifiers, e.g. IEC’s ‘IEC … | ISO …` form).

This is the public seam: callers should never reach into ‘Pubid::Iso::Builder` directly.

Parameters:

  • hash (Hash)

    parse tree for an ISO identifier

Returns:



124
125
126
# File 'lib/pubid/iso.rb', line 124

def self.build_from_parse(hash)
  builder.build(hash)
end

.builderPubid::Iso::Builder

Memoized builder instance used by Normalizer / URN parser

Returns:



163
164
165
# File 'lib/pubid/iso.rb', line 163

def self.builder
  @builder ||= Builder.new
end

.default_typed_stagePubid::Components::TypedStage

Default typed stage used when no type_with_stage is present (e.g., the bare International Standard published stage)



215
216
217
# File 'lib/pubid/iso.rb', line 215

def self.default_typed_stage
  locate_stage("") || all_typed_stages.first
end

.identifier_typesArray<Class>

Auto-discover all identifier types from the Identifiers namespace

Returns:

  • (Array<Class>)

    identifier classes that define a self.type Hash



142
143
144
145
146
147
# File 'lib/pubid/iso.rb', line 142

def self.identifier_types
  @identifier_types ||= Identifiers.constants
    .filter_map { |c| begin; Identifiers.const_get(c); rescue NameError; nil; end }
    .select { |c| c.is_a?(Class) && c.singleton_methods(false).include?(:type) }
    .select { |c| c.type.is_a?(Hash) }
end

.joint_grammar_atom(rule_name) ⇒ Parslet::Atoms::Base

Return the named Parslet rule atom from the ISO parser, for embedding in another flavor’s grammar (e.g. IEC joint identifiers).

This is the public seam: callers should never instantiate ‘Pubid::Iso::Parser` directly.

Parameters:

  • rule_name (Symbol)

    the parser rule to expose

Returns:

  • (Parslet::Atoms::Base)


136
137
138
# File 'lib/pubid/iso.rb', line 136

def self.joint_grammar_atom(rule_name)
  parser.public_send(rule_name)
end

.locate_stage(abbr) ⇒ Pubid::Components::TypedStage?

Lookup: abbreviation -> typed stage

Parameters:

  • abbr (String, Symbol)

    the abbreviation to find

Returns:



183
184
185
186
# File 'lib/pubid/iso.rb', line 183

def self.locate_stage(abbr)
  abbr_str = abbr.to_s.upcase
  all_typed_stages.find { |s| s.abbr.any? { |a| a.to_s.upcase == abbr_str } }
end

.locate_stage_by_code(code) ⇒ Pubid::Components::TypedStage?

Lookup: per-typed-stage code -> typed stage

Parameters:

  • code (String, Symbol)

Returns:



199
200
201
202
# File 'lib/pubid/iso.rb', line 199

def self.locate_stage_by_code(code)
  code_sym = code.to_sym
  all_typed_stages.find { |s| s.code&.to_sym == code_sym }
end

.locate_stage_by_harmonized_code(harmonized_code) ⇒ Pubid::Components::TypedStage?

Lookup: harmonized stage code -> typed stage

Parameters:

  • harmonized_code (String)

Returns:



207
208
209
210
# File 'lib/pubid/iso.rb', line 207

def self.locate_stage_by_harmonized_code(harmonized_code)
  harmonized_str = harmonized_code.to_s
  all_typed_stages.find { |s| s.harmonized_stages&.include?(harmonized_str) }
end

.locate_stage_by_stage_code(stage_code) ⇒ Pubid::Components::TypedStage?

Lookup: stage code -> typed stage

Parameters:

  • stage_code (String, Symbol)

Returns:



191
192
193
194
# File 'lib/pubid/iso.rb', line 191

def self.locate_stage_by_stage_code(stage_code)
  stage_code_sym = stage_code.to_sym
  all_typed_stages.find { |s| s.stage_code.to_sym == stage_code_sym }
end

.locate_type(code) ⇒ Class?

Lookup: type code -> identifier class

Parameters:

  • code (String, Symbol)

    the type key to find

Returns:

  • (Class, nil)

    the matching identifier class



176
177
178
# File 'lib/pubid/iso.rb', line 176

def self.locate_type(code)
  identifier_types.find { |t| t.type[:key].to_s == code.to_s }
end

.parse(identifier, format: :auto) ⇒ Identifier

Parse an ISO identifier string

Parameters:

  • identifier (String)

    the identifier string to parse

  • format (Symbol) (defaults to: :auto)

    :auto, :human, :mr_string, or :urn

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pubid/iso.rb', line 27

def self.parse(identifier, format: :auto)
  format = Pubid::FormatDetector.detect(identifier) if format == :auto

  case format
  when :urn
    UrnParser.parse(identifier)
  when :mr_string
    Pubid::Parsers::MrString.parse(identifier)
  else
    Normalizer.apply(identifier)
  end
end

.parse_urn(urn) ⇒ Identifier

Parse an ISO URN string

Parameters:

  • urn (String)

    the URN string to parse

Returns:

Raises:

  • (Errors::ParseError)

    if URN is invalid



112
113
114
# File 'lib/pubid/iso.rb', line 112

def self.parse_urn(urn)
  UrnParser.parse(urn)
end

.parserPubid::Iso::Parser

Memoized parser instance used by Normalizer

Returns:



169
170
171
# File 'lib/pubid/iso.rb', line 169

def self.parser
  @parser ||= Parser.new
end