Class: Pubid::Itu::Scheme
Constant Summary collapse
- ATTRIBUTE_MAPPINGS =
Attribute mappings define how parsed data maps to model attributes
{ sector: :sector, series: :series, number: :number, subseries: :subseries, part: :part, supplement: :supplement, amendment: :amendment, annex: :annex, corrigendum: :corrigendum, addendum: :addendum, appendix: :appendix, date: :date, month: :month, year: :year, second_number: :second_number, range_series: :range_series, range_number: :range_number, sg_number: :sg_number, }.freeze
Instance Attribute Summary
Attributes inherited from Scheme
#identifiers, #languages, #publishers, #stages, #supplement_identifiers, #types
Class Method Summary collapse
-
.model_class ⇒ Object
Model class to use for rendering.
-
.roman_to_arabic(roman) ⇒ Object
Convert Roman numerals to Arabic numbers.
-
.transform(parsed) ⇒ Object
Convert parsed data to model attributes.
Methods inherited from Scheme
#all_identifier_classes, #all_typed_stages, #configure, #identifier_class_index, #initialize, #locate_identifier_klass_by_type_code, #locate_typed_stage_by_abbr, #locate_typed_stage_by_harmonized_code, #locate_typed_stage_by_stage_code, #supplement_typed_stages, #typed_stage_index, #typed_stages
Constructor Details
This class inherits a constructor from Pubid::Scheme
Class Method Details
.model_class ⇒ Object
Model class to use for rendering
31 32 33 |
# File 'lib/pubid/itu/scheme.rb', line 31 def self.model_class Model end |
.roman_to_arabic(roman) ⇒ Object
Convert Roman numerals to Arabic numbers
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/pubid/itu/scheme.rb', line 148 def self.roman_to_arabic(roman) return roman unless roman.match?(/^[IVXLCDM]+$/i) roman_map = { "I" => 1, "V" => 5, "X" => 10, "L" => 50, "C" => 100, "D" => 500, "M" => 1000 } roman = roman.upcase result = 0 prev_value = 0 roman.reverse.each_char do |char| value = roman_map[char] if value < prev_value result -= value else result += value end prev_value = value end result.to_s end |
.transform(parsed) ⇒ Object
Convert parsed data to model attributes
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/pubid/itu/scheme.rb', line 36 def self.transform(parsed) attributes = {} # Extract sector attributes[:sector] = parsed[:sector].to_s if parsed[:sector] # Determine content based on sector content = parsed[:t_content] || parsed[:r_content] || {} # Extract series and study group if content[:series].is_a?(Hash) && content[:series][:sg_number] # Study groups (questions) attributes[:series] = "SG#{content[:series][:sg_number]}" elsif content[:series] attributes[:series] = content[:series].to_s end # Extract numbering components if content[:numbering] numbering = content[:numbering] # Handle numbering as array (e.g., number + subseries) if numbering.is_a?(Array) numbering.each do |part| if part.is_a?(Hash) if part[:number] attributes[:number] = part[:number].to_s elsif part[:subseries] # Collect subseries parts if attributes[:subseries] attributes[:subseries] = "#{attributes[:subseries]}.#{part[:subseries]}" else attributes[:subseries] = part[:subseries].to_s end elsif part[:part] attributes[:part] = part[:part].to_s end end end elsif numbering.is_a?(Hash) # Handle numbering as hash attributes[:number] = numbering[:number].to_s if numbering[:number] if numbering[:subseries] attributes[:subseries] = numbering[:subseries].to_s end attributes[:part] = numbering[:part].to_s if numbering[:part] end end # Extract supplement/amendment/etc. if content[:supplement] attributes[:supplement] = content[:supplement].to_s end attributes[:amendment] = content[:amendment].to_s if content[:amendment] attributes[:annex] = content[:annex].to_s if content[:annex] if content[:corrigendum] attributes[:corrigendum] = content[:corrigendum].to_s end attributes[:addendum] = content[:addendum].to_s if content[:addendum] # Handle appendix (convert Roman numerals to Arabic) if content[:appendix] appendix = content[:appendix].to_s attributes[:appendix] = roman_to_arabic(appendix) || appendix end # Extract date if content[:date] date = content[:date] if date[:month] && date[:year] attributes[:month] = date[:month].to_s attributes[:year] = date[:year].to_s elsif date[:year] attributes[:year] = date[:year].to_s end end # Extract second number (for combined standards like G.780/Y.1351) if content[:second_number] second = content[:second_number] second_attrs = {} second_attrs[:series] = second[:series].to_s if second[:series] if second[:numbering] numbering = second[:numbering] if numbering[:number] second_attrs[:number] = numbering[:number].to_s end if numbering[:subseries] second_attrs[:subseries] = numbering[:subseries].to_s end second_attrs[:part] = numbering[:part].to_s if numbering[:part] end attributes[:second_number] = second_attrs unless second_attrs.empty? end # Extract range (for Q.400-Q.490 style) if content[:range_series] && content[:range_number] attributes[:range] = { series: content[:range_series].to_s, number: content[:range_number].to_s, } end attributes end |