relaton-bib 0.3.4 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,11 +47,11 @@ module RelatonBib
47
47
  # @return [Hash]
48
48
  def to_hash
49
49
  hash = {}
50
- hash[:street] = street if street&.any?
51
- hash[:city] = city
52
- hash[:state] = state if state
53
- hash[:country] = country
54
- hash[:postcode] = postcode if postcode
50
+ hash["street"] = street if street&.any?
51
+ hash["city"] = city
52
+ hash["state"] = state if state
53
+ hash["country"] = country
54
+ hash["postcode"] = postcode if postcode
55
55
  hash
56
56
  end
57
57
  end
@@ -77,12 +77,14 @@ module RelatonBib
77
77
 
78
78
  # @return [Hash]
79
79
  def to_hash
80
- { type: type, value: value }
80
+ { "type" => type, "value" => value }
81
81
  end
82
82
  end
83
83
 
84
84
  # Affilation.
85
85
  class Affilation
86
+ include RelatonBib
87
+
86
88
  # @return [RelatonBib::LocalizedString, NilClass]
87
89
  attr_reader :name
88
90
 
@@ -113,14 +115,16 @@ module RelatonBib
113
115
  # @return [Hash]
114
116
  def to_hash
115
117
  hash = organization.to_hash
116
- hash[:name] = name.to_hash if name
117
- hash[:description] = description.map(&:to_hash) if description&.any?
118
+ hash["name"] = name.to_hash if name
119
+ hash["description"] = single_element_array(description) if description&.any?
118
120
  hash
119
121
  end
120
122
  end
121
123
 
122
124
  # Contributor.
123
125
  class Contributor
126
+ include RelatonBib
127
+
124
128
  # @return [URI]
125
129
  attr_reader :uri
126
130
 
@@ -148,8 +152,8 @@ module RelatonBib
148
152
  # @return [Hash]
149
153
  def to_hash
150
154
  hash = {}
151
- hash[:url] = uri.to_s if uri
152
- hash[:contact] = contact.map(&:to_hash) if contact&.any?
155
+ hash["url"] = uri.to_s if uri
156
+ hash["contact"] = single_element_array(contact) if contact&.any?
153
157
  hash
154
158
  end
155
159
  end
@@ -22,14 +22,14 @@ module RelatonBib
22
22
  else owner
23
23
  end
24
24
 
25
- @from = Date.strptime(from, "%Y") unless from.empty?
26
- @to = Date.strptime(to, "%Y") unless to.to_s.empty?
25
+ @from = Date.strptime(from.to_s, "%Y") if from.to_s =~ /\d{4}/
26
+ @to = Date.strptime(to.to_s, "%Y") unless to.to_s.empty?
27
27
  end
28
28
 
29
29
  # @param builder [Nokogiri::XML::Builder]
30
30
  def to_xml(builder)
31
31
  builder.copyright do
32
- builder.from from.year
32
+ builder.from from ? from.year : "unknown"
33
33
  builder.to to.year if to
34
34
  builder.owner { owner.to_xml builder }
35
35
  end
@@ -37,8 +37,8 @@ module RelatonBib
37
37
 
38
38
  # @return [Hash]
39
39
  def to_hash
40
- hash = { owner: owner.to_hash[:organization], from: from.to_s }
41
- hash[:to] = to.to_s if to
40
+ hash = { "owner" => owner.to_hash["organization"], "from" => from.year.to_s }
41
+ hash["to"] = to.year.to_s if to
42
42
  hash
43
43
  end
44
44
  end
@@ -46,8 +46,8 @@ module RelatonBib
46
46
 
47
47
  # @return [Hash]
48
48
  def to_hash
49
- hash = { id: id }
50
- hash[:type] if type
49
+ hash = { "id" => id }
50
+ hash["type"] = type if type
51
51
  hash
52
52
  end
53
53
  end
@@ -14,6 +14,8 @@ module RelatonBib
14
14
 
15
15
  # Documett relation
16
16
  class DocumentRelation
17
+ include RelatonBib
18
+
17
19
  # @return [String]
18
20
  attr_reader :type
19
21
 
@@ -50,8 +52,8 @@ module RelatonBib
50
52
 
51
53
  # @return [Hash]
52
54
  def to_hash
53
- hash = { type: type, bibitem: bibitem.to_hash }
54
- hash[:bib_locality] = bib_locality.map(&:to_hash) if bib_locality&.any?
55
+ hash = { "type" => type, "bibitem" => bibitem.to_hash }
56
+ hash["bib_locality"] = single_element_array(bib_locality) if bib_locality&.any?
55
57
  hash
56
58
  end
57
59
  end
@@ -35,9 +35,9 @@ module RelatonBib
35
35
 
36
36
  # @return [Hash]
37
37
  def to_hash
38
- hash = { stage: stage }
39
- hash[:substage] = substage if substage
40
- hash[:iteration] = iteration if iteration
38
+ hash = { "stage" => stage }
39
+ hash["substage"] = substage if substage
40
+ hash["iteration"] = iteration if iteration
41
41
  hash
42
42
  end
43
43
  end
@@ -13,8 +13,8 @@ module RelatonBib
13
13
  attr_reader :format
14
14
 
15
15
  # @param content [String]
16
- # @param language [String] language code Iso639
17
- # @param script [String] script code Iso15924
16
+ # @param language [String, NilClass] language code Iso639
17
+ # @param script [String, NilClass] script code Iso15924
18
18
  # @param format [String] the content type
19
19
  def initialize(content:, language: nil, script: nil, format: "text/plain")
20
20
  # if format && !FORMATS.include?(format)
@@ -34,7 +34,10 @@ module RelatonBib
34
34
  # @return [Hash]
35
35
  def to_hash
36
36
  hash = super
37
- hash[:format] = format if format
37
+ return hash unless format
38
+
39
+ hash = { "content" => hash } if hash.is_a? String
40
+ hash["format"] = format if format
38
41
  hash
39
42
  end
40
43
  end
@@ -35,6 +35,7 @@ module RelatonBib
35
35
 
36
36
  def extent_hash_to_bib(ret)
37
37
  return unless ret[:extent]
38
+
38
39
  ret[:extent] = array(ret[:extent])
39
40
  ret[:extent]&.each_with_index do |e, i|
40
41
  ret[:extent][i] = BibItemLocality.new(e[:type], e[:reference_from],
@@ -44,45 +45,57 @@ module RelatonBib
44
45
 
45
46
  def title_hash_to_bib(ret)
46
47
  return unless ret[:title]
48
+
47
49
  ret[:title] = array(ret[:title])
48
50
  ret[:title] = ret[:title].map do |t|
49
- t.is_a?(Hash) ? t : { content: t, language: "en", script: "Latn",
50
- format: "text/plain", type: "main" }
51
+ if t.is_a?(Hash) then t
52
+ else
53
+ { content: t, language: "en", script: "Latn", format: "text/plain", type: "main" }
54
+ end
51
55
  end
52
56
  end
53
57
 
54
58
  def language_hash_to_bib(ret)
55
59
  return unless ret[:language]
60
+
56
61
  ret[:language] = array(ret[:language])
57
62
  end
58
63
 
59
64
  def script_hash_to_bib(ret)
60
65
  return unless ret[:script]
66
+
61
67
  ret[:script] = array(ret[:script])
62
68
  end
63
69
 
64
70
  def abstract_hash_to_bib(ret)
65
71
  return unless ret[:abstract]
66
- ret[:abstract] = array(ret[:abstract])
72
+
73
+ ret[:abstract] = array(ret[:abstract]).map do |a|
74
+ a.is_a?(String) ? FormattedString.new(content: a) : a
75
+ end
67
76
  end
68
77
 
69
78
  def link_hash_to_bib(ret)
70
79
  return unless ret[:link]
80
+
71
81
  ret[:link] = array(ret[:link])
72
82
  end
73
83
 
74
84
  def place_hash_to_bib(ret)
75
85
  return unless ret[:place]
86
+
76
87
  ret[:place] = array(ret[:place])
77
88
  end
78
89
 
79
90
  def accesslocation_hash_to_bib(ret)
80
91
  return unless ret[:accesslocation]
92
+
81
93
  ret[:accesslocation] = array(ret[:accesslocation])
82
94
  end
83
95
 
84
96
  def dates_hash_to_bib(ret)
85
97
  return unless ret[:date]
98
+
86
99
  ret[:date] = array(ret[:date])
87
100
  ret[:date].each_with_index do |d, i|
88
101
  # value is synonym of on: it is reserved word in YAML
@@ -95,6 +108,7 @@ module RelatonBib
95
108
 
96
109
  def docid_hash_to_bib(ret)
97
110
  return unless ret[:docid]
111
+
98
112
  ret[:docid] = array(ret[:docid])
99
113
  ret[:docid]&.each_with_index do |id, i|
100
114
  ret[:docid][i] = DocumentIdentifier.new(id: id[:id], type: id[:type])
@@ -103,115 +117,129 @@ module RelatonBib
103
117
 
104
118
  def version_hash_to_bib(ret)
105
119
  return unless ret[:version]
120
+
106
121
  ret[:version][:draft] = array(ret[:version][:draft])
107
- ret[:version] and ret[:version] = BibliographicItem::Version.new(
108
- ret[:version][:revision_date], ret[:version][:draft])
122
+ ret[:version] && ret[:version] = BibliographicItem::Version.new(
123
+ ret[:version][:revision_date], ret[:version][:draft]
124
+ )
109
125
  end
110
126
 
111
127
  def biblionote_hash_to_bib(ret)
112
128
  return unless ret[:biblionote]
129
+
113
130
  ret[:biblionote] = array(ret[:biblionote])
114
131
  (ret[:biblionote])&.each_with_index do |n, i|
115
- ret[:biblionote][i] =
116
- BiblioNote.new(content: n[:content], type: n[:type],
117
- language: n[:language],
118
- script: n[:script], format: n[:format])
132
+ ret[:biblionote][i] = BiblioNote.new(
133
+ content: n[:content], type: n[:type], language: n[:language],
134
+ script: n[:script], format: n[:format]
135
+ )
119
136
  end
120
137
  end
121
138
 
122
139
  def formattedref_hash_to_bib(ret)
123
- ret[:formattedref] and ret[:formattedref] =
124
- formattedref(ret[:formattedref])
140
+ ret[:formattedref] && ret[:formattedref] = formattedref(ret[:formattedref])
125
141
  end
126
142
 
127
143
  def docstatus_hash_to_bib(ret)
128
- ret[:docstatus] and ret[:docstatus] =
129
- DocumentStatus.new(stage: ret[:docstatus][:stage],
130
- substage: ret[:docstatus][:substage],
131
- iteration: ret[:docstatus][:iteration])
144
+ ret[:docstatus] && ret[:docstatus] = DocumentStatus.new(
145
+ stage: ret[:docstatus][:stage],
146
+ substage: ret[:docstatus][:substage],
147
+ iteration: ret[:docstatus][:iteration],
148
+ )
132
149
  end
133
150
 
134
151
  def contributors_hash_to_bib(ret)
135
152
  return unless ret[:contributor]
153
+
136
154
  ret[:contributor] = array(ret[:contributor])
137
155
  ret[:contributor]&.each_with_index do |c, i|
138
156
  roles = array(ret[:contributor][i][:role]).map do |r|
139
157
  if r.is_a? Hash
140
158
  { type: r[:type], description: array(r[:description]) }
141
- elsif r.is_a? Array
142
- { type: r[0], description: r.fetch(1) }
159
+ # elsif r.is_a? Array
160
+ # { type: r[0], description: r.fetch(1) }
143
161
  else
144
162
  { type: r }
145
163
  end
146
164
  end
147
165
  ret[:contributor][i][:role] = roles
148
- ret[:contributor][i][:entity] = c[:person] ?
149
- person_hash_to_bib(c[:person]) : org_hash_to_bib(c[:organization])
166
+ ret[:contributor][i][:entity] = if c[:person]
167
+ person_hash_to_bib(c[:person])
168
+ else
169
+ org_hash_to_bib(c[:organization])
170
+ end
150
171
  ret[:contributor][i].delete(:person)
151
172
  ret[:contributor][i].delete(:organization)
152
173
  end
153
174
  end
154
175
 
155
- def org_hash_to_bib(c)
156
- return nil if c.nil?
157
- c[:identifier] = array(c[:identifier])&.map do |a|
176
+ def org_hash_to_bib(org)
177
+ return nil if org.nil?
178
+
179
+ org[:identifier] = array(org[:identifier])&.map do |a|
158
180
  OrgIdentifier.new(a[:type], a[:id])
159
181
  end
160
- c
182
+ org
161
183
  end
162
184
 
163
- def person_hash_to_bib(c)
185
+ def person_hash_to_bib(person)
164
186
  Person.new(
165
- name: fullname_hash_to_bib(c),
166
- affiliation: affiliation_hash_to_bib(c),
167
- contact: contacts_hash_to_bib(c),
168
- identifier: person_identifiers_hash_to_bib(c),
187
+ name: fullname_hash_to_bib(person),
188
+ affiliation: affiliation_hash_to_bib(person),
189
+ contact: contacts_hash_to_bib(person),
190
+ identifier: person_identifiers_hash_to_bib(person),
169
191
  )
170
192
  end
171
193
 
172
- def fullname_hash_to_bib(c)
173
- n = c[:name]
194
+ def fullname_hash_to_bib(person)
195
+ n = person[:name]
174
196
  FullName.new(
175
- forename: array(n[:forename])&.map { |f| localname(f, c) },
176
- initial: array(n[:initial])&.map { |f| localname(f, c) },
177
- addition: array(n[:addition])&.map { |f| localname(f, c) },
178
- prefix: array(n[:prefix])&.map { |f| localname(f, c) },
179
- surname: localname(n[:surname], c),
180
- completename: localname(n[:completename], c),
197
+ forename: array(n[:forename])&.map { |f| localname(f, person) },
198
+ initial: array(n[:initial])&.map { |f| localname(f, person) },
199
+ addition: array(n[:addition])&.map { |f| localname(f, person) },
200
+ prefix: array(n[:prefix])&.map { |f| localname(f, person) },
201
+ surname: localname(n[:surname], person),
202
+ completename: localname(n[:completename], person),
181
203
  )
182
204
  end
183
205
 
184
- def person_identifiers_hash_to_bib(c)
185
- array(c[:identifier])&.map do |a|
206
+ def person_identifiers_hash_to_bib(person)
207
+ array(person[:identifier])&.map do |a|
186
208
  PersonIdentifier.new(a[:type], a[:id])
187
209
  end
188
210
  end
189
211
 
190
- def affiliation_hash_to_bib(c)
191
- return [] unless c[:affiliation]
192
- array(c[:affiliation]).map do |a|
212
+ def affiliation_hash_to_bib(person)
213
+ return [] unless person[:affiliation]
214
+
215
+ array(person[:affiliation]).map do |a|
193
216
  a[:description] = array(a[:description])&.map do |d|
194
- FormattedString.new(
195
- d.is_a?(Hash) ?
196
- { content: d[:content], language: d[:language],
197
- script: d[:script], format: d[:format] } :
198
- { content: d })
217
+ cnt = if d.is_a?(Hash)
218
+ { content: d[:content], language: d[:language],
219
+ script: d[:script], format: d[:format] }
220
+ else { content: d }
221
+ end
222
+ FormattedString.new cnt
199
223
  end
200
224
  Affilation.new(
201
225
  organization: Organization.new(org_hash_to_bib(a[:organization])),
202
- description: a[:description]
226
+ description: a[:description],
203
227
  )
204
228
  end
205
229
  end
206
230
 
207
- def contacts_hash_to_bib(c)
208
- return [] unless c[:contact]
209
- array(c[:contact]).map do |a|
210
- (a[:city] || a[:country]) ?
231
+ def contacts_hash_to_bib(person)
232
+ return [] unless person[:contact]
233
+
234
+ array(person[:contact]).map do |a|
235
+ if a[:city] || a[:country]
211
236
  RelatonBib::Address.new(
212
237
  street: Array(a[:street]), city: a[:city], postcode: a[:postcode],
213
- country: a[:country], state: a[:state]) :
214
- RelatonBib::Contact.new(type: a[:type], value: a[:value])
238
+ country: a[:country], state: a[:state]
239
+ )
240
+ else
241
+ RelatonBib::Contact.new(type: a[:type], value: a[:value])
242
+ end
215
243
  end
216
244
  end
217
245
 
@@ -226,14 +254,14 @@ module RelatonBib
226
254
  end
227
255
 
228
256
  # @param ret [Hash]
229
- # @param r [Hash] relation
230
- # @param i [Integr] index of relation
231
- def relation_bibitem_hash_to_bib(ret, r, i)
232
- if r[:bibitem]
233
- ret[:relation][i][:bibitem] = bib_item(hash_to_bib(r[:bibitem], true))
257
+ # @param rel [Hash] relation
258
+ # @param idx [Integr] index of relation
259
+ def relation_bibitem_hash_to_bib(ret, rel, idx)
260
+ if rel[:bibitem]
261
+ ret[:relation][idx][:bibitem] = bib_item(hash_to_bib(rel[:bibitem], true))
234
262
  else
235
- warn "bibitem missing: #{r}"
236
- ret[:relation][i][:bibitem] = nil
263
+ warn "bibitem missing: #{rel}"
264
+ ret[:relation][idx][:bibitem] = nil
237
265
  end
238
266
  end
239
267
 
@@ -243,24 +271,23 @@ module RelatonBib
243
271
  BibliographicItem.new(item)
244
272
  end
245
273
 
246
- def relation_biblocality_hash_to_bib(ret, r, i)
247
- ret[:relation][i][:bib_locality] =
248
- array(r[:bib_locality])&.map do |bl|
274
+ def relation_biblocality_hash_to_bib(ret, rel, idx)
275
+ ret[:relation][idx][:bib_locality] =
276
+ array(rel[:bib_locality])&.map do |bl|
249
277
  BibItemLocality.new(bl[:type], bl[:reference_from],
250
278
  bl[:reference_to])
251
279
  end
252
280
  end
253
281
 
254
282
  def series_hash_to_bib(ret)
255
- array(ret[:series])&.each_with_index do |s, i|
256
- s[:formattedref] and s[:formattedref] = formattedref(s[:formattedref])
283
+ ret[:series] = array(ret[:series])&.map do |s|
284
+ s[:formattedref] && s[:formattedref] = formattedref(s[:formattedref])
257
285
  if s[:title]
258
286
  s[:title] = { content: s[:title] } unless s.is_a?(Hash)
259
287
  s[:title] = typed_title_strig(s[:title])
260
288
  end
261
- s[:abbreviation] and
262
- s[:abbreviation] = localizedstring(s[:abbreviation])
263
- ret[:series][i] = Series.new(s)
289
+ s[:abbreviation] && s[:abbreviation] = localizedstring(s[:abbreviation])
290
+ Series.new(s)
264
291
  end
265
292
  end
266
293
 
@@ -271,7 +298,7 @@ module RelatonBib
271
298
  end
272
299
 
273
300
  def medium_hash_to_bib(ret)
274
- ret[:medium] and ret[:medium] = Medium.new(ret[:medium])
301
+ ret[:medium] = Medium.new(ret[:medium]) if ret[:medium]
275
302
  end
276
303
 
277
304
  def classification_hash_to_bib(ret)
@@ -279,52 +306,70 @@ module RelatonBib
279
306
  # ret[:classification]&.each_with_index do |c, i|
280
307
  # ret[:classification][i] = RelatonBib::Classification.new(c)
281
308
  # end
282
- ret[:classification] and
309
+ if ret[:classification]
283
310
  ret[:classification] = Classification.new(ret[:classification])
311
+ end
284
312
  end
285
313
 
286
314
  def validity_hash_to_bib(ret)
287
315
  return unless ret[:validity]
288
- ret[:validity][:begins] and b = Time.parse(ret[:validity][:begins])
289
- ret[:validity][:ends] and e = Time.parse(ret[:validity][:ends])
290
- ret[:validity][:revision] and r = Time.parse(ret[:validity][:revision])
316
+
317
+ ret[:validity][:begins] && b = Time.parse(ret[:validity][:begins])
318
+ ret[:validity][:ends] && e = Time.parse(ret[:validity][:ends])
319
+ ret[:validity][:revision] && r = Time.parse(ret[:validity][:revision])
291
320
  ret[:validity] = Validity.new(begins: b, ends: e, revision: r)
292
321
  end
293
322
 
323
+ # @param ogj [Hash, Array, String]
324
+ # @return [Hash, Array, String]
294
325
  def symbolize(obj)
295
- obj.is_a? Hash and
296
- return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize(v); memo}
297
- obj.is_a? Array and
298
- return obj.inject([]){|memo,v | memo << symbolize(v); memo}
299
- return obj
326
+ if obj.is_a? Hash
327
+ obj.reduce({}) do |memo, (k, v)|
328
+ memo[k.to_sym] = symbolize(v)
329
+ memo
330
+ end
331
+ elsif obj.is_a? Array
332
+ obj.reduce([]) { |memo, v| memo << symbolize(v) }
333
+ else
334
+ obj
335
+ end
300
336
  end
301
337
 
302
- def array(a)
303
- return [] unless a
304
- return [a] unless a.is_a?(Array)
305
- a
338
+ def array(arr)
339
+ return [] unless arr
340
+ return [arr] unless arr.is_a?(Array)
341
+
342
+ arr
306
343
  end
307
344
 
308
- def localname(f, c)
309
- return nil if f.nil?
310
- f.is_a?(Hash) and lang = f[:language]
311
- lang ||= c[:name][:language]
312
- f.is_a?(Hash) and script = f[:script]
313
- script ||= c[:name][:script]
314
- f.is_a?(Hash) ?
315
- RelatonBib::LocalizedString.new(f[:content], lang, script) :
316
- RelatonBib::LocalizedString.new(f, lang, script)
345
+ def localname(name, person)
346
+ return nil if name.nil?
347
+
348
+ lang = name[:language] if name.is_a?(Hash)
349
+ lang ||= person[:name][:language]
350
+ script = name[:script] if name.is_a?(Hash)
351
+ script ||= person[:name][:script]
352
+ if name.is_a?(Hash)
353
+ RelatonBib::LocalizedString.new(name[:content], lang, script)
354
+ else
355
+ RelatonBib::LocalizedString.new(name, lang, script)
356
+ end
317
357
  end
318
358
 
319
- def localizedstring(f)
320
- f.is_a?(Hash) ?
321
- RelatonBib::LocalizedString.new(f[:content], f[:language], f[:script]) :
322
- RelatonBib::LocalizedString.new(f)
359
+ def localizedstring(lst)
360
+ if lst.is_a?(Hash)
361
+ RelatonBib::LocalizedString.new(lst[:content], lst[:language], lst[:script])
362
+ else
363
+ RelatonBib::LocalizedString.new(lst)
364
+ end
323
365
  end
324
366
 
325
- def formattedref(f)
326
- f.is_a?(Hash) ? RelatonBib::FormattedRef.new(f) :
327
- RelatonBib::FormattedRef.new(content: f)
367
+ def formattedref(frf)
368
+ if frf.is_a?(Hash)
369
+ RelatonBib::FormattedRef.new(frf)
370
+ else
371
+ RelatonBib::FormattedRef.new(content: frf)
372
+ end
328
373
  end
329
374
  end
330
375
  end