Class: Pubid::Iec::Builder

Inherits:
Builder::Base show all
Defined in:
lib/pubid/iec/builder.rb

Instance Attribute Summary

Attributes inherited from Builder::Base

#identifier

Instance Method Summary collapse

Constructor Details

#initialize(scheme) ⇒ Builder

Returns a new instance of Builder.



9
10
11
# File 'lib/pubid/iec/builder.rb', line 9

def initialize(scheme)
  @scheme = scheme
end

Instance Method Details

#build(parsed_hash) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/pubid/iec/builder.rb', line 33

def build(parsed_hash)
  # Handle sheet_supplement_identifier pattern:
  # {base_identifier: {...}, sheet_number: ..., sheet_year: ..., type_with_stage: "COR", number_with_part: "1", date: "1995"}
  # This is a Corrigendum/Amendment wrapping a SheetIdentifier
  if parsed_hash[:base_identifier] && (parsed_hash[:sheet_number] || parsed_hash[:sheet_year]) && parsed_hash[:type_with_stage]
    # Extract sheet info first
    sheet_number = parsed_hash.delete(:sheet_number)
    sheet_year = parsed_hash.delete(:sheet_year)

    # Build the base identifier (IEC 60695-2-1 with /1:1994 -> SheetIdentifier)
    base_parsed = parsed_hash[:base_identifier].dup
    base_parsed[:sheet_number] = sheet_number
    base_parsed[:sheet_year] = sheet_year if sheet_year
    sheet_id = wrap_with_sheet(build(base_parsed), sheet_number,
                               sheet_year)

    # Now build the supplement on top of the sheet
    # We need to preserve the supplement data and create a wrapper
    supplement_type = parsed_hash.delete(:type_with_stage)
    supplement_number = parsed_hash.delete(:number_with_part)
    supplement_date = parsed_hash.delete(:date)
    supplement_edition = parsed_hash.delete(:edition)

    # Locate the supplement identifier class
    typed_stage = locate_typed_stage(supplement_type)
    identifier_class = @scheme.locate_identifier_klass_by_type_code(typed_stage.type_code)

    # Create the supplement identifier
    supplement = identifier_class.new
    supp_attrs = identifier_class.attributes
    if supp_attrs.key?(:number)
      supplement.number = Components::Code.new(value: supplement_number.to_s)
    end
    if supplement_date && supp_attrs.key?(:date)
      supplement.date = cast(:date, supplement_date)
    end
    if supplement_edition && supp_attrs.key?(:edition)
      supplement.edition = cast(:edition, supplement_edition)
    end
    supplement.typed_stage = typed_stage

    # Wrap the sheet with the supplement
    return wrap_with_supplement(sheet_id, supplement)
  end

  # Handle sheet_identifier pattern: {base_identifier: {...}, sheet_number: ..., sheet_year: ...}
  if parsed_hash[:base_identifier] && (parsed_hash[:sheet_number] || parsed_hash[:sheet_year])
    # Build the base identifier first
    base_id = build(parsed_hash[:base_identifier])
    # Wrap with SheetIdentifier
    sheet_number = parsed_hash.delete(:sheet_number)
    sheet_year = parsed_hash.delete(:sheet_year)
    return wrap_with_sheet(base_id, sheet_number, sheet_year)
  end

  # Extract FRAG/FRAGC indicator if present
  frag_abbrs = Pubid::Iec::Identifiers::FragmentIdentifier::TYPED_STAGES
    .flat_map(&:abbr)
  is_fragment = frag_abbrs.include?(parsed_hash[:type_with_stage]) ||
    parsed_hash[:type_with_stage] == "FRAGC"
  fragment_typed_stage = nil
  fragment_number = nil
  fragment_edition = nil

  if is_fragment
    # For FRAG typed stages, look up the fragment's own typed stage
    begin
      fragment_typed_stage = @scheme.locate_typed_stage_by_abbr(
        parsed_hash[:type_with_stage].to_s,
      )
    rescue ArgumentError
      # FRAGC is a rendering notation, not a typed stage abbreviation
      fragment_typed_stage = @scheme.locate_typed_stage_by_abbr("FRAG")
    end

    # For FRAG/FRAGC, we need to build the base Amendment/Corrigendum
    # then wrap it with FragmentIdentifier
    if parsed_hash[:number_with_part]
      fragment_number = parsed_hash[:number_with_part].to_s
    end

    # Extract edition for fragment
    fragment_edition = parsed_hash.delete(:edition)

    # Build the base identifier (Amendment/Corrigendum) directly
    if parsed_hash[:base_identifier]
      base_id = build(parsed_hash[:base_identifier])
      return wrap_with_fragment(base_id, fragment_number,
                                fragment_edition, fragment_typed_stage)
    end
  end

  # Check for TRF with CISPR - build embedded identifier
  trf_org = parsed_hash.delete(:trf_org)
  if trf_org && parsed_hash[:number_with_part]
    # Build CISPR identifier without year
    cispr_number = parsed_hash.delete(:number_with_part)
    cispr_id = Identifiers::InternationalStandard.new(
      publisher: Components::Publisher.new(body: "CISPR"),
    )
    # Set number via cast
    number_components = cast(:number_with_part, cispr_number)
    attrs = cispr_id.class.attributes
    number_components.each_pair do |k, v|
      cispr_id.public_send("#{k}=", v) if attrs.key?(k.to_sym)
    end
    parsed_hash[:cispr_identifier] = cispr_id
  end

  # Extract and store wrapper data before building
  consolidated_supplements_data = parsed_hash.delete(:consolidated_supplements)
  vap_suffix_data = parsed_hash.delete(:vap_suffix)
  # Note: sheet_number and sheet_year are now handled in sheet_identifier check above
  fragment_type_data = parsed_hash.delete(:fragment_type)
  fragment_number_data = parsed_hash.delete(:fragment_number)

  # Instantiate the identifier based on the typed stage
  identifier = locate_identifier_klass(parsed_hash).new

  # For French GUIDE entries: "Guide IEC 51:1999"
  if type_with_stage_fr = parsed_hash.delete(:type_with_stage_fr)
    parsed_hash[:type_with_stage] = type_with_stage_fr
  end

  assign_attributes(identifier, parsed_hash)

  # Detect rendering style from parsed abbreviation
  if identifier.class.attributes.key?(:rendering_style) && identifier.typed_stage
    require_relative "rendering_style"
    ts = identifier.typed_stage

    # Detect IEC format from parsed abbreviation
    # IEC uses space to indicate long form: "Amd 1", "Cor 1"
    # No space indicates short form: "AMD1", "COR1"
    stage_format_long = if ts.long_abbr && ts.original_abbr&.include?(" ")
                          true # Long form has space: "Amd 1", "Cor 1"
                        elsif ts.short_abbr && ts.original_abbr && !ts.original_abbr.include?(" ")
                          false  # Short form: "AMD1", "COR1", "CDV", "FDIS"
                        else
                          false  # Default to short/canonical
                        end

    # Detect language code format from parsed languages
    langs = if identifier.class.attributes.key?(:languages)
              identifier.languages
            end
    with_language_code = if langs&.any?
                           # Check if original_code was single-char (E, F, R, A, S, D)
                           first_lang = langs.first
                           if first_lang.is_a?(Components::Language) && first_lang.original_code && first_lang.original_code.length == 1
                             :single
                           else
                             :iso # 2-char codes (en, fr, ru, ar, es, de)
                           end
                         else
                           :none
                         end

    # with_date is always true for base identifiers (show the date if present)
    # Only supplements might have undated references
    with_date = true

    # Create custom rendering style based on parsed format
    identifier.rendering_style = RenderingStyle.new(
      with_language_code: with_language_code,
      stage_format_long: stage_format_long,
      with_date: with_date,
    )
  end

  # After building base identifier, apply wrappers
  if fragment_type_data && fragment_number_data
    frag_ts = @scheme.locate_typed_stage_by_abbr("FRAG")
    identifier = wrap_with_fragment(identifier, fragment_number_data.to_s,
                                    nil, frag_ts)
  end
  # Note: sheet wrapping is now handled earlier in build method for sheet_identifier pattern
  if consolidated_supplements_data
    identifier = wrap_with_consolidated(identifier,
                                        consolidated_supplements_data)
  end
  if vap_suffix_data
    identifier = wrap_with_vap(identifier,
                               vap_suffix_data)
  end

  identifier
end

#cast(type, value) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/pubid/iec/builder.rb', line 340

def cast(type, value)
  case type
  when :base_identifier
    # If it has a base identifier, we need to build a supplement
    # We assume that the base identifier is already a valid Identifier object
    build(value)

  when :publisher
    Components::Publisher.new(body: value)

  when :copublishers
    if value.nil? || value.empty?
      nil
    else
      value.map do |copublisher|
        Components::Publisher.new(body: copublisher[:copublisher])
      end
    end

  when :technical_committee, :wd_number, :wd_stage, :wd_language, :wp_stage, :wp_type
    # Working document/programme fields - just return as string
    value.to_s

  when :cispr_identifier
    # CISPR identifier object for TRF
    value

  when :number
    # Plain number for sub-org identifiers (CA, IECQ CS, IECQ OD)
    # Just return as Code component
    { number: Components::Code.new(value: value.to_s) }

  when :number_with_part
    # "60038" (no part)
    # or "60038-1" ('1' is part)
    # or "60038-1-2" ('1' is part, '2' is subpart)
    # or "29110-5-1-1" ('5' is part, '1-1' is subpart)

    # Split the number into parts
    normalized_value = value.to_s.tr(Parser::DASH_CHARS.join, "-")

    parts = normalized_value.split("-")
    number = parts.shift # The first part is always the number
    part = parts.shift # The second part is the part, if present
    subpart = parts.any? ? parts.join("-") : nil # The remaining parts form the subpart, if present

    part = convert_roman_to_integer(part)

    code_hash = { number: Components::Code.new(value: number) }

    if part
      code_hash[:part] = Components::Code.new(value: part)
    end

    if subpart
      code_hash[:subpart] = Components::Code.new(value: subpart)
    end

    code_hash

  when :type_with_stage
    # "WD"
    # "TS"
    # "Guide"
    # FRAG typed stages are handled in the is_fragment check above

    iteration = value.to_s.match(/(\d+)$/)
    value = value.to_s.sub(iteration.to_s, "")
    typed_stage = locate_typed_stage(value || "")

    ## IMPORTANT!!
    # Always use TypedStage in an Identifier or separate Type and Stage.
    {
      stage: typed_stage.to_stage,
      type: typed_stage.to_type,
      typed_stage: typed_stage,
    }
  when :stage_iteration
    # "1" or "2"
    Components::Code.new(value: value.to_s)

  when :date
    parse_date(value)

  when :edition
    Pubid::Components::Edition.new(number: value)

  when :sheet_number
    value.to_s

  when :sheet_year
    value.to_s

  when :languages
    parse_languages(value)

  when :all_parts
    Pubid::Components::Locality.new(all_parts: true)

  when :database
    # Database flag - return true if DB suffix present
    true

  # Handle joint identifiers with ISO
  when :joint_identifier
    case value[:publisher]
    when "ISO"
      require_relative "../iso/builder"
      Iso::Builder.new(Iso::Scheme).build(value)
    end

  else
    raise ArgumentError, "Unknown parameter type: #{type}"
  end
end

#convert_roman_to_integer(roman_numeral) ⇒ Object



334
335
336
337
338
# File 'lib/pubid/iec/builder.rb', line 334

def convert_roman_to_integer(roman_numeral)
  return roman_numeral if roman_numeral.to_s.upcase == "X"

  super
end

#handle_key(identifier, key, value) ⇒ Object



222
223
224
225
226
227
228
229
230
# File 'lib/pubid/iec/builder.rb', line 222

def handle_key(identifier, key, value)
  if key == :joint_identifier
    identifier.additional_identifiers ||= []
    identifier.additional_identifiers << value
    true
  else
    false
  end
end

#locate_identifier_klass(parsed_hash) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pubid/iec/builder.rb', line 13

def locate_identifier_klass(parsed_hash)
  # Check for working programme
  if parsed_hash[:wp_stage]
    require_relative "identifiers/working_document"
    return Identifiers::WorkingDocument
  end

  # Check for working document
  if parsed_hash[:technical_committee] && parsed_hash[:wd_number]
    require_relative "identifiers/working_document"
    return Identifiers::WorkingDocument
  end

  # Check the `:type_with_stage` to determine the identifier class
  # :type_with_stage will be nil if it is an IS.
  typed_stage = locate_typed_stage(parsed_hash[:type_with_stage])

  @scheme.locate_identifier_klass_by_type_code(typed_stage.type_code)
end

#wrap_with_consolidated(base_identifier, supplements_data) ⇒ Object

Wrap identifier with ConsolidatedIdentifier for +AMD chains



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/pubid/iec/builder.rb', line 274

def wrap_with_consolidated(base_identifier, supplements_data)
  require_relative "identifiers/consolidated_identifier"
  require_relative "identifiers/amendment"
  require_relative "identifiers/corrigendum"

  supplements = supplements_data.filter_map do |supp|
    type = supp[:supplement_type].to_s
    number = supp[:supplement_number].to_s
    year = supp[:supplement_year]

    # Only create Date component if year is present
    date_component = year ? Pubid::Components::Date.new(year: year.to_s) : nil

    if type == "AMD"
      Identifiers::Amendment.new(
        number: Components::Code.new(value: number),
        date: date_component,
      )
    elsif type == "COR"
      Identifiers::Corrigendum.new(
        number: Components::Code.new(value: number),
        date: date_component,
      )
    end
  end

  Identifiers::ConsolidatedIdentifier.new(
    identifiers: [base_identifier] + supplements,
  )
end

#wrap_with_fragment(base_identifier, fragment_number, edition_data = nil, typed_stage = nil) ⇒ Object

Wrap identifier with FragmentIdentifier for /FRAGN notation



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/pubid/iec/builder.rb', line 233

def wrap_with_fragment(base_identifier, fragment_number,
edition_data = nil, typed_stage = nil)
  require_relative "identifiers/fragment_identifier"

  fragment = Identifiers::FragmentIdentifier.new(
    base_identifier: base_identifier,
    fragment_number: fragment_number,
  )

  # Set typed stage if provided
  fragment.typed_stage = typed_stage if typed_stage

  # Set edition if provided
  if edition_data
    fragment.edition = cast(:edition, edition_data)
  end

  fragment
end

#wrap_with_sheet(base_identifier, sheet_number, sheet_year) ⇒ Object

Wrap identifier with SheetIdentifier for /N:YEAR notation



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/pubid/iec/builder.rb', line 254

def wrap_with_sheet(base_identifier, sheet_number, sheet_year)
  require_relative "identifiers/sheet_identifier"

  # Only pass year if it's present
  year_value = sheet_year&.to_s

  Identifiers::SheetIdentifier.new(
    base_identifier: base_identifier,
    sheet_number: sheet_number.to_s,
    year: year_value,
  )
end

#wrap_with_supplement(base_identifier, supplement) ⇒ Object

Wrap base identifier with supplement (Amendment/Corrigendum)



268
269
270
271
# File 'lib/pubid/iec/builder.rb', line 268

def wrap_with_supplement(base_identifier, supplement)
  supplement.base_identifier = base_identifier
  supplement
end

#wrap_with_vap(base_identifier, vap_suffix_data) ⇒ Object

Wrap identifier with VapIdentifier for CSV/CMV/RLV/SER suffix



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/pubid/iec/builder.rb', line 306

def wrap_with_vap(base_identifier, vap_suffix_data)
  require_relative "identifiers/vap_identifier"
  require_relative "identifiers/consolidated_identifier"
  require_relative "components/vap_suffix"

  vap_suffix = Components::VapSuffix.new(code: vap_suffix_data.to_s)

  # Extract edition - need to go deep for ConsolidatedIdentifier
  edition = nil
  if base_identifier.is_a?(Identifiers::ConsolidatedIdentifier)
    # Edition is on the first identifier (base document)
    base_first = base_identifier.identifiers.first
    if base_first && base_first.class.attributes.key?(:edition)
      edition = base_first.edition
      base_first.edition = nil
    end
  elsif base_identifier.class.attributes.key?(:edition)
    edition = base_identifier.edition
    base_identifier.edition = nil
  end

  Identifiers::VapIdentifier.new(
    base_identifier: base_identifier,
    vap_suffix: vap_suffix,
    edition: edition,
  )
end