Class: Pubid::Iso::Identifier::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher: "ISO", number: nil, stage: nil, iteration: nil, supplement: nil, joint_document: nil, tctype: nil, sctype: nil, wgtype: nil, tcnumber: nil, scnumber: nil, wgnumber: nil, dir: nil, dirtype: nil, year: nil, amendments: nil, corrigendums: nil, type: nil, base: nil, supplements: nil, part: nil, addendum: nil, **opts) ⇒ Base

Creates new identifier from options provided, includes options from Pubid::Core::Identifier#initialize

Parameters:

  • stage (Stage, Symbol, String) (defaults to: nil)

    stage or typed stage, e.g. “PWI”, “NP”, “50.00”, Stage.new(abbr: :WD), “DTR”

  • iteration (Integer) (defaults to: nil)

    document iteration, eg. “1”, “2”, “3”

  • joint_document (Identifier) (defaults to: nil)

    joint document

  • supplement (Supplement) (defaults to: nil)

    supplement

  • tctype (String) (defaults to: nil)

    Technical Committee type, eg. “TC”, “JTC”

  • sctype (String) (defaults to: nil)

    TC subsommittee, eg. “SC”

  • wgtype (String) (defaults to: nil)

    TC working group type, eg. “AG”, “AHG”

  • tcnumber (Integer) (defaults to: nil)

    Technical Committee number, eg. “1”, “2”

  • scnumber (Integer) (defaults to: nil)

    Subsommittee number, eg. “1”, “2”

  • wgnumber (Integer) (defaults to: nil)

    Working group number, eg. “1”, “2”

  • dirtype (String) (defaults to: nil)

    Directives document type, eg. “JTC”

  • base (Identifier) (defaults to: nil)

    base document for supplement’s identifier

  • type (nil, :tr, :ts, :amd, :cor, :guide, :dir, :tc, Type) (defaults to: nil)

    document’s type, eg. :tr, :ts, :amd, :cor, Type.new(:tr)

Raises:

See Also:



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

def initialize(publisher: "ISO", number: nil, stage: nil, iteration: nil, supplement: nil,
               joint_document: nil, tctype: nil, sctype: nil, wgtype: nil, tcnumber: nil,
               scnumber: nil, wgnumber:nil,
               dir: nil, dirtype: nil, year: nil, amendments: nil,
               corrigendums: nil, type: nil, base: nil, supplements: nil,
               part: nil, addendum: nil, **opts)
  super(**opts.merge(number: number, publisher: publisher, year: year,
                     amendments: amendments, corrigendums: corrigendums))

  if supplements
    @supplements = supplements.map do |supplement|
      if supplement.is_a?(Hash)
        self.class.get_transformer_class.new.apply(:supplements => [supplement])[:supplements].first
      else
        supplement
      end
    end
  end

  if stage
    if stage.is_a?(Pubid::Core::Stage)
      @stage = stage
      @typed_stage = resolve_typed_stage(@stage.harmonized_code) unless @stage.abbr
    elsif self.class.has_typed_stage?(stage)
      @typed_stage, @stage = find_typed_stage(stage)
    else
      @stage = Identifier.parse_stage(stage)
      # resolve typed stage when harmonized code provided as stage
      # or stage abbreviation was not resolved
      if /\A[\d.]+\z/.match?(stage) || @stage.empty_abbr?(with_prf: true)
        @typed_stage = resolve_typed_stage(@stage.harmonized_code)
      end
    end
  elsif iteration && !is_a?(Supplement)
    raise Errors::IterationWithoutStageError, "Document without stage cannot have iteration"
  end

  @iteration = iteration.to_i if iteration
  @supplement = supplement if supplement
  @joint_document = joint_document if joint_document
  @tctype = tctype if tctype
  @sctype = sctype.to_s if sctype
  @wgtype = wgtype.to_s if wgtype
  @tcnumber = tcnumber.to_s if tcnumber
  @scnumber = scnumber.to_s if scnumber
  @wgnumber = wgnumber.to_s if wgnumber
  @dir = dir.to_s if dir
  @dirtype = dirtype.to_s if dirtype
  @base = base if base
  @part = part if part
  @addendum = addendum if addendum
end

Instance Attribute Details

#addendumObject

Returns the value of attribute addendum.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def addendum
  @addendum
end

#baseObject

Returns the value of attribute base.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def base
  @base
end

#dirtypeObject

Returns the value of attribute dirtype.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def dirtype
  @dirtype
end

#iterationObject

Returns the value of attribute iteration.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def iteration
  @iteration
end

#joint_documentObject

Returns the value of attribute joint_document.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def joint_document
  @joint_document
end

#scnumberObject

Returns the value of attribute scnumber.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def scnumber
  @scnumber
end

#sctypeObject

Returns the value of attribute sctype.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def sctype
  @sctype
end

#stageObject

Returns the value of attribute stage.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def stage
  @stage
end

#supplementObject

Returns the value of attribute supplement.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def supplement
  @supplement
end

#supplementsObject

Returns the value of attribute supplements.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def supplements
  @supplements
end

#tcnumberObject

Returns the value of attribute tcnumber.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def tcnumber
  @tcnumber
end

#tctypeObject

Returns the value of attribute tctype.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def tctype
  @tctype
end

#typed_stageObject

Returns the value of attribute typed_stage.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def typed_stage
  @typed_stage
end

#wgnumberObject

Returns the value of attribute wgnumber.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def wgnumber
  @wgnumber
end

#wgtypeObject

Returns the value of attribute wgtype.



10
11
12
# File 'lib/pubid/iso/identifier/base.rb', line 10

def wgtype
  @wgtype
end

Class Method Details

.get_amendment_classObject



217
218
219
# File 'lib/pubid/iso/identifier/base.rb', line 217

def get_amendment_class
  Pubid::Iso::Amendment
end

.get_corrigendum_classObject



221
222
223
# File 'lib/pubid/iso/identifier/base.rb', line 221

def get_corrigendum_class
  Pubid::Iso::Corrigendum
end

.get_parser_classObject



225
226
227
# File 'lib/pubid/iso/identifier/base.rb', line 225

def get_parser_class
  Parser
end

.get_renderer_classObject



233
234
235
# File 'lib/pubid/iso/identifier/base.rb', line 233

def get_renderer_class
  Renderer::Base
end

.get_transformer_classObject



229
230
231
# File 'lib/pubid/iso/identifier/base.rb', line 229

def get_transformer_class
  Transformer
end

.get_update_codesObject



237
238
239
# File 'lib/pubid/iso/identifier/base.rb', line 237

def get_update_codes
  UPDATE_CODES
end

.has_typed_stage?(typed_stage) ⇒ Boolean

Returns true when identifier has associated typed stage.

Parameters:

  • typed_stage (String, Symbol)

    typed stage, eg. “DTR” or :dtr

Returns:

  • (Boolean)

    true when identifier has associated typed stage



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pubid/iso/identifier/base.rb', line 201

def has_typed_stage?(typed_stage)
  return self::TYPED_STAGES.key?(typed_stage) if typed_stage.is_a?(Symbol)

  self::TYPED_STAGES.any? do |_, v|
    if v[:abbr].is_a?(Hash)
      v[:abbr].value?(typed_stage)
    else
      if v.key?(:legacy_abbr)
        v[:legacy_abbr].include?(typed_stage) || v[:abbr] == typed_stage
      else
        v[:abbr] == typed_stage
      end
    end
  end
end

.supplement_by_type(supplements, type) ⇒ Object



148
149
150
# File 'lib/pubid/iso/identifier/base.rb', line 148

def supplement_by_type(supplements, type)
  supplements.select { |supplement| supplement.type[:key] == type }.first
end

.supplements_has_type?(supplements, type) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/pubid/iso/identifier/base.rb', line 142

def supplements_has_type?(supplements, type)
  supplements.any? do |supplement|
    supplement.type[:key] == type
  end
end

.transform(params) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pubid/iso/identifier/base.rb', line 176

def transform(params)
  identifier_params = params.map do |k, v|
    get_transformer_class.new.apply(k => v)
  end.inject({}, :merge)

  # return supplement if supplements applied
  if identifier_params[:supplements]
    return transform_supplements(
      identifier_params[:supplements],
      identifier_params.dup.tap { |h| h.delete(:supplements) }
    )
  end

  if identifier_params[:extract]
    base_parameters = params.reject { |k, _| k == :extract }

    return Identifier.create(base: Identifier.create(**base_parameters),
                             type: :ext, **identifier_params[:extract])
  end

  Identifier.create(**identifier_params)
end

.transform_supplements(supplements_params, base_params) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/pubid/iso/identifier/base.rb', line 152

def transform_supplements(supplements_params, base_params)
  supplements = supplements_params.map do |supplement|
    Identifier.create(number: supplement[:number], year: supplement[:year],
        stage: supplement[:typed_stage], edition: supplement[:edition],
        iteration: supplement[:iteration], type: (supplement[:type] || !supplement[:typed_stage] && :sup),
        publisher: supplement[:publisher], base: Identifier.create(**base_params))
  end

  return supplements.first if supplements.count == 1

  # update corrigendum base to amendment
  if supplements_has_type?(supplements, :cor) &&
      (supplements_has_type?(supplements, :amd) ||
        supplements_has_type?(supplements, :sup)) && supplements.count == 2

    supplement = supplement_by_type(supplements, :cor)
    supplement.base = supplement_by_type(supplements, :amd) ||
      supplement_by_type(supplements, :sup)
    supplement
  else
    raise Errors::SupplementRenderingError, "don't know how to render provided supplements"
  end
end

.type_match?(parameters) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/pubid/iso/identifier/base.rb', line 241

def type_match?(parameters)
  parameters[:type] ? has_type?(parameters[:type]) : has_typed_stage?(parameters[:stage])
end

Instance Method Details

#find_typed_stage(typed_stage) ⇒ [Symbol, Stage]

Returns typed stage and stage with assigned harmonized codes.

Parameters:

  • typed_stage (String, Symbol)

    eg. “DTR” or :dtr

Returns:

  • ([Symbol, Stage])

    typed stage and stage with assigned harmonized codes



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

def find_typed_stage(typed_stage)
  if typed_stage.is_a?(Symbol)
    return [typed_stage,
            Identifier.build_stage(
              harmonized_code: Identifier.build_harmonized_stage_code(self.class::TYPED_STAGES[typed_stage][:harmonized_stages])),
    ]
  end

  typed_stage = self.class::TYPED_STAGES.find do |_, v|
    if v[:abbr].is_a?(Hash)
      v[:abbr].value?(typed_stage)
    else
      if v.key?(:legacy_abbr)
        v[:legacy_abbr].include?(typed_stage) || v[:abbr] == typed_stage
      else
        v[:abbr] == typed_stage
      end
      #
      # v[:abbr] == typed_stage
    end
  end

  [typed_stage.first,
   Identifier.build_stage(
     harmonized_code: Identifier.build_harmonized_stage_code(typed_stage[1][:harmonized_stages]))]
end

#get_paramsObject



254
255
256
257
258
259
260
261
262
# File 'lib/pubid/iso/identifier/base.rb', line 254

def get_params
  instance_variables.map do |var|
    if var.to_s == "@typed_stage" && @typed_stage
      [:typed_stage, self.class::TYPED_STAGES[@typed_stage][:abbr]]
    else
      [var.to_s.gsub("@", "").to_sym, instance_variable_get(var)]
    end
  end.to_h
end

#render_joint_document(joint_document) ⇒ Object



331
332
333
# File 'lib/pubid/iso/identifier/base.rb', line 331

def render_joint_document(joint_document)
  "|#{@joint_document}"
end

#resolve_format(format = :ref_dated_long) ⇒ Object

Format options are:

:ref_num_short -- instance reference number: 1 letter language code + short form (DAM) + dated
:ref_num_long -- instance reference number long: 2 letter language code + long form (DAmd) + dated
:ref_dated -- reference dated: no language code + short form (DAM) + dated
:ref_dated_long -- reference dated long: no language code + short form (DAM) + dated
:ref_undated -- reference undated: no language code + short form (DAM) + undated
:ref_undated_long -- reference undated long: 1 letter language code + long form (DAmd) + undated

Parameters:

  • format (:ref_num_short, :ref_num_long, :ref_dated, :ref_dated_long, :ref_undated, :ref_undated_long) (defaults to: :ref_dated_long)

    create reference with specified format



272
273
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
# File 'lib/pubid/iso/identifier/base.rb', line 272

def resolve_format(format = :ref_dated_long)
  options = { with_date: true }
  case format
  when :ref_num_short
    options[:with_language_code] = :single
    options[:stage_format_long] = false
  when :ref_num_long
    options[:with_language_code] = :iso
    options[:stage_format_long] = true
  when :ref_dated
    options[:with_language_code] = :none
    options[:stage_format_long] = false
  when :ref_dated_long
    options[:with_language_code] = :none
    options[:stage_format_long] = true
  when :ref_undated
    options[:with_language_code] = :none
    options[:stage_format_long] = false
    options[:with_date] = false
  when :ref_undated_long
    options[:with_language_code] = :none
    options[:stage_format_long] = true
    options[:with_date] = false
  else
    raise Errors::WrongFormat, "#{format} is not available"
  end
  options
end

#resolve_typed_stage(harmonized_code) ⇒ Symbol?

Resolve typed stage using stage harmonized stage code

Parameters:

  • harmonized_code (HarmonizedStageCode)

Returns:

  • (Symbol, nil)

    typed stage or nil



132
133
134
135
136
137
138
139
# File 'lib/pubid/iso/identifier/base.rb', line 132

def resolve_typed_stage(harmonized_code)
  self.class::TYPED_STAGES.each do |k, v|
    if (v[:harmonized_stages] & harmonized_code.stages) == harmonized_code.stages
      return k
    end
  end
  nil
end

#rootObject

returns root identifier



354
355
356
357
358
# File 'lib/pubid/iso/identifier/base.rb', line 354

def root
  return base.base if base&.base

  base || self
end

#to_s(lang: nil, with_edition: false, with_prf: false, format: :ref_dated_long) ⇒ String

Renders pubid identifier

Format options are:

:ref_num_short -- instance reference number: 1 letter language code + short form (DAM) + dated
:ref_num_long -- instance reference number long: 2 letter language code + long form (DAmd) + dated
:ref_dated -- reference dated: no language code + short form (DAM) + dated
:ref_dated_long -- reference dated long: no language code + short form (DAM) + dated
:ref_undated -- reference undated: no language code + short form (DAM) + undated
:ref_undated_long -- reference undated long: 1 letter language code + long form (DAmd) + undated

Parameters:

  • lang (:french, :russian) (defaults to: nil)

    use language specific renderer

  • with_date (Boolean)

    render identifier with date

  • with_edition (Boolean) (defaults to: false)

    render identifier with edition

  • stage_format_long (Boolean)

    render with long or short stage format

  • format (:ref_num_short, :ref_num_long, :ref_dated, :ref_dated_long, :ref_undated, :ref_undated_long) (defaults to: :ref_dated_long)

    create reference with specified format

  • with_prf (Boolean) (defaults to: false)

    include PRF stage in output

Returns:

  • (String)

    pubid identifier



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/pubid/iso/identifier/base.rb', line 317

def to_s(lang: nil, with_edition: false, with_prf: false,
         format: :ref_dated_long)

  options = resolve_format(format)
  options[:with_edition] = with_edition
  options[:with_prf] = with_prf
  options[:language] = lang

  self.class.get_renderer_class.new(get_params).render(**options) +
    if @joint_document
      render_joint_document(@joint_document)
    end.to_s
end

#typed_stage_abbrevObject

Return typed stage abbreviation, eg. “FDTR”, “DIS”, “TR”



336
337
338
339
340
341
342
# File 'lib/pubid/iso/identifier/base.rb', line 336

def typed_stage_abbrev
  if self.class::TYPED_STAGES.key?(typed_stage)
    self.class::TYPED_STAGES[typed_stage][:abbr]
  else
    stage ? "#{stage.abbr} #{type[:key].to_s.upcase}" : type[:key].to_s.upcase
  end
end

#typed_stage_nameObject

Return typed stage name, eg. “Final Draft Technical Report” for “FDTR”



345
346
347
348
349
350
351
# File 'lib/pubid/iso/identifier/base.rb', line 345

def typed_stage_name
  if self.class::TYPED_STAGES.key?(typed_stage)
    self.class::TYPED_STAGES[typed_stage][:name]
  else
    stage ? "#{stage.name} #{type[:title]}" : type[:title]
  end
end

#urnString

Render URN identifier

Returns:

  • (String)

    URN identifier



248
249
250
251
252
# File 'lib/pubid/iso/identifier/base.rb', line 248

def urn
  ((@tctype && Renderer::UrnTc) || Pubid::Iso::Renderer::Urn).new(
    get_params.merge({ type: type[:key] }),
  ).render + (language ? ":#{language}" : "")
end