Class: Pubid::Jcgm::Builder

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

Instance Attribute Summary

Attributes inherited from Builder::Base

#identifier

Instance Method Summary collapse

Methods inherited from Builder::Base

#initialize

Constructor Details

This class inherits a constructor from Pubid::Builder::Base

Instance Method Details

#build(parsed_hash) ⇒ Object



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
# File 'lib/pubid/jcgm/builder.rb', line 27

def build(parsed_hash)
  klass = locate_identifier_klass(parsed_hash)

  # GUM guides store their part number in the shared `number` attribute
  # ("JCGM GUM-6" -> number "6"); the parser tags it :gum_number only to
  # pick this class. Fold it back onto :number before assigning.
  if parsed_hash[:gum_number] && klass == Identifiers::GumGuide
    parsed_hash = parsed_hash.merge(number: parsed_hash[:gum_number])
    parsed_hash.delete(:gum_number)
  end

  identifier = klass.new
  assign_attributes(identifier, parsed_hash)

  # Bare guides/gum-guides carry no parsed stage; derive the (single,
  # published) typed_stage from the concrete class so parse and from_hash
  # agree. Avoids the ambiguous locate_stage("") lookup (Guide and
  # GumGuide both register abbr: [""]).
  if identifier.class.attributes.key?(:typed_stage) && identifier.typed_stage.nil?
    identifier.typed_stage = identifier.class.published_typed_stage ||
      raise(ArgumentError, "No published typed_stage for #{identifier.class}")
  end

  identifier
end

#locate_identifier_klass(parsed_hash) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pubid/jcgm/builder.rb', line 6

def locate_identifier_klass(parsed_hash)
  if parsed_hash[:base]
    type_with_stage = parsed_hash[:type_with_stage]
    return Jcgm.locate_type(:amendment) unless type_with_stage

    return Jcgm.locate_type(locate_typed_stage(type_with_stage).type_code)
  end

  if parsed_hash[:gum_number]
    return Jcgm.locate_type(:gum_guide)
  end

  # Standard guide (no type_with_stage, no gum_number).
  # Don't use locate_stage("") — Guide and GumGuide both have
  # abbr: [""], so the result depends on constant iteration order.
  return Jcgm.locate_type(:guide) unless parsed_hash[:type_with_stage]

  typed_stage = locate_typed_stage(parsed_hash[:type_with_stage])
  Jcgm.locate_type(typed_stage.type_code)
end