Class: Pubid::CenCenelec::SingleIdentifier

Inherits:
Identifier
  • Object
show all
Defined in:
lib/pubid/cen_cenelec/single_identifier.rb

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

Constructor Details

This class inherits a constructor from Pubid::Identifier

Instance Method Details

#<=>(other) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pubid/cen_cenelec/single_identifier.rb', line 106

def <=>(other)
  return nil unless other.is_a?(SingleIdentifier)

  # Compare by number first
  num_cmp = number.to_s <=> other.number.to_s
  return num_cmp unless num_cmp.zero?

  # Then by part
  part_cmp = (part || Components::Code.new(value: "0")).to_s <=> (other.part || Components::Code.new(value: "0")).to_s
  return part_cmp unless part_cmp.zero?

  # Then by date
  if date && other.date
    date.to_s <=> other.date.to_s
  elsif date
    1
  elsif other.date
    -1
  else
    0
  end
end

#to_s(lang: :en, lang_single: false) ⇒ String

Generate URN for this identifier

Returns:

  • (String)

    URN representation



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pubid/cen_cenelec/single_identifier.rb', line 14

def to_s(lang: :en, lang_single: false)
  parts = []

  # Check if we have a draft stage (prEN, FprEN) - these include both stage and type
  is_draft_stage = typed_stage&.abbr && %w[prEN
                                           FprEN].include?(typed_stage.abbr.first)

  # Get type short name - for draft stages, extract base type
  type_short = if is_draft_stage
                 typed_stage.type_code.to_s.upcase # :en => "EN"
               elsif type.is_a?(Components::Type)
                 type.abbr
               elsif self.class.respond_to?(:type) && self.class.type.is_a?(Hash)
                 self.class.type[:short]
               else
                 "EN" # Default
               end

  # Track if we should use slash before type
  use_slash_before_type = false

  # For CWA/HD, they act as publisher (not EN)
  if %w[CWA HD CR].include?(type_short)
    # Stage prefix OR type as publisher
    parts << if typed_stage&.abbr && typed_stage.abbr.first != type_short
               typed_stage.abbr.first
             else
               type_short
             end
  elsif is_draft_stage
    # Draft stage prefix (prEN, FprEN) OR regular publisher
    parts << typed_stage.abbr.first
  elsif publisher
    parts << (publisher.is_a?(Components::Publisher) ? publisher.body : publisher.to_s)
    use_slash_before_type = true # When publisher present, use slash before type
  end

  # Copublishers - add to last part (publisher) with slash
  if copublishers&.any?
    copub_str = copublishers.map do |cp|
      cp.is_a?(Components::Publisher) ? cp.body : cp.to_s
    end.join("/")
    unless copub_str.empty?
      if parts.any?
        parts[-1] = "#{parts[-1]}/#{copub_str}"
      else
        parts << copub_str
      end
    end
  end

  # Type for non-EN documents (TS, TR) - but not CWA/HD or Guide
  if type_short != "EN" && !%w[CWA HD CR Guide].include?(type_short)
    if use_slash_before_type && parts.any?
      # Use slash separator for publisher/type combination (TS, TR only)
      parts << "/#{type_short}"
    else
      parts << type_short
    end
  elsif type_short == "Guide"
    # Guide uses SPACE separator, not slash
    parts << "Guide"
  end

  # Number with part (which may be multi-level like "5-1-1")
  if number
    number_str = number.is_a?(Components::Code) ? number.value.to_s : number.to_s
    if part
      part_val = part.is_a?(Components::Code) ? part.value : part
      number_str += "-#{part_val}"
    end
    parts << number_str
  end

  # Join parts - but handle slash prefix for type
  result = ""
  parts.each_with_index do |part, idx|
    if idx.positive? && !part.start_with?("/")
      result += " "
    end
    result += part
  end

  # Date
  if date
    year_val = date.is_a?(Components::Date) ? date.year : date.to_i
    result += ":#{year_val}"
  end

  result
end