Class: Pubid::Nist::Identifiers::Base

Inherits:
Identifier
  • Object
show all
Includes:
Pubid::Nist::Identifier
Defined in:
lib/pubid/nist/identifiers/base.rb

Overview

Base NIST/NBS identifier class Each series type inherits from this and overrides series_code

Constant Summary collapse

EQUALITY_IGNORED_ATTRS =

Attributes that are build artifacts or rendering aliases, not part of an identifier’s logical identity. They diverge between equally- valid spellings of the same id (e.g. long “Rev. 1” vs short “r1”):

- edition_component: redundant alias of :edition
- first_number/second_number: decomposed parts of the canonical
  :number, retained from the parse for building
- parsed_format: records the input format for round-trip rendering
%i[
  edition_component first_number second_number parsed_format
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pubid::Nist::Identifier

parse

Methods included from IdentifierFacade

#from_hash, #polymorphic_type_map

Methods inherited from Identifier

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

Constructor Details

#initialize(**attributes) ⇒ Base

Returns a new instance of Base.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pubid/nist/identifiers/base.rb', line 91

def initialize(**attributes)
  super()

  attrs = self.class.attributes
  attributes.each do |key, value|
    next if value.nil?

    setter = :"#{key}="
    public_send(setter, value) if attrs.key?(key)
  end

  # NOTE: Compound number building is handled by the Builder class
  # Do NOT build compound numbers here - let the builder apply special patterns first
  # See lib/pubid/nist/builder.rb lines 368-472 for compound number logic
end

Class Method Details

.typed_stagesObject

Default: no typed stages. Subclasses override as needed.



17
18
19
# File 'lib/pubid/nist/identifiers/base.rb', line 17

def self.typed_stages
  []
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Logical identity comparison: equal when every attribute except the build artifacts/aliases above matches. (Edition#== already ignores its rendering-only original_prefix.)



121
122
123
124
125
126
127
# File 'lib/pubid/nist/identifiers/base.rb', line 121

def ==(other)
  return false unless other.instance_of?(self.class)

  self.class.attributes.each_key.all? do |name|
    EQUALITY_IGNORED_ATTRS.include?(name) || public_send(name) == other.public_send(name)
  end
end

#append_mr_components(skip_part: false) ⇒ Object

Render the optional component tail for machine-readable (MR) form. Mirrors #append_short_components so the MR output is just as lossless; subclasses that override #to_mr_style reuse this instead of hand-listing components. skip_part: behaves as in the short helper.



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/pubid/nist/identifiers/base.rb', line 551

def append_mr_components(skip_part: false)
  result = ""
  effective_part = skip_part ? nil : part

  # Volume / Part components (pt1, v6n1, etc.)
  if volume.is_a?(Components::Volume) && effective_part.is_a?(Components::Part)
    result += "#{volume}#{effective_part}"
  elsif effective_part.is_a?(Components::Part)
    result += effective_part.to_s
  elsif volume && !issue_number && !effective_part
    result += (volume.is_a?(Components::Volume) ? volume.to_s : "v#{volume}")
  elsif volume && issue_number
    vol_str = volume.is_a?(Components::Volume) ? volume.to_s : "v#{volume}"
    result += "#{vol_str}n#{issue_number.number}"
  end

  # Use edition component - NO space before edition in MR format (per NIST spec)
  result += edition.to_s if edition

  # Use version_component
  result += version_component.to_s(:mr) if version_component

  # Supplement (e.g. ".9981sup7") - keep distinct documents distinct
  result += supplement_short

  # Use update_component
  result += update_component.to_s(:mr) if update_component

  # Use stage
  result += ".#{stage.to_s(:mr)}" if stage

  # Add addendum - render as ".Add." suffix in MR format
  if addendum || addendum_number
    result += ".Add."
  end

  # Use translation_component
  result += translation_component.to_s(:mr) if translation_component

  result
end

#append_short_components(skip_part: false) ⇒ Object

Render the optional component “tail” that follows publisher/series/number in short (human) form. Extracted from #to_short_style so subclasses that build a series-specific prefix can reuse it instead of hand-listing components (and forgetting some).

skip_part: true lets a series that renders its part differently (e.g. FIPS uses dash “-1”, not “pt1”) emit the part itself and skip the generic part rendering here, while still getting volume/edition/ supplement/etc.



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/pubid/nist/identifiers/base.rb', line 430

def append_short_components(skip_part: false)
  result = ""
  effective_part = skip_part ? nil : part

  # Volume and Part components (v6n1 notation for CSM, pt1 for SP)
  if volume.is_a?(Components::Volume) && effective_part.is_a?(Components::Part)
    # CSM series: v#n# notation
    result += " #{volume}#{effective_part}"
  elsif effective_part.is_a?(Components::Part)
    # SP and other series: use Part.type to determine format
    result += effective_part.to_s
  # Legacy: Render standalone volume (not part of v#n#)
  elsif volume && !issue_number && !effective_part
    vol_str = volume.is_a?(Components::Volume) ? volume.to_s : "v#{volume}"
    result += vol_str
  elsif volume && issue_number
    # Render volume and issue number in short form: "v6n12"
    vol_str = volume.is_a?(Components::Volume) ? volume.to_s : "v#{volume}"
    result += "#{vol_str}n#{issue_number.number}"
  end

  # Use edition component properly (e2, e2021, r5, -3)
  # NO space before edition when number present (per NIST spec)
  # Only add space for bare edition (no number case) or if original_prefix has specific format
  if edition
    if edition.original_prefix && !edition.original_prefix.empty?
      # original_prefix includes the full prefix (e.g., " Rev. " for verbose format)
      result += edition.to_s
    elsif number
      # Number present, NO space: "800-53r5"
      result += edition.to_s
    else
      # Bare edition, add space: " r5"
      result += " #{edition}"
    end
  end

  # Use version_component if available, else use version string.
  # Attach directly (no leading space) to match edition rendering
  # (e.g. "800-53r5"), so version reads "800-45ver2" not
  # "800-45 ver2".
  if version_component
    result += version_component.to_s(:short)
  elsif version
    result += "ver#{version}"
  end

  # Add supplement. NIST/NBS canonical short form is single-p "sup"
  # with the suffix attached directly, no dash (relaton-data-nist
  # uses "sup2", "sup1940", "supA"); date-range keeps its inner dash.
  # Rendered from the structured component; a present-but-empty
  # component is the bare "sup" marker.
  result += supplement_short

  # Add other attributes
  result += errata.to_s if errata
  result += "index" if index
  result += "insert" if insert
  result += "sec#{section}" if section
  result += "app" if appendix

  # Add addendum - render as " Add." suffix
  if addendum || addendum_number
    result += " Add."
  end

  # Use update_component if available, else use update string
  if update_component
    result += update_component.to_s(:short)
  elsif update
    result += "-upd#{update}"
  end

  # Add draft - render as {N}pd if draft_number present
  if draft_number
    result += " #{draft_number}pd"
  elsif draft&.to_s&.include?("draft") && !draft.to_s.include?("Draft)")
    result += "-draft"
  end

  # Add stage component (at end, before translation)
  if stage
    result += " #{stage.to_s(:short)}"
  end

  # Use translation_component if available, else use translation string
  # Note: translation_component.to_s already includes the space prefix
  if translation_component
    result += translation_component.to_s(:short)
  elsif translation
    result += " #{translation}"
  end

  result
end

#default_publisherObject

Default publisher for series without explicit publisher Subclasses can override



635
636
637
# File 'lib/pubid/nist/identifiers/base.rb', line 635

def default_publisher
  "NIST"
end

#edition_greater?(edition1, edition2) ⇒ Boolean

Helper to compare edition values numerically

Returns:

  • (Boolean)

    true if edition1 is greater than edition2



294
295
296
297
298
# File 'lib/pubid/nist/identifiers/base.rb', line 294

def edition_greater?(edition1, edition2)
  num1 = extract_edition_number(edition1)
  num2 = extract_edition_number(edition2)
  num1 && num2 && num1 > num2
end

#exclude(*args) ⇒ Object

Return a copy with the named attributes nil’d. Overrides Pubid::Identifier#exclude because NIST’s initialize is keyword-only (initialize(**attributes)) while the inherited exclude rebuilds via the positional self.class.new(attrs) form — passing a positional hash to a keyword-only initializer raises ArgumentError. Rebuild with the keyword splat instead.



169
170
171
172
173
174
175
176
177
# File 'lib/pubid/nist/identifiers/base.rb', line 169

def exclude(*args)
  excluded_args = args.dup
  excluded_args << :date if excluded_args.delete(:year)

  attrs = self.class.attributes.each_with_object({}) do |(name, _), h|
    h[name] = excluded_args.include?(name) ? nil : public_send(name)
  end
  self.class.new(**attrs)
end

#extract_edition_number(edition) ⇒ Integer?

Extract numeric value from edition (r3 -> 3, r5 -> 5, e2 -> 2)

Returns:

  • (Integer, nil)

    the edition number or nil if not extractable



302
303
304
305
306
307
308
# File 'lib/pubid/nist/identifiers/base.rb', line 302

def extract_edition_number(edition)
  # Handle both String and Edition component
  edition_str = edition.to_s
  # Match patterns like r3, r5, e2, etc.
  match = edition_str.match(/^[er]?(\d+)$/)
  match ? match[1].to_i : nil
end

#hashObject



131
132
133
134
135
136
# File 'lib/pubid/nist/identifiers/base.rb', line 131

def hash
  vals = self.class.attributes.each_key.reject do |name|
    EQUALITY_IGNORED_ATTRS.include?(name)
  end.map { |name| public_send(name) }
  [self.class, *vals].hash
end

#languageObject

Backward compatibility: language method returns translation_component This allows tests to use parsed.language instead of parsed.translation_component



211
212
213
# File 'lib/pubid/nist/identifiers/base.rb', line 211

def language
  translation_component
end

#matches?(candidate) ⇒ Boolean

Wildcard / partial-identifier match. Treats self as a QUERY pattern and candidate as a concrete document: every ID part SET on the query must equal the candidate’s, while parts left unset (nil/empty) are wildcards that match any value. So a query carrying no edition and no supplement matches that document across ALL editions, years, and supplements — the basis for “select docs by ID parts”.

Asymmetric (unlike ==): “NBS CIRC 25”.matches?(“NBS CIRC 25sup1924”) is true, but not the reverse. The candidate must be the same class or a subclass so series-level identity still holds.

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pubid/nist/identifiers/base.rb', line 148

def matches?(candidate)
  return false unless candidate.is_a?(self.class)

  self.class.attributes.each_key.all? do |name|
    next true if EQUALITY_IGNORED_ATTRS.include?(name)

    query_val = public_send(name)
    next true if query_val.nil?
    next true if query_val.is_a?(String) && query_val.empty?
    next true if query_val.is_a?(Array) && query_val.empty?

    query_val == candidate.public_send(name)
  end
end

#merge(document) ⇒ Base

Merge another document into this one Used for combining document data, preferring more specific values

Parameters:

  • document (Base)

    another NIST document to merge

Returns:

  • (Base)

    self with merged attributes



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/pubid/nist/identifiers/base.rb', line 258

def merge(document)
  return self unless document.is_a?(Base)

  attrs = self.class.attributes
  attrs.each_key do |var_name|
    next if var_name == :rendering_style
    next if var_name == :parsed_format

    current_val = public_send(var_name)
    new_val = document.public_send(var_name)
    next unless new_val

    should_merge = case var_name
                   when :publisher, :series, :number
                     true
                   when :edition
                     current_val.nil? || edition_greater?(new_val,
                                                          current_val)
                   when :volume, :part, :version, :revision
                     current_val.nil? || (new_val.to_s.length > current_val.to_s.length)
                   when :supplement, :errata, :index, :insert, :section, :appendix, :translation
                     true
                   when :year, :month, :update, :draft
                     true
                   else
                     false
                   end

    public_send(:"#{var_name}=", new_val) if should_merge
  end

  self
end

#publisher_abbreviated_nameObject



622
623
624
625
626
627
628
629
630
631
# File 'lib/pubid/nist/identifiers/base.rb', line 622

def publisher_abbreviated_name
  case publisher.to_s
  when "NBS"
    "Natl. Bur. Stand."
  when "NIST"
    "Natl. Inst. Stand. Technol."
  else
    publisher.to_s
  end
end

#publisher_full_nameObject



611
612
613
614
615
616
617
618
619
620
# File 'lib/pubid/nist/identifiers/base.rb', line 611

def publisher_full_name
  case publisher.to_s
  when "NBS"
    "National Bureau of Standards"
  when "NIST"
    "National Institute of Standards and Technology"
  else
    publisher.to_s
  end
end

#render(format: :human, **opts) ⇒ Object

Override parent render to pass NIST-specific format option through to the renderer. Pubid::Identifier#render only forwards :with_edition; NIST needs :nist_format for multi-format support.



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/pubid/nist/identifiers/base.rb', line 218

def render(format: :human, **opts)
  registry = self.class.format_registry
  unless registry
    raise ArgumentError, "No format registry configured on #{self.class}"
  end

  renderer = registry.renderer_for(format)
  unless renderer
    raise ArgumentError, "No renderer registered for format: #{format}"
  end

  renderer.new(self).render(**opts)
end

#revisionString?

Compute revision from edition component for backward compatibility

Returns:

  • (String, nil)

    revision string (e.g., “r5”) or nil



194
195
196
197
198
199
200
201
# File 'lib/pubid/nist/identifiers/base.rb', line 194

def revision
  return @revision if @revision

  # Compute from edition component if available
  if edition&.type && edition.id
    "#{edition.type}#{edition.id}"
  end
end

#series_abbreviated_nameObject



602
603
604
605
606
607
608
609
# File 'lib/pubid/nist/identifiers/base.rb', line 602

def series_abbreviated_name
  {
    "SP" => "Spec. Publ.",
    "FIPS" => "Fed. Inf. Proc. Stand.",
    "IR" => "Interag. Rep.",
    "TN" => "Tech. Note",
  }[series&.to_s || series_code] || (series&.to_s || series_code)
end

#series_codeObject

Default series_code — subclasses override to provide a normalized series name.



87
88
89
# File 'lib/pubid/nist/identifiers/base.rb', line 87

def series_code
  nil
end

#series_full_nameObject



593
594
595
596
597
598
599
600
# File 'lib/pubid/nist/identifiers/base.rb', line 593

def series_full_name
  {
    "SP" => "Special Publication",
    "FIPS" => "Federal Information Processing Standards",
    "IR" => "Interagency Report",
    "TN" => "Technical Note",
  }[series] || series
end

#supplement_shortObject

Short-form supplement fragment (“sup”, “sup1924”, “supJan1924”, “suprev”, “ supJun1925-Jun1926”), rendered from the structured component. A present-but-empty component is the bare “sup” marker; a number-less date range gets the leading space the number would have supplied. Shared by base and the per-series to_short_style overrides.



184
185
186
187
188
189
190
# File 'lib/pubid/nist/identifiers/base.rb', line 184

def supplement_short
  return "" unless supplement

  prefix = supplement.range? && !number ? " " : ""
  rendered = supplement.to_s(:short)
  prefix + (rendered.empty? ? "sup" : rendered)
end

#to_abbreviated_styleObject



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/pubid/nist/identifiers/base.rb', line 346

def to_abbreviated_style
  # "Natl. Inst. Stand. Technol. Spec. Publ. 800-57 Part 1, Revision 4"
  result = publisher_abbreviated_name
  result += " #{series_abbreviated_name}" if series
  result += " #{number}" if number
  result += " Part #{parts.first}" if parts&.any?

  # NEW: Use edition component properly
  result += " #{edition.to_s(:abbrev)}" if edition

  result += ", Revision #{revision}" if revision

  # V2: Use version_component
  result += " #{version_component.to_s(:abbrev)}" if version_component

  # V2: Use update_component
  result += " #{update_component.to_s(:abbrev)}" if update_component

  # V2: Use stage
  result += " #{stage.to_s(:abbrev)}" if stage

  # V2: Use translation_component
  result += ", #{translation_component.to_s(:abbrev)}" if translation_component

  result
end

#to_full_styleObject



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/pubid/nist/identifiers/base.rb', line 312

def to_full_style
  # "National Institute of Standards and Technology Special Publication 800-27, Revision A"
  result = publisher_full_name
  result += " #{series_full_name}" if series
  result += " #{number.value}" if number
  result += parts.map { |p| "-#{p}" }.join if parts&.any?

  # Render volume and issue number in long form: "Vol. 6, No. 12"
  if volume && issue_number
    result += " Vol. #{volume}, #{issue_number.to_s(:long)}"
  elsif volume
    result += " Vol. #{volume}"
  end

  # NEW: Use edition component properly
  result += " #{edition.to_s(:long)}" if edition

  result += ", Revision #{revision.sub(/^r/, '')}" if revision

  # V2: Use version_component
  result += " #{version_component.to_s(:long)}" if version_component

  # V2: Use update_component
  result += " #{update_component.to_s(:long)}" if update_component

  # V2: Use stage
  result += " #{stage.to_s(:long)}" if stage

  # V2: Use translation_component (already includes space)
  result += translation_component.to_s(:long) if translation_component

  result
end

#to_mr_styleObject



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/pubid/nist/identifiers/base.rb', line 526

def to_mr_style
  # "NIST.SP.800-116r1.ipd" (machine-readable with dots)
  result = (publisher || "NIST").to_s

  # Determine effective series - PREFER series_code if subclass defines it
  # This allows normalization (e.g., LCIRC → LC in LetterCircular)
  effective_series = if series_code
                       series_code
                     elsif series
                       series.to_s
                     end

  result += ".#{effective_series}" if effective_series
  result += ".#{number}" if number
  result += parts.map { |p| "-#{p}" }.join if parts&.any?

  result += append_mr_components

  result
end

#to_s(format = nil) ⇒ Object

Generate identifier string in specified format

Parameters:

  • format (:full, :long, :abbreviated, :short, :mr) (defaults to: nil)

    output format



234
235
236
237
238
239
240
241
242
# File 'lib/pubid/nist/identifiers/base.rb', line 234

def to_s(format = nil)
  # Handle both keyword argument (hash) and positional argument (symbol/string)
  format = format[:format] if format.is_a?(Hash)

  # Default to parsed_format if available (preserves input format on round-trip)
  # Falls back to :short format for output (normalization)
  # Explicit format parameter always overrides parsed_format
  render(format: :human, nist_format: format || parsed_format&.to_sym || :short)
end

#to_short_styleObject



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/pubid/nist/identifiers/base.rb', line 373

def to_short_style
  # "SP 800-187" or "NIST SP 800-187" - handle compound series properly
  result = ""

  # Determine effective publisher
  # Only show publisher if it was explicitly parsed (either directly or from series prefix)
  effective_publisher = if publisher && publisher_was_parsed
                          # Publisher was in input (either as separate field or extracted from "NBS CS" series)
                          publisher.to_s
                        else
                          # No publisher in input, don't show default
                          nil
                        end

  # Determine effective series - PREFER series_code if subclass defines it
  # This allows normalization (e.g., LCIRC → LC in LetterCircular)
  effective_series = if series_code
                       series_code
                     elsif series
                       series.to_s
                     end

  # Special handling for compound series that include publisher prefix
  # If series starts with "NBS " (like "NBS CIRC"), use it as-is
  if effective_series&.start_with?("NBS ")
    result += effective_series
  elsif effective_publisher && effective_series
    result += "#{effective_publisher} #{effective_series}"
  elsif effective_series && publisher_was_parsed
    # Only add "NIST" prefix if publisher was explicitly in the input
    result += "NIST #{effective_series}"
  elsif effective_series
    # No publisher in input, just show series without prefix
    result += effective_series
  end

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

  # Append every optional component the parser may have attached
  # (volume, part, edition, version, supplement, update, stage, ...).
  # Shared with the lossy subclasses so they cannot silently drop a
  # distinguishing component and collide with another document.
  result += append_short_components

  result
end

#translationObject

Backward compatibility: translation method returns translation_component This allows tests to use parsed.translation.language instead of parsed.translation_component.language



205
206
207
# File 'lib/pubid/nist/identifiers/base.rb', line 205

def translation
  translation_component
end

#weightInteger

Returns weight based on amount of defined attributes Used for ranking identifiers by specificity for conflict resolution

Returns:

  • (Integer)

    weight score (higher = more specific)



247
248
249
250
251
252
# File 'lib/pubid/nist/identifiers/base.rb', line 247

def weight
  self.class.attributes.keys.inject(0) do |sum, key|
    val = public_send(key)
    val && !val.to_s.empty? ? sum + 1 : sum
  end
end