Class: Pubid::Nist::Components::Supplement

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/pubid/nist/components/supplement.rb

Overview

Supplement component for NIST publications Represents supplement notation with number, year, month, or revision

Examples:

Supplement.new(number: "2").to_s(:short)              # => "sup2"
Supplement.new(year: "1925").to_s(:short)              # => "sup1925"
Supplement.new(number: "3", year: "1926").to_s(:short) # => "sup3/1926"
Supplement.new(month: "Jan", year: "1924").to_s(:short) # => "supJan1924"
Supplement.new(has_revision: true).to_s(:short)        # => "suprev"

Constant Summary collapse

IDENTITY_FIELDS =

Value equality over the isolated parts, so a structured supplement participates correctly in Identifier#== and #matches? (two supplements with the same number/year/month/range/revision are the same).

%i[
  number year month month_end year_end has_revision suffix
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_raw(value, has_revision: false) ⇒ Object

Build a Supplement from the flat string the parser/builder produces (“1924” year, “Jan1924” month+year, “1” number, “A” suffix, “” bare). has_revision is supplied separately. Returns a present-but-empty Supplement for an empty/nil value so callers can distinguish a bare supplement (“sup”) from no supplement (nil).



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pubid/nist/components/supplement.rb', line 37

def self.from_raw(value, has_revision: false)
  supp = new
  if has_revision
    supp.has_revision = true
  elsif value.nil? || value.to_s.empty?
    # bare marker: present but no isolated parts
  elsif (m = value.to_s.match(/\A([A-Za-z]{3,9})(\d{4})\z/))
    supp.month = m[1]
    supp.year = m[2]
  elsif value.to_s.match?(/\A(?:18|19|20)\d{2}\z/)
    supp.year = value.to_s
  elsif value.to_s.match?(/\A\d+\z/)
    supp.number = value.to_s
  else
    supp.suffix = value.to_s
  end
  supp
end

Instance Method Details

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



99
100
101
102
103
# File 'lib/pubid/nist/components/supplement.rb', line 99

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

  IDENTITY_FIELDS.all? { |f| send(f) == other.send(f) }
end

#hashObject



107
108
109
# File 'lib/pubid/nist/components/supplement.rb', line 107

def hash
  [self.class, *IDENTITY_FIELDS.map { |f| send(f) }].hash
end

#range?Boolean

True when this supplement is a date range (start reuses month/year).

Returns:

  • (Boolean)


74
75
76
# File 'lib/pubid/nist/components/supplement.rb', line 74

def range?
  date_range?
end

#to_s(format = :short) ⇒ String

Render supplement in specified format

Parameters:

  • format (:short, :mr, :long) (defaults to: :short)

    The output format

Returns:

  • (String)

    The formatted supplement representation



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pubid/nist/components/supplement.rb', line 59

def to_s(format = :short)
  return "" if number.nil? && year.nil? && month.nil? && !has_revision &&
    suffix.nil? && month_end.nil? && year_end.nil?

  case format
  when :short, :mr
    build_short_format
  when :long
    build_long_format
  else
    build_short_format
  end
end

#value_stringObject

The text after the “sup” marker for the simple (non-range, non-rev) forms: “1924” / “Jan1924” / “1” / “A”; “” for a bare supplement. Used by URN rendering, which handles range/revision separately.



81
82
83
84
85
86
87
88
89
90
# File 'lib/pubid/nist/components/supplement.rb', line 81

def value_string
  return "" if has_revision || date_range?
  return suffix.to_s if suffix
  return "#{month}#{year}" if month && year
  return "#{number}/#{year}" if number && year
  return year.to_s if year
  return number.to_s if number

  ""
end