Class: Pubid::Idf::Identifier

Inherits:
Pubid::Identifier show all
Defined in:
lib/pubid/idf/identifier.rb

Overview

An IDF identifier has no copublisher

Direct Known Subclasses

SingleIdentifier, SupplementIdentifier

Constant Summary collapse

TYPE_KEY_TO_KLASS =

Factory mirroring pubid 1.x’s ‘Pubid::Idf::Identifier.create` API. Default subclass is Pubid::Idf::Identifiers::InternationalStandard.

IDF’s renderer requires ‘typed_stage` to be set (calls `.abbreviation` without a nil check); factory auto-resolves the “published” TypedStage for the chosen subclass.

{
  is:              "InternationalStandard",
  reviewed_method: "ReviewedMethod",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pubid::Identifier

#base_identifier, #eql?, #exclude, #hash, #initialize, #mr_number, #mr_number_with_part, #mr_part, #mr_publisher, #mr_type, #mr_year, #new_edition_of?, polymorphic_name, #render, #resolve_urn_generator, #root, #to_mr_string, #to_supplement_s, #to_urn, #urn_supplement_type, #urn_type_code, #year

Constructor Details

This class inherits a constructor from Pubid::Identifier

Class Method Details

.create(type: nil, stage: nil, **opts) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/pubid/idf/identifier.rb', line 79

def self.create(type: nil, stage: nil, **opts)
  klass = resolve_create_class(type)
  attrs = coerce_create_attrs(opts)
  ts = resolve_create_typed_stage(klass, stage)
  attrs[:typed_stage] = ts if ts
  klass.new(**attrs)
end

.parse(string) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/pubid/idf/identifier.rb', line 58

def self.parse(string)
  parsed = Pubid::Idf::Parser.new.parse(string)
  if parsed.nil? || parsed.empty?
    raise Pubid::Idf::Parser::ParseError,
          "Invalid identifier format"
  end

  Pubid::Idf::Builder.new.build(parsed)
end

Instance Method Details

#language_portion(lang_single: false) ⇒ Object

Returns a string representation of the languages :single returns single-char language codes



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pubid/idf/identifier.rb', line 46

def language_portion(lang_single: false)
  return "" unless languages&.any?

  [
    "(",
    languages.map do |lang|
      lang.to_s(lang_single: lang_single)
    end.join(lang_single ? "/" : ","),
    ")",
  ].join
end

#number_portion(lang_single: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pubid/idf/identifier.rb', line 24

def number_portion(lang_single: false)
  [
    # Directives may not have a number
    (number ? " #{number.value}" : ""),

    # Parts and subparts are optional
    (part ? "-#{part.value}" : ""),
    (subpart ? "-#{subpart.value}" : ""),

    # Stage iteration is optional
    (stage_iteration ? ".#{stage_iteration.value}" : ""),

    # Date is optional
    (date ? ":#{date.year}" : ""),

    # Languages are optional
    language_portion(lang_single: lang_single),
  ].join
end

#publisher_portion(lang: :en) ⇒ Object



17
18
19
20
21
22
# File 'lib/pubid/idf/identifier.rb', line 17

def publisher_portion(lang: :en)
  [
    publisher.body,
    (typed_stage.abbreviation.empty? ? "" : "/#{typed_stage.abbreviation}"),
  ].join
end

#to_s(lang: :en, lang_single: false, with_edition: false) ⇒ Object



10
11
12
13
14
15
# File 'lib/pubid/idf/identifier.rb', line 10

def to_s(lang: :en, lang_single: false, with_edition: false)
  [
    publisher_portion(lang: lang),
    number_portion(lang_single: lang_single),
  ].join
end