Class: Pubid::Iec::Base

Inherits:
Core::Identifier::Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pubid/iec/identifier/base.rb

Constant Summary collapse

PROJECT_STAGES =
{}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher: "IEC", vap: nil, database: nil, fragment: nil, version: nil, decision_sheet: nil, conjuction_part: nil, part_version: nil, trf_publisher: nil, trf_series: nil, trf_version: nil, test_type: nil, edition: nil, type: nil, month: nil, day: nil, language: nil, stage: nil, sheet: nil, **args) ⇒ Base

Returns a new instance of Base.

Parameters:

  • stage (String) (defaults to: nil)

    stage eg. “PWI”, “PNW”



15
16
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
# File 'lib/pubid/iec/identifier/base.rb', line 15

def initialize(publisher: "IEC", vap: nil, database: nil,
               fragment: nil, version: nil, decision_sheet: nil,
               conjuction_part: nil, part_version: nil, trf_publisher: nil,
               trf_series: nil, trf_version: nil, test_type: nil,
               edition: nil, type: nil, month: nil, day: nil,
               language: nil, stage: nil, sheet: nil, **args)

  @vap = vap.to_s if vap
  @database = database if database
  @fragment = fragment if fragment
  @version = version if version
  @decision_sheet = decision_sheet if decision_sheet
  @conjuction_part = conjuction_part if conjuction_part
  @part_version = part_version if part_version
  @trf_publisher = trf_publisher.to_s if trf_publisher
  @trf_series = trf_series if trf_series
  @trf_version = trf_version.to_s if trf_version
  @test_type = test_type if test_type
  @edition = edition.to_s if edition
  @month = month if month
  @day = day if day
  @language = language if language
  @sheet = sheet if sheet

  if stage
    @stage = self.class.has_project_stage?(stage) ? self.class.resolve_project_stage(stage) : resolve_stage(stage)
  end

  super(**args.merge(publisher: publisher))
end

Instance Attribute Details

#conjuction_partObject

Returns the value of attribute conjuction_part.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def conjuction_part
  @conjuction_part
end

#databaseObject

Returns the value of attribute database.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def database
  @database
end

#dayObject

Returns the value of attribute day.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def day
  @day
end

#decision_sheetObject

Returns the value of attribute decision_sheet.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def decision_sheet
  @decision_sheet
end

#fragmentObject

Returns the value of attribute fragment.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def fragment
  @fragment
end

#monthObject

Returns the value of attribute month.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def month
  @month
end

#part_versionObject

Returns the value of attribute part_version.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def part_version
  @part_version
end

#sheetObject

Returns the value of attribute sheet.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def sheet
  @sheet
end

#test_typeObject

Returns the value of attribute test_type.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def test_type
  @test_type
end

#trf_publisherObject

Returns the value of attribute trf_publisher.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def trf_publisher
  @trf_publisher
end

#trf_seriesObject

Returns the value of attribute trf_series.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def trf_series
  @trf_series
end

#trf_versionObject

Returns the value of attribute trf_version.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def trf_version
  @trf_version
end

#vapObject

Returns the value of attribute vap.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def vap
  @vap
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/pubid/iec/identifier/base.rb', line 6

def version
  @version
end

Class Method Details

.get_amendment_classObject



120
121
122
# File 'lib/pubid/iec/identifier/base.rb', line 120

def get_amendment_class
  Amendment
end

.get_corrigendum_classObject



124
125
126
# File 'lib/pubid/iec/identifier/base.rb', line 124

def get_corrigendum_class
  Corrigendum
end

.get_identifierObject



144
145
146
# File 'lib/pubid/iec/identifier/base.rb', line 144

def get_identifier
  Identifier
end

.get_parser_classObject



128
129
130
# File 'lib/pubid/iec/identifier/base.rb', line 128

def get_parser_class
  Parser
end

.get_renderer_classObject



132
133
134
# File 'lib/pubid/iec/identifier/base.rb', line 132

def get_renderer_class
  Renderer::Pubid
end

.get_transformer_classObject



136
137
138
# File 'lib/pubid/iec/identifier/base.rb', line 136

def get_transformer_class
  Transformer
end

.get_update_codesObject



140
141
142
# File 'lib/pubid/iec/identifier/base.rb', line 140

def get_update_codes
  UPDATE_CODES
end

.has_project_stage?(project_stage) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/pubid/iec/identifier/base.rb', line 76

def has_project_stage?(project_stage)
  self::PROJECT_STAGES.any? { |k, v| v[:abbr] == project_stage }
end

.has_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
# File 'lib/pubid/iec/identifier/base.rb', line 80

def has_type?(type)
  return type == self.type[:key] if type.is_a?(Symbol)

  if self.type.key?(:short)
    self.type[:short].is_a?(Array) ? self.type[:short].include?(type) : self.type[:short] == type
  else
    type.to_s.downcase.to_sym == self.type[:key]
  end
end

.resolve_project_stage(project_stage) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/pubid/iec/identifier/base.rb', line 90

def resolve_project_stage(project_stage)
  stage = self::PROJECT_STAGES.find do |k, v|
    v[:abbr] == project_stage
  end

  Identifier.build_project_stage(abbr: stage[1][:abbr], harmonized_code: stage[1][:harmonized_stages])
end

.transform(params) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pubid/iec/identifier/base.rb', line 104

def transform(params)
  identifier_params = transform_hash(params)

  if identifier_params[:interpretation_sheet]
    return Identifier.create(
      type: :ish,
      base: Identifier.create(
        **identifier_params.dup.tap { |h| h.delete(:interpretation_sheet) }),
      number: identifier_params[:interpretation_sheet][:number],
      year: identifier_params[:interpretation_sheet][:year]
    )
  end

  Identifier.create(**identifier_params)
end

.transform_hash(params) ⇒ Object



98
99
100
101
102
# File 'lib/pubid/iec/identifier/base.rb', line 98

def transform_hash(params)
  params.map do |k, v|
    get_transformer_class.new.apply(k => v.is_a?(Hash) ? transform_hash(v) : v)
  end.inject({}, :merge)
end

Instance Method Details

#lookup_typed_stage(lookup_code) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/pubid/iec/identifier/base.rb', line 50

def lookup_typed_stage(lookup_code)
  self.class::TYPED_STAGES.each do |abbr, stage|
    if stage[:harmonized_stages] & lookup_code == lookup_code
      return Identifier.build_typed_stage(abbr: abbr, harmonized_code: lookup_code)
    end
  end

  nil
end

#renderer_dataObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/pubid/iec/identifier/base.rb', line 60

def renderer_data
  values = to_h(deep: false)

  # convert project stage to typed stage
  if values.key?(:stage) && values[:stage].is_a?(TypedProjectStage)
    values[:stage] = lookup_typed_stage(values[:stage].harmonized_code.stages) ||
      resolve_stage(values[:stage].harmonized_code.stages.first)
  end
  values
end

#to_s(with_edition_month_date: false) ⇒ Object



71
72
73
# File 'lib/pubid/iec/identifier/base.rb', line 71

def to_s(with_edition_month_date: false)
  self.class.get_renderer_class.new(renderer_data).render(with_edition_month_date: with_edition_month_date)
end

#urnObject



46
47
48
# File 'lib/pubid/iec/identifier/base.rb', line 46

def urn
  Renderer::Urn.new(renderer_data).render
end