Class: Pubid::Csa::Identifiers::Bundled

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/pubid/csa/identifiers/bundled.rb

Instance Method Summary collapse

Instance Method Details

#to_sObject



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pubid/csa/identifiers/bundled.rb', line 17

def to_s
  # For Cec identifiers, use normalized form (code instead of cec_part + NO.)
  # This is used for "normalized form" rendering in bundled identifiers
  if base.is_a?(Cec)
    # Render base with normalized code format
    prefix = base.publisher_prefix || "CSA"
    needs_space = !prefix.end_with?("-")
    # Convert 4-digit year back to 2-digit for display
    year_display = if base.year
                     year_str = base.year.to_s
                     if year_str.length == 4 && year_str.start_with?("20")
                       year_str[2..3]
                     else
                       year_str
                     end
                   end
    base_str = (needs_space ? "#{prefix} " : prefix) +
      base.code.value.to_s + # Use normalized code (e.g., "C22.2-1")
      ":#{year_display}"
    parts = [base_str]
  else
    # Standard rendering for other identifier types
    parts = [base.to_s]
  end

  # Add bundled amendments/supplements (without CSA prefix)
  if bundled_with && !bundled_with.empty?
    bundled_with.each do |bundled|
      # For Cec identifiers, use normalized code format
      if bundled.is_a?(Cec)
        bundled_part = bundled.code.value.to_s # Normalized code (e.g., "C22.2-2")
        if bundled.year
          # Use dash if year_format is dash, otherwise colon
          separator = bundled.year_format == "dash" ? "-" : ":"
          bundled_part += separator
          bundled_part += bundled.year_prefix if bundled.year_prefix # Add M or F prefix
          bundled_part += "F" if bundled.french && bundled.year_format != "dash" && !bundled.year_prefix
          # Convert 4-digit year back to 2-digit
          year_str = bundled.year.to_s
          bundled_part += if year_str.length == 4 && year_str.start_with?("20")
                            year_str[2..3]
                          else
                            year_str
                          end
        end
      else
        # Standard rendering for other identifier types
        bundled_part = ""
        bundled_part += bundled.code.to_s if bundled.code

        if bundled.year
          # Use dash if year_format is dash, otherwise colon
          separator = bundled.year_format == "dash" ? "-" : ":"
          bundled_part += separator
          bundled_part += bundled.year_prefix if bundled.year_prefix # Add M or F prefix
          bundled_part += "F" if bundled.french && bundled.year_format != "dash" && !bundled.year_prefix # Only add F if no prefix
          # Convert 4-digit year back to 2-digit
          # Use original_year_4digit flag to determine original format
          year_str = bundled.year.to_s
          bundled_part += if bundled.class.attributes.key?(:original_year_4digit) && bundled.original_year_4digit
                            # Preserve 4-digit format
                            year_str
                          elsif year_str.length == 4 && year_str.start_with?("20")
                            # 2000s → convert to 2-digit
                            year_str[2..3]
                          elsif year_str.length == 4 && year_str.start_with?("19") && bundled.class.attributes.key?(:original_year_4digit) && !bundled.original_year_4digit
                            # 1900s with no M/F prefix, but original was 2-digit (e.g., bundled identifier)
                            # Convert back to 2-digit
                            year_str[2..3]
                          else
                            # Other formats - keep as-is
                            year_str
                          end
        end
      end

      parts << bundled_part
    end
  end

  result = parts.join(" + ")

  # Reaffirmation - preserve original format and determine spacing
  if reaffirmation
    # For bundled identifiers, check year format from base identifier
    # to determine spacing
    year_was_2digit = base.class.attributes.key?(:original_year_4digit) && !base.original_year_4digit

    # Check if reaffirmation was originally 4-digit
    # Note: We need to track this at the Bundled level, but for now
    # assume 4-digit if the value is 4 digits and starts with 19/20
    reaffirmation_was_4digit = reaffirmation.to_s.length == 4 &&
      reaffirmation.to_s.start_with?("19", "20")

    # Determine spacing based on original formats
    # Space needed if year is 2-digit and reaffirmation is 4-digit
    result += if year_was_2digit && reaffirmation_was_4digit
                " (R#{reaffirmation})"
              else
                "(R#{reaffirmation})"
              end
  end

  result
end