Class: Pubid::Cie::Components::Code

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

Overview

Code component for CIE identifiers Handles number-part-iteration with style awareness

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(code_str, style: :current) ⇒ Object

Parse code string with style detection



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pubid/cie/components/code.rb', line 48

def self.parse(code_str, style: :current)
  return nil if code_str.nil? || code_str.strip.empty?

  # Detect iteration format (dot separator)
  if code_str.include?(".")
    parts = code_str.split(".")
    new(number: parts[0], iteration: parts[1], style: style)
  # Detect part with separator
  elsif code_str.include?("/")
    parts = code_str.split("/")
    new(number: parts[0], part: parts[1], style: "legacy",
        part_separator: "slash")
  elsif code_str.include?("-")
    parts = code_str.split("-", 2) # Limit to 2 parts
    new(number: parts[0], part: parts[1], style: "current",
        part_separator: "dash")
  # Simple number
  else
    new(number: code_str, style: style)
  end
end

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
# File 'lib/pubid/cie/components/code.rb', line 17

def to_s
  result = number

  # Handle part AND iteration together (e.g., "19/2.1")
  if part && iteration
    # Use slash for part, then dot for iteration
    result += "/#{part}.#{iteration}"
  # Handle iteration only (e.g., "006.1", "13.3")
  elsif iteration
    result += ".#{iteration}"
  # Handle part only (separator depends on part_separator or style)
  elsif part
    # Use part_separator if explicitly set (preserves original)
    # Otherwise fall back to style-based separator
    separator = case part_separator
                when "slash"
                  "/"
                when "dash"
                  "-"
                else
                  # Legacy: slash "/" (e.g., "170/1")
                  # Current: dash "-" (e.g., "170-1")
                  style == "legacy" ? "/" : "-"
                end
    result += "#{separator}#{part}"
  end

  result
end