Module: Relaton::Bib::HashParserV1

Extended by:
HashParserV1
Includes:
Core::ArrayWrapper, Core::DateParser
Included in:
HashParserV1
Defined in:
lib/relaton/bib/hash_parser_v1.rb

Instance Method Summary collapse

Instance Method Details

#abstract_hash_to_bib(ret) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/relaton/bib/hash_parser_v1.rb', line 130

def abstract_hash_to_bib(ret)
  ret[:abstract] &&= array(ret[:abstract]).map do |a|
    a = { content: a } if a.is_a?(String)
    lang_scrip_array_to_string(a)
    Bib::Abstract.new(**a)
  end
end

#accesslocation_hash_to_bib(ret) ⇒ Object



168
169
170
171
172
# File 'lib/relaton/bib/hash_parser_v1.rb', line 168

def accesslocation_hash_to_bib(ret)
  return unless ret[:accesslocation]

  ret[:accesslocation] = array(ret[:accesslocation])
end

#address_hash_to_bib(contact) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/relaton/bib/hash_parser_v1.rb', line 393

def address_hash_to_bib(contact)
  array(contact).reduce([]) do |a, c|
    next a unless c[:address]

    a << create_address(c[:address])
  end
end

#affiliation_hash_to_bib(person) ⇒ Object

rubocop:disable Metrics/AbcSize



379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/relaton/bib/hash_parser_v1.rb', line 379

def affiliation_hash_to_bib(person) # rubocop:disable Metrics/AbcSize
  return [] unless person[:affiliation]

  array(person[:affiliation]).map do |a|
    a[:description] = array(a[:description]).map do |d|
      localized_marked_up_string d
    end
    Bib::Affiliation.new(
      organization: create_organization(a[:organization]),
      description: a[:description], name: localizedstring(a[:name])
    )
  end
end

#bib_item(item_hash) ⇒ Relaton::Bib::Item

Parameters:

  • item_hash (Hash)

Returns:



502
503
504
# File 'lib/relaton/bib/hash_parser_v1.rb', line 502

def bib_item(item_hash)
  ItemData.new(**item_hash)
end

#biblionote_hash_to_bib(ret) ⇒ Object



218
219
220
221
222
# File 'lib/relaton/bib/hash_parser_v1.rb', line 218

def biblionote_hash_to_bib(ret)
  ret[:note] = array(ret.delete(:biblionote)).map do |n|
    n.is_a?(String) ? Bib::Note.new(content: n) : Bib::Note.new(**n)
  end
end

#classification_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


593
594
595
596
597
598
599
600
# File 'lib/relaton/bib/hash_parser_v1.rb', line 593

def classification_hash_to_bib(ret)
  if ret[:classification]
    ret[:classification] = array(ret[:classification]).map do |cls|
      cls[:content] ||= cls.delete(:value)
      Docidentifier.new(**cls)
    end
  end
end

#contributors_hash_to_bib(ret) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength,Metrics/PerceivedComplexity



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/relaton/bib/hash_parser_v1.rb', line 247

def contributors_hash_to_bib(ret) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength,Metrics/PerceivedComplexity
  ret[:contributor] &&= array(ret[:contributor]).map do |c|
    roles = array(c[:role]).map do |r|
      if r.is_a? Hash
        desc = array(r[:description]).map { |d| localized_marked_up_string d }
        Bib::Contributor::Role.new(type: r[:type], description: desc)
      else
        Bib::Contributor::Role.new(type: r)
      end
    end
    c[:role] = roles
    (c[:person] &&= create_person(c[:person])) ||
      (c[:organization] &&= create_organization(c[:organization]))
    Bib::Contributor.new(**c)
  end
end

Parameters:

  • ret (Hash)


454
455
456
457
458
459
460
461
# File 'lib/relaton/bib/hash_parser_v1.rb', line 454

def copyright_hash_to_bib(ret)
  ret[:copyright] &&= array(ret[:copyright]).map do |c|
    c[:owner] = array(c[:owner]).map do |o|
      create_contribution_info(o)
    end
    Bib::Copyright.new(**c)
  end
end

#create_address(adr) ⇒ Object

Relaton.array(entity).map do |a|

  type, value = a.reject { |k, _| k == :type }.flatten
  case type
  when :street, :city, :state, :country, :postcode # it's for old version compatibility, should be removed in the future
    a[:street] = Relaton.array(a[:street])
    Bib::Address.new(**a)
  when :address then create_address(a[:address])
  when :phone, :email, :uri
    Bib::Contact.new(type: type.to_s, value: value, subtype: a[:type])
  else # it's for old version compatibility, should be removed in the future
    Bib::Contact.new(**a)
  end
end

end



444
445
446
447
448
449
450
451
# File 'lib/relaton/bib/hash_parser_v1.rb', line 444

def create_address(adr)
  if adr.is_a?(Hash)
    adr[:street] = array(adr[:street])
    Bib::Address.new(**adr)
  else
    Bib::Address.new(formatted_address: adr)
  end
end

#create_contribution_info(contrib) ⇒ Object



463
464
465
466
467
468
469
# File 'lib/relaton/bib/hash_parser_v1.rb', line 463

def create_contribution_info(contrib)
  if contrib[:name].is_a?(Hash) && (contrib[:name].keys & %i[prefix forename addition surname completename]).any?
    Bib::ContributionInfo.new person: create_person(contrib)
  else
    Bib::ContributionInfo.new organization: create_organization(contrib)
  end
end

#create_docid(**args) ⇒ Object



192
193
194
# File 'lib/relaton/bib/hash_parser_v1.rb', line 192

def create_docid(**args)
  Bib::Docidentifier.new(**args)
end

#create_doctype(args) ⇒ Object



762
763
764
# File 'lib/relaton/bib/hash_parser_v1.rb', line 762

def create_doctype(args)
  Bib::Doctype.new(**args)
end

#create_org_identifier(identifier) ⇒ Object



293
294
295
296
297
# File 'lib/relaton/bib/hash_parser_v1.rb', line 293

def create_org_identifier(identifier)
  array(identifier).map do |id|
    Bib::Organization::Identifier.new(type: id[:type], content: id[:id])
  end
end

#create_org_subdivision(subdivision) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
# File 'lib/relaton/bib/hash_parser_v1.rb', line 299

def create_org_subdivision(subdivision)
  array(subdivision).map do |sub|
    if sub.is_a? String
      orgname = Bib::TypedLocalizedString.new(content: sub)
      Bib::Subdivision.new(name: [orgname])
    elsif sub.is_a?(Hash) && sub[:content]
      sub[:name] = sub.delete(:content)
      Bib::Subdivision.new(**org_hash_to_bib(sub))
    end
  end.compact
end

#create_organization(org) ⇒ Object



272
273
274
275
276
# File 'lib/relaton/bib/hash_parser_v1.rb', line 272

def create_organization(org)
  return nil if org.nil?

  Bib::Organization.new(**org_hash_to_bib(org))
end

#create_person(person) ⇒ Object

def create_org_name(name)

array(name).map do |nm|
  if nm.is_a?(Hash)
    Bib::Organization::Name.new(**nm)
  else
    Bib::Organization::Name.new(content: nm)
  end
end

end



331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/relaton/bib/hash_parser_v1.rb', line 331

def create_person(person)
  Bib::Person.new(
    name: fullname_hash_to_bib(person),
    credential: array(person[:credential]),
    affiliation: affiliation_hash_to_bib(person),
    address: address_hash_to_bib(person[:contact]),
    phone: phone_hash_to_bib(person[:contact]),
    email: email_hash_to_bib(person[:contact]),
    uri: uri_hash_to_bib(person[:contact]),
    identifier: person_identifiers_hash_to_bib(person),
  )
end

#create_place(place) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/relaton/bib/hash_parser_v1.rb', line 150

def create_place(place)
  if place.is_a?(String)
    Bib::Place.new(formatted_place: place)
  else
    place[:region] &&= create_region(place[:region])
    place[:country] &&= create_region(place[:country])
    Bib::Place.new(**place)
  end
end

#create_region(region) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/relaton/bib/hash_parser_v1.rb', line 160

def create_region(region)
  array(region).map do |r|
    r[:content] ||= r.delete(:name)
    r[:iso] ||= r.delete(:code)
    Bib::Place::RegionType.new(**r)
  end
end

#create_relation(rel) ⇒ Object



486
487
488
# File 'lib/relaton/bib/hash_parser_v1.rb', line 486

def create_relation(rel)
  Bib::Relation.new(**rel)
end

#dates_hash_to_bib(ret) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/relaton/bib/hash_parser_v1.rb', line 174

def dates_hash_to_bib(ret)
  ret[:date] &&= array(ret[:date]).map do |d|
    # `at` is synonym of `on`, which is reserved word in YAML
    d[:at] = d.delete(:value) if d[:value]
    Bib::Date.new(**d)
  end
end

#docid_hash_to_bib(ret) ⇒ Object

rubocop:disable Metrics/AbcSize



182
183
184
185
186
187
188
189
190
# File 'lib/relaton/bib/hash_parser_v1.rb', line 182

def docid_hash_to_bib(ret) # rubocop:disable Metrics/AbcSize
  return unless ret[:docid]

  ret[:docidentifier] = array(ret[:docid]).map do |id|
    id[:type] ||= id[:id].match(/^\w+(?=\s)/)&.to_s
    id[:content] = id[:id]
    create_docid(**id)
  end
end

#docstatus_hash_to_bib(ret) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/relaton/bib/hash_parser_v1.rb', line 229

def docstatus_hash_to_bib(ret)
  ret[:docstatus] && ret[:status]= Bib::Status.new(
    stage: stage(ret[:docstatus][:stage]),
    substage: stage(ret[:docstatus][:substage]),
    iteration: ret[:docstatus][:iteration],
  )
end

#doctype_args(args) ⇒ Object



753
754
755
756
757
758
759
760
# File 'lib/relaton/bib/hash_parser_v1.rb', line 753

def doctype_args(args)
  if args.is_a?(String)
    { content: args }
  else
    args[:content] = args.delete(:type)
    args
  end
end

#doctype_hash_to_bib(ret) ⇒ Object



746
747
748
749
750
751
# File 'lib/relaton/bib/hash_parser_v1.rb', line 746

def doctype_hash_to_bib(ret)
  doctype = ret.dig(:ext, :doctype) || ret[:doctype] # @todo remove ret[:doctype] in the future
  return unless doctype

  ret[:ext][:doctype] = create_doctype(doctype_args(doctype))
end

#edition_hash_to_bib(ret) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/relaton/bib/hash_parser_v1.rb', line 264

def edition_hash_to_bib(ret)
  ret[:edition] &&= if ret[:edition].is_a?(Hash)
                      Bib::Edition.new(**ret[:edition])
                    else
                      Bib::Edition.new(content: ret[:edition])
                    end
end

#editorialgroup_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/relaton/bib/hash_parser_v1.rb', line 630

def editorialgroup_hash_to_bib(ret) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  eg = ret.dig(:ext, :editorialgroup) || ret[:editorialgroup]
  return unless eg

  ret[:ext]&.delete(:editorialgroup)
  ret.delete(:editorialgroup)
  ret[:contributor] ||= []
  array(eg).each do |wg|
    wg[:content] ||= wg.delete(:name)
    ret[:contributor] << Bib::Contributor.new(
      role: [Bib::Contributor::Role.new(
        type: "author",
        description: [Bib::LocalizedMarkedUpString.new(content: "committee")],
      )],
      organization: Bib::Organization.new(
        name: wg[:prefix] ? [Bib::TypedLocalizedString.new(content: wg[:prefix])] : [],
        subdivision: [Bib::Subdivision.new(
          type: "technical-committee",
          subtype: wg[:type],
          name: [Bib::TypedLocalizedString.new(content: wg[:content])],
          identifier: eg_identifiers(wg),
        )],
        abbreviation: wg[:prefix] ? Bib::LocalizedString.new(content: wg[:prefix]) : nil,
      ),
    )
  end
end

#eg_identifiers(workgroup) ⇒ Object

rubocop:disable Metrics/MethodLength



658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/relaton/bib/hash_parser_v1.rb', line 658

def eg_identifiers(workgroup) # rubocop:disable Metrics/MethodLength
  ids = []
  if workgroup[:number]
    ids << Bib::OrganizationType::Identifier.new(
      content: workgroup[:number].to_s,
    )
  end
  if workgroup[:identifier]
    ids << Bib::OrganizationType::Identifier.new(
      content: workgroup[:identifier],
    )
  end
  ids
end

#email_hash_to_bib(contact) ⇒ Object



410
411
412
413
414
415
416
# File 'lib/relaton/bib/hash_parser_v1.rb', line 410

def email_hash_to_bib(contact)
  array(contact).reduce([]) do |a, c|
    next a unless c[:email]

    a << c[:email]
  end
end

#ext_hash_to_bib(ret) ⇒ Object

rubocop:disable Metrics/AbcSize



56
57
58
59
60
61
62
63
64
# File 'lib/relaton/bib/hash_parser_v1.rb', line 56

def ext_hash_to_bib(ret) # rubocop:disable Metrics/AbcSize
  ret[:ext] ||= {}
  doctype_hash_to_bib ret
  ret[:ext][:subdoctype] = ret.delete(:subdoctype) if ret[:subdoctype]
  ret[:ext][:flavor] ||= flavor(ret)
  ics_hash_to_bib ret
  structuredidentifier_hash_to_bib ret
  ret[:ext] = Bib::Ext.new(**ret[:ext])
end

#extent_hash_to_bib(ret) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/relaton/bib/hash_parser_v1.rb', line 81

def extent_hash_to_bib(ret)
  return unless ret[:extent]

  ret[:extent] = array(ret[:extent]).map do |e|
    Relaton::Bib::Extent.new  locality: locality(e[:locality]),
                              locality_stack: locality_stack(e[:locality_stack])
  end
end

#flavor(ret) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/relaton/bib/hash_parser_v1.rb', line 66

def flavor(ret)
  return unless ret[:docid]

  docid = ret[:docidentifier].find(&:primary)
  return unless docid

  docid.type.downcase
end

#forename_hash_to_bib(fname) ⇒ Object



364
365
366
367
368
369
370
371
# File 'lib/relaton/bib/hash_parser_v1.rb', line 364

def forename_hash_to_bib(fname)
  case fname
  when Hash
    lang_scrip_array_to_string fname
    FullNameType::Forename.new(**fname)
  when String then FullNameType::Forename.new(content: fname)
  end
end

#formattedref(frf) ⇒ Relaton::Bib::Formattedref

Parameters:

  • frf (Hash, String)

Returns:



737
738
739
740
741
742
743
744
# File 'lib/relaton/bib/hash_parser_v1.rb', line 737

def formattedref(frf)
  if frf.is_a?(Hash)
    lang_scrip_array_to_string(frf)
    Relaton::Bib::Formattedref.new(**frf)
  else
    Relaton::Bib::Formattedref.new(content: frf)
  end
end

#formattedref_hash_to_bib(ret) ⇒ Object



224
225
226
227
# File 'lib/relaton/bib/hash_parser_v1.rb', line 224

def formattedref_hash_to_bib(ret)
  ret[:formattedref] &&
    ret[:formattedref] = formattedref(ret[:formattedref])
end

#fullname_hash_to_bib(person) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/relaton/bib/hash_parser_v1.rb', line 344

def fullname_hash_to_bib(person) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  n = person[:name]
  fname = given_hash_to_bib n[:given] || n # `n` is for backward compatibility
  Bib::FullName.new(
    abbreviation: localizedstring(n[:abbreviation]),
    forename: fname,
    formatted_initials: localizedstring(n.dig(:given, :formatted_initials)),
    addition: array(n[:addition])&.map { |f| localizedstring(f) },
    prefix: array(n[:prefix])&.map { |f| localizedstring(f) },
    surname: localizedstring(n[:surname]),
    completename: localizedstring(n[:completename]),
  )
end

#given_hash_to_bib(given) ⇒ Object



358
359
360
361
362
# File 'lib/relaton/bib/hash_parser_v1.rb', line 358

def given_hash_to_bib(given)
  return unless given

  array(given[:forename])&.map { |f| forename_hash_to_bib(f) }
end

#hash_to_bib(args) ⇒ Hash

Parameters:

  • args (Hash)

Returns:

  • (Hash)


12
13
14
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
45
46
47
48
# File 'lib/relaton/bib/hash_parser_v1.rb', line 12

def hash_to_bib(args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  return nil unless args.is_a?(Hash)

  ret = Marshal.load(Marshal.dump(symbolize(args))) # deep copy
  ret[:fetched] &&= ::Date.parse(ret[:fetched])
  ret.delete(:"schema-version")
  ret.delete(:schema_version)
  id_hash_to_bib ret
  title_hash_to_bib(ret)
  link_hash_to_bib(ret)
  language_hash_to_bib(ret)
  script_hash_to_bib(ret)
  dates_hash_to_bib(ret)
  docid_hash_to_bib(ret)
  version_hash_to_bib(ret)
  biblionote_hash_to_bib(ret)
  abstract_hash_to_bib(ret)
  formattedref_hash_to_bib(ret)
  docstatus_hash_to_bib(ret)
  contributors_hash_to_bib(ret)
  editorialgroup_hash_to_bib(ret)
  edition_hash_to_bib(ret)
  copyright_hash_to_bib(ret)
  relations_hash_to_bib(ret)
  series_hash_to_bib(ret)
  medium_hash_to_bib(ret)
  place_hash_to_bib(ret)
  extent_hash_to_bib(ret)
  size_hash_to_bib(ret)
  accesslocation_hash_to_bib(ret)
  classification_hash_to_bib(ret)
  validity_hash_to_bib(ret)
  keyword_hash_to_bib(ret)
  ret[:license] = array(ret[:license])
  ext_hash_to_bib ret
  ret
end

#ics_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


674
675
676
677
678
679
# File 'lib/relaton/bib/hash_parser_v1.rb', line 674

def ics_hash_to_bib(ret)
  ics = ret.dig(:ext, :ics) || ret[:ics] # @todo remove ret[:ics] in the future
  return unless ics

  ret[:ext][:ics] = array(ics).map { |item| Bib::ICS.new(**item) }
end

#id_hash_to_bib(ret) ⇒ Object



50
51
52
53
54
# File 'lib/relaton/bib/hash_parser_v1.rb', line 50

def id_hash_to_bib(ret)
  return unless ret[:id]

  ret[:id] = ret[:id].gsub(/\W+/, "")
end

#keyword_hash_to_bib(ret) ⇒ Object



75
76
77
78
79
# File 'lib/relaton/bib/hash_parser_v1.rb', line 75

def keyword_hash_to_bib(ret)
  ret[:keyword] = array(ret[:keyword]).map do |keyword|
    Bib::Keyword.new vocab: localizedstring(keyword)
  end
end

#lang_scrip_array_to_string(lst) ⇒ Object



720
721
722
723
# File 'lib/relaton/bib/hash_parser_v1.rb', line 720

def lang_scrip_array_to_string(lst)
  lst[:language] = lst[:language][0] if lst[:language].is_a?(Array)
  lst[:script] = lst[:script][0] if lst[:script].is_a?(Array)
end

#language_hash_to_bib(ret) ⇒ Object



118
119
120
121
122
# File 'lib/relaton/bib/hash_parser_v1.rb', line 118

def language_hash_to_bib(ret)
  return unless ret[:language]

  ret[:language] = array(ret[:language])
end

#legacy_version_content(hash) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/relaton/bib/hash_parser_v1.rb', line 210

def legacy_version_content(hash)
  draft = hash[:draft]
  revdate = hash[:revision_date]
  return "#{draft} (#{revdate})" if draft && revdate

  draft || revdate
end


138
139
140
141
142
143
144
# File 'lib/relaton/bib/hash_parser_v1.rb', line 138

def link_hash_to_bib(ret)
  return unless ret[:link]

  ret[:source] = array(ret[:link]).map do |l|
    Bib::Uri.new(**l)
  end
end

#locality(locality) ⇒ Object



90
91
92
# File 'lib/relaton/bib/hash_parser_v1.rb', line 90

def locality(locality)
  array(locality).map { |l| Bib::Locality.new(**l) }
end

#locality_stack(locality_stack) ⇒ Object



94
95
96
# File 'lib/relaton/bib/hash_parser_v1.rb', line 94

def locality_stack(locality_stack)
  array(locality_stack).map { |l| Bib::LocalityStack.new locality: locality(l[:locality]) }
end

#localized_marked_up_string(lst) ⇒ Object



725
726
727
728
729
730
731
732
733
# File 'lib/relaton/bib/hash_parser_v1.rb', line 725

def localized_marked_up_string(lst)
  return unless lst

  if lst.is_a?(Hash)
    lang_scrip_array_to_string lst
    LocalizedMarkedUpString.new(**lst)
  else LocalizedMarkedUpString.new(content: lst)
  end
end

#localizedstring(lst) ⇒ Relaton::Bib::LocalizedString

Parameters:

Returns:



710
711
712
713
714
715
716
717
718
# File 'lib/relaton/bib/hash_parser_v1.rb', line 710

def localizedstring(lst)
  return unless lst

  if lst.is_a?(Hash)
    lang_scrip_array_to_string lst
    Bib::LocalizedString.new(**lst)
  else Bib::LocalizedString.new(content: lst)
  end
end

#medium_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


588
589
590
# File 'lib/relaton/bib/hash_parser_v1.rb', line 588

def medium_hash_to_bib(ret)
  ret[:medium] = Bib::Medium.new(**ret[:medium]) if ret[:medium]
end

#org_hash_to_bib(org) ⇒ Object

rubocop:disable Metrics/AbcSize



278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/relaton/bib/hash_parser_v1.rb', line 278

def org_hash_to_bib(org) # rubocop:disable Metrics/AbcSize
  org[:identifier] = create_org_identifier(org[:identifier])
  org[:subdivision] = create_org_subdivision(org[:subdivision])
  org[:address] = address_hash_to_bib(org[:contact])
  org[:phone] = phone_hash_to_bib(org[:contact])
  org[:email] = email_hash_to_bib(org[:contact])
  org[:uri] = uri_hash_to_bib(org[:contact] || org)
  org[:logo] = array(org[:logo]).map do |l|
    Bib::Logo.new(image: Bib::Image.new(**l[:image]))
  end
  org[:name] = typed_localized_string(org[:name])
  org[:abbreviation] &&= localizedstring(org[:abbreviation])
  org
end

#parse_validity_time(val, period) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/relaton/bib/hash_parser_v1.rb', line 612

def parse_validity_time(val, period)
  t = val[period]&.to_s
  return unless t

  p = period == :ends ? -1 : 1
  case t
  when /^\d{4}$/
    ::Date.new(t.to_i, p, p).to_datetime.to_time.utc
  when /^(?<year>\d{4})-(?<month>\d{1,2})$/
    ::Date.new($~[:year].to_i, $~[:month].to_i, p).to_datetime.to_time.utc
  else
    # Default to UTC when no timezone is specified
    t += " UTC" unless t.match?(/[+-]\d{2}:?\d{2}\s*$|Z\s*$/i)
    ::Time.parse t
  end
end

#person_identifiers_hash_to_bib(person) ⇒ Object



373
374
375
376
377
# File 'lib/relaton/bib/hash_parser_v1.rb', line 373

def person_identifiers_hash_to_bib(person)
  array(person[:identifier])&.map do |a|
    Bib::Person::Identifier.new(type: a[:type], content: a[:id])
  end
end

#phone_hash_to_bib(contact) ⇒ Object



401
402
403
404
405
406
407
408
# File 'lib/relaton/bib/hash_parser_v1.rb', line 401

def phone_hash_to_bib(contact)
  array(contact).reduce([]) do |a, c|
    next a unless c[:phone]

    c[:type] ||= "work"
    a << Bib::Phone.new(type: c[:type], content: c[:phone])
  end
end

#place_hash_to_bib(ret) ⇒ Object



146
147
148
# File 'lib/relaton/bib/hash_parser_v1.rb', line 146

def place_hash_to_bib(ret)
  ret[:place] &&= array(ret[:place]).map { |pl| create_place(pl) }
end

#relation_bibitem_hash_to_bib(rel) ⇒ Object

Parameters:

  • rel (Hash)

    relation



491
492
493
494
495
496
497
498
# File 'lib/relaton/bib/hash_parser_v1.rb', line 491

def relation_bibitem_hash_to_bib(rel)
  if rel[:bibitem]
    rel[:bibitem] = bib_item hash_to_bib(rel[:bibitem])
  else
    Util.warn "Bibitem missing: `#(rel)`"
    rel[:bibitem] = nil
  end
end

#relation_locality_hash_to_bib(rel) ⇒ Relaton::Bib::LocalityStack

Parameters:

  • rel (Hash)

    relation

Returns:



508
509
510
511
512
513
514
# File 'lib/relaton/bib/hash_parser_v1.rb', line 508

def relation_locality_hash_to_bib(rel)
  return unless rel[:locality]&.any?

  rel[:locality] = array(rel[:locality]).map do |bl|
    Bib::Locality.new(**bl)
  end
end

#relation_locality_stack_hash_to_bib(rel) ⇒ Object



516
517
518
519
520
521
522
# File 'lib/relaton/bib/hash_parser_v1.rb', line 516

def relation_locality_stack_hash_to_bib(rel)
  return unless rel[:locality_stack]&.any?

  rel[:locality_stack] = array(rel[:locality_stack]).map do |ls|
    Bib::LocalityStack.new locality: relation_locality_hash_to_bib(ls)
  end
end

#relation_source_locality_hash_to_bib(rel) ⇒ Object

Parameters:

  • rel (Hash)

    relation



536
537
538
539
540
# File 'lib/relaton/bib/hash_parser_v1.rb', line 536

def relation_source_locality_hash_to_bib(rel)
  rel[:source_locality] = array(rel[:source_locality]).map do |loc|
    Bib::Locality.new(**loc)
  end
end

#relations_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/relaton/bib/hash_parser_v1.rb', line 472

def relations_hash_to_bib(ret) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  return unless ret[:relation]

  ret[:relation] &&= array(ret[:relation]).map do |rel|
    rel[:description] = LocalizedMarkedUpString.new(**rel[:description]) if rel[:description]
    relation_bibitem_hash_to_bib(rel)
    relation_locality_hash_to_bib(rel)
    relation_locality_stack_hash_to_bib(rel)
    relaton_source_locality_stack_hash_to_bib(rel)
    relation_source_locality_hash_to_bib(rel)
    create_relation rel
  end
end

#relaton_source_locality_stack_hash_to_bib(rel) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/relaton/bib/hash_parser_v1.rb', line 542

def relaton_source_locality_stack_hash_to_bib(rel) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
  sls = array(rel[:source_locality_stack])
  array(rel[:source_locality]).each do |loc|
    sls << array(loc[:source_locality_stack]) if loc[:source_locality_stack]
  end

  case rel[:source_locality]
  when Hash then rel[:source_locality].delete(:source_locality_stack)
  when Array then rel[:source_locality].delete_if { |loc| loc[:source_locality_stack] }
  end

  rel[:source_locality_stack] = sls.map do |loc|
    SourceLocalityStack.new source_locality: relation_source_locality_hash_to_bib(source_locality: loc)
  end
end

#script_hash_to_bib(ret) ⇒ Object



124
125
126
127
128
# File 'lib/relaton/bib/hash_parser_v1.rb', line 124

def script_hash_to_bib(ret)
  return unless ret[:script]

  ret[:script] = array(ret[:script])
end

#series_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


559
560
561
562
563
564
565
566
567
568
569
# File 'lib/relaton/bib/hash_parser_v1.rb', line 559

def series_hash_to_bib(ret) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  ret[:series] &&= array(ret[:series]).map do |s|
    s[:formattedref] && s[:formattedref] = formattedref(s[:formattedref])
    s[:title] &&= title_collection(s[:title])
    s[:place] &&= create_place(s[:place])
    s[:abbreviation] &&= localizedstring(s[:abbreviation])
    s[:from] &&= s[:from].to_s
    s[:to] &&= s[:to].to_s
    Bib::Series.new(**s)
  end
end

#size_hash_to_bib(ret) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/relaton/bib/hash_parser_v1.rb', line 98

def size_hash_to_bib(ret)
  return unless ret[:size]

  ret[:size] = array(ret[:size])
  value = ret[:size]&.map do |val|
    val[:content] = val.delete(:value)
    Bib::Size::Value.new(**val)
  end
  ret[:size] = Bib::Size.new(value: value)
end

#stage(stg) ⇒ Relaton::Bib::DocumentStatus::Stage

Parameters:

  • stg (Hash)

Returns:

  • (Relaton::Bib::DocumentStatus::Stage)


239
240
241
242
243
244
245
# File 'lib/relaton/bib/hash_parser_v1.rb', line 239

def stage(stg)
  return unless stg

  args = stg.is_a?(String) ? { content: stg } : stg
  args[:content] ||= args.delete(:value)
  Bib::Status::Stage.new(**args)
end

#structuredidentifier_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


682
683
684
685
686
687
688
689
690
691
692
# File 'lib/relaton/bib/hash_parser_v1.rb', line 682

def structuredidentifier_hash_to_bib(ret)
  struct_id = ret.dig(:ext, :structuredidentifier) || ret[:structuredidentifier] # @todo remove ret[:structuredidentifier] in the future
  return unless struct_id

  sids = array(struct_id).map do |si|
    si[:agency] = array si[:agency]
    si[:klass] = si.delete(:class)
    Bib::StructuredIdentifier.new(**si)
  end
  ret[:ext][:structuredidentifier] = sids
end

#symbolize(obj) ⇒ Hash, ...

Parameters:

  • ogj (Hash, Array, String)

Returns:

  • (Hash, Array, String)


696
697
698
699
700
701
702
703
704
705
706
# File 'lib/relaton/bib/hash_parser_v1.rb', line 696

def symbolize(obj)
  case obj
  when Hash
    obj.reduce({}) do |memo, (k, v)|
      memo[k.to_sym] = symbolize(v)
      memo
    end
  when Array then obj.reduce([]) { |memo, v| memo << symbolize(v) }
  else obj
  end
end

#title_collection(title) ⇒ Relaton::Bib::TitleCollection

Parameters:

  • title (Hash, Strinbg, Array<Hash, String>)

Returns:

  • (Relaton::Bib::TitleCollection)


576
577
578
579
580
581
582
583
584
585
# File 'lib/relaton/bib/hash_parser_v1.rb', line 576

def title_collection(title)
  array(title).map do |t|
    if t.is_a?(Hash)
      lang_scrip_array_to_string t
      Bib::Title.new(**t)
    elsif t.is_a?(String)
      Bib::Title.new(content: t)
    end
  end
end

#title_hash_to_bib(ret) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/relaton/bib/hash_parser_v1.rb', line 109

def title_hash_to_bib(ret)
  # return unless ret[:title]

  # ret[:title] = array(ret[:title]).reduce(Bib::TitleCollection.new) do |m, t|
  #   m << (t.is_a?(Hash) ? Bib::Title.new(**t) : Bib::Title.new(content: t))
  # end
  ret[:title] &&= title_collection(ret[:title])
end

#typed_localized_string(typed_strs) ⇒ Object



311
312
313
314
315
316
317
318
319
# File 'lib/relaton/bib/hash_parser_v1.rb', line 311

def typed_localized_string(typed_strs)
  array(typed_strs).map do |args|
    if args.is_a? String
      Bib::TypedLocalizedString.new(content: args)
    elsif args.is_a? Hash
      Bib::TypedLocalizedString.new(**args)
    end
  end
end

#uri_hash_to_bib(contact) ⇒ Object



418
419
420
421
422
423
424
# File 'lib/relaton/bib/hash_parser_v1.rb', line 418

def uri_hash_to_bib(contact)
  array(contact).reduce([]) do |a, c|
    next a unless c[:uri] || c[:url]

    a << Bib::Uri.new(content: c[:uri] || c[:url])
  end
end

#validity_hash_to_bib(ret) ⇒ Object

Parameters:

  • ret (Hash)


603
604
605
606
607
608
609
610
# File 'lib/relaton/bib/hash_parser_v1.rb', line 603

def validity_hash_to_bib(ret)
  return unless ret[:validity]

  b = parse_validity_time(ret[:validity], :begins)
  e = parse_validity_time(ret[:validity], :ends)
  r = parse_validity_time(ret[:validity], :revision)
  ret[:validity] = Bib::Validity.new(begins: b, ends: e, revision: r)
end

#version_entry(hash) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/relaton/bib/hash_parser_v1.rb', line 202

def version_entry(hash)
  return Bib::Version.new(**hash) if hash[:content]

  attrs = { content: legacy_version_content(hash) }
  attrs[:type] = hash[:type] if hash[:type]
  Bib::Version.new(**attrs)
end

#version_hash_to_bib(ret) ⇒ Object



196
197
198
199
200
# File 'lib/relaton/bib/hash_parser_v1.rb', line 196

def version_hash_to_bib(ret)
  return unless ret[:version]

  ret[:version] = array(ret[:version]).map { |v| version_entry(v) }
end