relaton-bib 1.3.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,19 +38,21 @@ module RelatonBib
38
38
  @scope = scope
39
39
  end
40
40
 
41
- # @param builder [Nokogiri::XML::Builder]
42
- def to_xml(builder)
43
- builder.copyright do
41
+ # @param opts [Hash]
42
+ # @option opts [Nokogiri::XML::Builder] :builder XML builder
43
+ # @option opts [String, Symbol] :lang language
44
+ def to_xml(**opts)
45
+ opts[:builder].copyright do |builder|
44
46
  builder.from from ? from.year : "unknown"
45
47
  builder.to to.year if to
46
- owner.each { |o| builder.owner { o.to_xml builder } }
48
+ owner.each { |o| builder.owner { o.to_xml **opts } }
47
49
  builder.scope scope if scope
48
50
  end
49
51
  end
50
52
  # rubocop:enable Metrics/AbcSize
51
53
 
52
54
  # @return [Hash]
53
- def to_hash
55
+ def to_hash # rubocop:disable Metrics/AbcSize
54
56
  owners = single_element_array(owner.map { |o| o.to_hash["organization"] })
55
57
  hash = {
56
58
  "owner" => owners,
@@ -20,30 +20,40 @@ module RelatonBib
20
20
  def remove_part
21
21
  case @type
22
22
  when "Chinese Standard" then @id.sub!(/\.\d+/, "")
23
- else
24
- @id.sub!(/-\d+/, "")
23
+ when "ISO", "IEC" then @id.sub!(/-[^:]+/, "")
24
+ when "URN" then remove_urn_part
25
25
  end
26
26
  end
27
27
 
28
28
  def remove_date
29
29
  case @type
30
30
  when "Chinese Standard" then @id.sub!(/-[12]\d\d\d/, "")
31
- else
32
- @id.sub!(/:[12]\d\d\d/, "")
31
+ when "ISO", "IEC" then @id.sub!(/:[12]\d\d\d/, "")
32
+ when "URN"
33
+ @id.sub!(/^(urn:iec:std:[^:]+:[^:]+:)[^:]*/, '\1')
33
34
  end
34
35
  end
35
36
 
36
37
  def all_parts
37
- @id += " (all parts)"
38
+ if type == "URN"
39
+ @id.sub!(%r{^(urn:iec:std(?::[^:]*){4}).*}, '\1:ser')
40
+ else
41
+ @id += " (all parts)"
42
+ end
38
43
  end
39
44
 
40
45
  #
41
46
  # Add docidentifier xml element
42
47
  #
43
- # @param [Nokogiri::XML::Builder] builder
44
- #
45
- def to_xml(builder)
46
- element = builder.docidentifier id
48
+ # @param opts [Hash]
49
+ # @option opts [Nokogiri::XML::Builder] :builder XML builder
50
+ # @option opts [String] :lang language
51
+ def to_xml(**opts) # rubocop:disable Metrics/AbcSize
52
+ lid = if type == "URN" && opts[:lang]
53
+ id.sub %r{(?<=:)(?:\w{2},)*?(#{opts[:lang]})(?:,\w{2})*}, '\1'
54
+ else id
55
+ end
56
+ element = opts[:builder].docidentifier lid
47
57
  element[:type] = type if type
48
58
  element[:scope] = scope if scope
49
59
  end
@@ -67,5 +77,24 @@ module RelatonBib
67
77
  out += "#{pref}docid.id:: #{id}\n"
68
78
  out
69
79
  end
80
+
81
+ private
82
+
83
+ def remove_urn_part # rubocop:disable Metrics/MethodLength
84
+ @id.sub!(%r{^
85
+ (urn:iso:std:[^:]+ # ISO prefix and originator
86
+ (?::(?:data|guide|isp|iwa|pas|r|tr|ts|tta)) # type
87
+ ?:\d+) # docnumber
88
+ (?::-[^:]+)? # partnumber
89
+ (?::(draft|cancelled|stage-[^:]+))? # status
90
+ (?::ed-\d+)?(?::v[^:]+)? # edition and version
91
+ (?::\w{2}(?:,\w{2})*)? # langauge
92
+ }x, '\1') # remove partnumber, status, version, and language
93
+ @id.sub!(%r{^
94
+ (urn:iec:std:[^:]+ # IEC prefix and originator
95
+ :\d+) # docnumber
96
+ (?:-[^:]+)? # partnumber
97
+ }x, '\1') # remove partnumber
98
+ end
70
99
  end
71
100
  end
@@ -63,7 +63,7 @@ module RelatonBib
63
63
  opts.delete :note
64
64
  builder.relation(type: type) do
65
65
  builder.description { description.to_xml builder } if description
66
- bibitem.to_xml(builder, **opts.merge(embedded: true))
66
+ bibitem.to_xml(**opts.merge(builder: builder, embedded: true))
67
67
  locality.each { |l| l.to_xml builder }
68
68
  source_locality.each { |l| l.to_xml builder }
69
69
  end
@@ -71,7 +71,7 @@ module RelatonBib
71
71
  # rubocop:enable Metrics/AbcSize
72
72
 
73
73
  # @return [Hash]
74
- def to_hash
74
+ def to_hash # rubocop:disable Metrics/AbcSize
75
75
  hash = { "type" => type, "bibitem" => bibitem.to_hash }
76
76
  hash["description"] = description.to_hash if description
77
77
  hash["locality"] = single_element_array(locality) if locality&.any?
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "forwardable"
4
-
5
3
  module RelatonBib
6
4
  # Document relation collection
7
5
  class DocRelationCollection
@@ -1,7 +1,4 @@
1
1
  # frozen_string_literal: true
2
-
3
- require "relaton_bib/localized_string"
4
-
5
2
  module RelatonBib
6
3
  # Document status.
7
4
  class DocumentStatus
@@ -32,5 +32,10 @@ module RelatonBib
32
32
  tc.to_asciibib pref, technical_committee.size
33
33
  end.join
34
34
  end
35
+
36
+ # @return [true]
37
+ def presence?
38
+ true
39
+ end
35
40
  end
36
41
  end
@@ -58,7 +58,8 @@ module RelatonBib
58
58
  def title_hash_to_bib(ret)
59
59
  return unless ret[:title]
60
60
 
61
- ret[:title] = array(ret[:title]).reduce([]) do |m, t|
61
+ ret[:title] = array(ret[:title])
62
+ .reduce(TypedTitleStringCollection.new) do |m, t|
62
63
  if t.is_a?(Hash) then m << t
63
64
  else
64
65
  m + TypedTitleString.from_string(t)
@@ -139,20 +140,14 @@ module RelatonBib
139
140
  )
140
141
  end
141
142
 
142
- def biblionote_hash_to_bib(ret) # rubocop:disable Metrics/MethodLength
143
+ def biblionote_hash_to_bib(ret) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
143
144
  return unless ret[:biblionote]
144
145
 
145
146
  ret[:biblionote] = array(ret[:biblionote])
146
- (ret[:biblionote])&.each_with_index do |n, i|
147
- ret[:biblionote][i] = if n.is_a?(String)
148
- BiblioNote.new content: n
149
- else
150
- BiblioNote.new(
151
- content: n[:content], type: n[:type],
152
- language: n[:language], script: n[:script],
153
- format: n[:format]
154
- )
155
- end
147
+ .reduce(BiblioNoteCollection.new([])) do |mem, n|
148
+ mem << if n.is_a?(String) then BiblioNote.new content: n
149
+ else BiblioNote.new(n)
150
+ end
156
151
  end
157
152
  end
158
153
 
@@ -178,7 +173,7 @@ module RelatonBib
178
173
  DocumentStatus::Stage.new(**args)
179
174
  end
180
175
 
181
- def contributors_hash_to_bib(ret) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
176
+ def contributors_hash_to_bib(ret) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength,Metrics/PerceivedComplexity
182
177
  return unless ret[:contributor]
183
178
 
184
179
  ret[:contributor] = array(ret[:contributor])
@@ -209,6 +204,9 @@ module RelatonBib
209
204
  org[:identifier] = array(org[:identifier])&.map do |a|
210
205
  OrgIdentifier.new(a[:type], a[:id])
211
206
  end
207
+ org[:subdivision] = array(org[:subdivision]).map do |sd|
208
+ LocalizedString.new sd
209
+ end
212
210
  org
213
211
  end
214
212
 
@@ -20,7 +20,7 @@ module RelatonBib
20
20
 
21
21
  # @return [String]
22
22
  def inspect
23
- "<#{self.class}:#{format('%#.14x', object_id << 1)} "\
23
+ "<#{self.class}:#{format('%<id>#.14x', id: object_id << 1)} "\
24
24
  "@text=\"#{@hit_collection&.text}\" "\
25
25
  "@fetched=\"#{!@fetch.nil?}\" "\
26
26
  "@fullIdentifier=\"#{@fetch&.shortref(nil)}\" "\
@@ -31,13 +31,17 @@ module RelatonBib
31
31
  raise "Not implemented"
32
32
  end
33
33
 
34
- # @param builder [Nokogiri::XML::Builder]
35
- def to_xml(builder = nil, **opts)
36
- if builder
37
- fetch.to_xml builder, **opts
34
+ # @param opts [Hash]
35
+ # @option opts [Nokogiri::XML::Builder] :builder XML builder
36
+ # @option opts [Boolean] :bibdata
37
+ # @option opts [String, Symbol] :lang language
38
+ # @return [String] XML
39
+ def to_xml(**opts)
40
+ if opts[:builder]
41
+ fetch.to_xml **opts
38
42
  else
39
43
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
40
- fetch.to_xml xml, **opts
44
+ fetch.to_xml **opts.merge(builder: xml)
41
45
  end
42
46
  builder.doc.root.to_xml
43
47
  end
@@ -36,12 +36,17 @@ module RelatonBib
36
36
  self
37
37
  end
38
38
 
39
+ # @param opts [Hash]
40
+ # @option opts [Nokogiri::XML::Builder] :builder XML builder
41
+ # @option opts [Boolean] :bibdata
42
+ # @option opts [String, Symbol] :lang language
43
+ # @return [String] XML
39
44
  def to_xml(**opts)
40
45
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
41
46
  xml.documents do
42
47
  @array.each do |hit|
43
48
  hit.fetch
44
- hit.to_xml xml, **opts
49
+ hit.to_xml **opts.merge(builder: xml)
45
50
  end
46
51
  end
47
52
  end
@@ -49,8 +54,8 @@ module RelatonBib
49
54
  end
50
55
 
51
56
  def select(&block)
52
- me = self.deep_dup
53
- array_dup = self.instance_variable_get(:@array).deep_dup
57
+ me = deep_dup
58
+ array_dup = instance_variable_get(:@array).deep_dup
54
59
  me.instance_variable_set(:@array, array_dup)
55
60
  array_dup.select!(&block)
56
61
  me
@@ -49,7 +49,7 @@ module RelatonBib
49
49
  end
50
50
 
51
51
  # @param builder [Nokogiri::XML::Builder]
52
- def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
52
+ def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
53
53
  return unless content
54
54
 
55
55
  if content.is_a?(Array)
@@ -62,7 +62,7 @@ module RelatonBib
62
62
  end
63
63
 
64
64
  # @return [Hash]
65
- def to_hash # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
65
+ def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
66
66
  if content.is_a? String
67
67
  return content unless language || script
68
68
 
@@ -77,7 +77,7 @@ module RelatonBib
77
77
  # @param prefix [String]
78
78
  # @param count [Integer] number of elements
79
79
  # @return [String]
80
- def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
80
+ def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
81
81
  pref = prefix.empty? ? prefix : prefix + "."
82
82
  if content.is_a? String
83
83
  out = count > 1 ? "#{prefix}::\n" : ""
@@ -55,7 +55,7 @@ module RelatonBib
55
55
  # @return [RelatonBib::LocalizedString, NilClass]
56
56
  attr_reader :abbreviation
57
57
 
58
- # @return [RelatonBib::LocalizedString, NilClass]
58
+ # @return [Array<RelatonBib::LocalizedString>]
59
59
  attr_reader :subdivision
60
60
 
61
61
  # @return [Array<RelatonBib::OrgIdentifier>]
@@ -63,11 +63,11 @@ module RelatonBib
63
63
 
64
64
  # @param name [String, Hash, Array<String, Hash>]
65
65
  # @param abbreviation [RelatoBib::LocalizedString, String]
66
- # @param subdivision [RelatoBib::LocalizedString, String]
66
+ # @param subdivision [Array<RelatoBib::LocalizedString>]
67
67
  # @param url [String]
68
68
  # @param identifier [Array<RelatonBib::OrgIdentifier>]
69
69
  # @param contact [Array<RelatonBib::Address, RelatonBib::Contact>]
70
- def initialize(**args) # rubocop:disable Metrics/AbcSize
70
+ def initialize(**args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
71
71
  raise ArgumentError, "missing keyword: name" unless args[:name]
72
72
 
73
73
  super(url: args[:url], contact: args.fetch(:contact, []))
@@ -79,17 +79,23 @@ module RelatonBib
79
79
  end
80
80
 
81
81
  @abbreviation = localized_string args[:abbreviation]
82
- @subdivision = localized_string args[:subdivision]
82
+ @subdivision = (args[:subdivision] || []).map do |sd|
83
+ localized_string sd
84
+ end
83
85
  @identifier = args.fetch(:identifier, [])
84
86
  end
85
87
 
86
- # @param builder [Nokogiri::XML::Builder]
87
- def to_xml(builder) # rubocop:disable Metrics/AbcSize
88
- builder.organization do
89
- name.each do |n|
90
- builder.name { |b| n.to_xml b }
91
- end
92
- builder.subdivision { |s| subdivision.to_xml s } if subdivision
88
+ # @param opts [Hash]
89
+ # @option opts [Nokogiri::XML::Builder] :builder XML builder
90
+ # @option opts [String] :lang language
91
+ def to_xml(**opts) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
92
+ opts[:builder].organization do |builder|
93
+ nm = name.select { |n| n.language&.include? opts[:lang] }
94
+ nm = name unless nm.any?
95
+ nm.each { |n| builder.name { |b| n.to_xml b } }
96
+ sbdv = subdivision.select { |sd| sd.language&.include? opts[:lang] }
97
+ sbdv = subdivision unless sbdv.any?
98
+ sbdv.each { |sd| builder.subdivision { sd.to_xml builder } }
93
99
  builder.abbreviation { |a| abbreviation.to_xml a } if abbreviation
94
100
  builder.uri url if uri
95
101
  identifier.each { |identifier| identifier.to_xml builder }
@@ -98,23 +104,28 @@ module RelatonBib
98
104
  end
99
105
 
100
106
  # @return [Hash]
101
- def to_hash
107
+ def to_hash # rubocop:disable Metrics/AbcSize
102
108
  hash = { "name" => single_element_array(name) }
103
109
  hash["abbreviation"] = abbreviation.to_hash if abbreviation
104
110
  hash["identifier"] = single_element_array(identifier) if identifier&.any?
105
- hash["subdivision"] = subdivision.to_hash if subdivision
111
+ if subdivision&.any?
112
+ hash["subdivision"] = single_element_array(subdivision)
113
+ end
106
114
  { "organization" => hash.merge(super) }
107
115
  end
108
116
 
109
117
  # @param prefix [String]
110
118
  # @param count [Integer]
111
119
  # @return [String]
112
- def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
120
+ def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
113
121
  pref = prefix.sub /\*$/, "organization"
114
122
  out = count > 1 ? "#{pref}::\m" : ""
115
123
  name.each { |n| out += n.to_asciibib "#{pref}.name", name.size }
116
124
  out += abbreviation.to_asciibib "#{pref}.abbreviation" if abbreviation
117
- out += subdivision.to_asciibib "#{pref}.subdivision" if subdivision
125
+ subdivision.each do |sd|
126
+ out += "#{pref}.subdivision::" if subdivision.size > 1
127
+ out += sd.to_asciibib "#{pref}.subdivision"
128
+ end
118
129
  identifier.each { |n| out += n.to_asciibib pref, identifier.size }
119
130
  out += super pref
120
131
  out
@@ -44,23 +44,33 @@ module RelatonBib
44
44
  @completename = args[:completename]
45
45
  end
46
46
 
47
- # @param builder [Nokogiri::XML::Builder]
48
- def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
49
- builder.name do
47
+ # @param opts [Hash]
48
+ # @option opts [Nokogiri::XML::Builder] :builder XML builder
49
+ # @option opts [String] :lang language
50
+ def to_xml(**opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
51
+ opts[:builder].name do |builder|
50
52
  if completename
51
53
  builder.completename { completename.to_xml builder }
52
54
  else
53
- prefix.each { |p| builder.prefix { p.to_xml builder } }
54
- forename.each { |f| builder.forename { f.to_xml builder } }
55
- initial.each { |i| builder.initial { i.to_xml builder } }
55
+ pref = prefix.select { |p| p.language&.include? opts[:lang] }
56
+ pref = prefix unless pref.any?
57
+ pref.each { |p| builder.prefix { p.to_xml builder } }
58
+ frnm = forename.select { |f| f.language&.include? opts[:lang] }
59
+ frnm = forename unless frnm.any?
60
+ frnm.each { |f| builder.forename { f.to_xml builder } }
61
+ init = initial.select { |i| i.language&.include? opts[:lang] }
62
+ init = initial unless init.any?
63
+ init.each { |i| builder.initial { i.to_xml builder } }
56
64
  builder.surname { surname.to_xml builder }
57
- addition.each { |a| builder.addition { a.to_xml builder } }
65
+ addn = addition.select { |a| a.language&.include? opts[:lang] }
66
+ addn = addition unless addn.any?
67
+ addn.each { |a| builder.addition { a.to_xml builder } }
58
68
  end
59
69
  end
60
70
  end
61
71
 
62
72
  # @return [Hash]
63
- def to_hash # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
73
+ def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
64
74
  hash = {}
65
75
  hash["forename"] = single_element_array(forename) if forename&.any?
66
76
  hash["initial"] = single_element_array(initial) if initial&.any?
@@ -73,7 +83,7 @@ module RelatonBib
73
83
 
74
84
  # @param pref [String]
75
85
  # @return [String]
76
- def to_asciibib(pref) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
86
+ def to_asciibib(pref) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
77
87
  prf = pref.empty? ? pref : pref + "."
78
88
  prf += "name."
79
89
  out = forename.map do |fn|
@@ -170,11 +180,13 @@ module RelatonBib
170
180
  @identifier = identifier
171
181
  end
172
182
 
173
- # @param builder [Nokogiri::XML::Builder]
174
- def to_xml(builder)
175
- builder.person do
176
- name.to_xml builder
177
- affiliation.each { |a| a.to_xml builder }
183
+ # @param opts [Hash]
184
+ # @option opts [Nokogiri::XML::Builder] :builder XML builder
185
+ # @option opts [String, Symbol] :lang language
186
+ def to_xml(**opts)
187
+ opts[:builder].person do |builder|
188
+ name.to_xml **opts
189
+ affiliation.each { |a| a.to_xml **opts }
178
190
  identifier.each { |id| id.to_xml builder }
179
191
  contact.each { |contact| contact.to_xml builder }
180
192
  end