Class: Pubid::Jis::Identifier

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

Overview

Base class for every JIS identifier AND the flavor’s parse/create entry point — mirrors Pubid::Iso::Identifier. Concrete identifiers under Pubid::Jis::Identifiers descend from this class, so a parsed JIS id is an instance of Pubid::Jis::Identifier.

Direct Known Subclasses

SingleIdentifier

Constant Summary collapse

JIS_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:jis:japanese-industrial-standard" =>
    "Pubid::Jis::Identifiers::JapaneseIndustrialStandard",
  "pubid:jis:standard" => "Pubid::Jis::Identifiers::Standard",
  "pubid:jis:technical-report" =>
    "Pubid::Jis::Identifiers::TechnicalReport",
  "pubid:jis:technical-specification" =>
    "Pubid::Jis::Identifiers::TechnicalSpecification",
  "pubid:jis:amendment" => "Pubid::Jis::Identifiers::Amendment",
  "pubid:jis:corrigendum" => "Pubid::Jis::Identifiers::Corrigendum",
  "pubid:jis:explanation" => "Pubid::Jis::Identifiers::Explanation",
}.freeze
PUBLISHER =

Publisher is always “JIS”. A plain constant (not a ‘publisher` method) so it doesn’t shadow the inherited lutaml ‘publisher` attribute, which would otherwise fail serialization type validation.

"JIS"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from 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

.from_hash(data, options = {}) ⇒ Object

Dispatch deserialization to the concrete identifier class named by the stored ‘_type`, so `from_hash` rebuilds e.g. a Corrigendum rather than a bare Pubid::Jis::Identifier. lutaml resolves `_type` only for validation, not instantiation, so we route to the mapped subclass and let its own (inherited) from_hash do the actual work.



125
126
127
128
129
130
131
132
133
# File 'lib/pubid/jis/identifier.rb', line 125

def self.from_hash(data, options = {})
  type = data["_type"] || data[:_type]
  klass_name = JIS_TYPE_MAP[type]
  if klass_name
    klass = Object.const_get(klass_name)
    return klass.from_hash(data, options) unless klass == self
  end
  super
end

.parse(identifier) ⇒ Identifier

Parse a JIS identifier string into an identifier object

Parameters:

  • identifier (String)

    The JIS identifier string to parse

Returns:

  • (Identifier)

    The appropriate identifier object

Raises:

  • (RuntimeError)

    If parsing fails



139
140
141
142
143
144
# File 'lib/pubid/jis/identifier.rb', line 139

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

Instance Method Details

#==(other) ⇒ Object

Comparison with all_parts logic When either identifier has all_parts=true, compare only series and number



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pubid/jis/identifier.rb', line 96

def ==(other)
  return false unless other.is_a?(Identifier)

  if all_parts? || other.all_parts?
    # Compare only series and number, ignore year, parts, all_parts
    return series == other.series && number == other.number
  end

  # Normal full comparison
  series == other.series &&
    number == other.number &&
    (parts || []) == (other.parts || []) &&
    year == other.year &&
    language == other.language &&
    all_parts? == other.all_parts? &&
    reaffirmed? == other.reaffirmed? &&
    symbol == other.symbol
end

#all_parts?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/pubid/jis/identifier.rb', line 65

def all_parts?
  all_parts == true
end

#codeObject

The rendered document code, e.g. “B 0205-1” (series, number, parts). A convenience for rendering; the underlying data is the flat series/number/parts attributes.



88
89
90
91
92
# File 'lib/pubid/jis/identifier.rb', line 88

def code
  result = "#{series} #{number}"
  result += parts.map { |p| "-#{p}" }.join if parts&.any?
  result
end

#reaffirmed?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/pubid/jis/identifier.rb', line 69

def reaffirmed?
  reaffirmed == true
end

#symbol_suffixObject

Render the trailing “ SYMBOL[ <value>]” clause, or “” when absent.



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

def symbol_suffix
  return "" if symbol.nil?

  symbol.empty? ? " SYMBOL" : " SYMBOL #{symbol}"
end

#to_s(**opts) ⇒ Object

Basic string representation. Delegates to renderer.



116
117
118
# File 'lib/pubid/jis/identifier.rb', line 116

def to_s(**opts)
  render(format: :human, **opts)
end

#year_with_reaffirmationObject

Render a year with its reaffirmation marker, e.g. “2019R”.



74
75
76
# File 'lib/pubid/jis/identifier.rb', line 74

def year_with_reaffirmation
  "#{year}#{'R' if reaffirmed?}"
end