relaton-bib 0.3.3 → 0.3.8

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.
@@ -1,10 +1,10 @@
1
1
  module RelatonBib
2
2
  # Copyright association.
3
3
  class CopyrightAssociation
4
- # @return [Time]
4
+ # @return [Date]
5
5
  attr_reader :from
6
6
 
7
- # @return [Time]
7
+ # @return [Date, NilClass]
8
8
  attr_reader :to
9
9
 
10
10
  # @return [RelatonBib::ContributionInfo]
@@ -22,17 +22,24 @@ module RelatonBib
22
22
  else owner
23
23
  end
24
24
 
25
- @from = Time.strptime(from, "%Y") unless from.empty?
26
- @to = Time.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
36
36
  end
37
+
38
+ # @return [Hash]
39
+ def to_hash
40
+ hash = { "owner" => owner.to_hash["organization"], "from" => from.year.to_s }
41
+ hash["to"] = to.year.to_s if to
42
+ hash
43
+ end
37
44
  end
38
45
  end
@@ -43,5 +43,12 @@ module RelatonBib
43
43
  def to_xml(builder)
44
44
  builder.docidentifier(id, type: type)
45
45
  end
46
+
47
+ # @return [Hash]
48
+ def to_hash
49
+ hash = { "id" => id }
50
+ hash["type"] = type if type
51
+ hash
52
+ end
46
53
  end
47
54
  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
 
@@ -47,5 +49,12 @@ module RelatonBib
47
49
  end
48
50
  end
49
51
  end
52
+
53
+ # @return [Hash]
54
+ def to_hash
55
+ hash = { "type" => type, "bibitem" => bibitem.to_hash }
56
+ hash["bib_locality"] = single_element_array(bib_locality) if bib_locality&.any?
57
+ hash
58
+ end
50
59
  end
51
60
  end
@@ -32,5 +32,13 @@ module RelatonBib
32
32
  builder.iteration iteration unless iteration.to_s.empty?
33
33
  end
34
34
  end
35
+
36
+ # @return [Hash]
37
+ def to_hash
38
+ hash = { "stage" => stage }
39
+ hash["substage"] = substage if substage
40
+ hash["iteration"] = iteration if iteration
41
+ hash
42
+ end
35
43
  end
36
44
  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)
@@ -30,5 +30,15 @@ module RelatonBib
30
30
  builder.parent["format"] = format if format
31
31
  super
32
32
  end
33
+
34
+ # @return [Hash]
35
+ def to_hash
36
+ hash = super
37
+ return hash unless format
38
+
39
+ hash = { "content" => hash } if hash.is_a? String
40
+ hash["format"] = format if format
41
+ hash
42
+ end
33
43
  end
34
44
  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,118 +117,135 @@ 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
- Organization.new(org_hash_to_bib(a[:organization])), a[:description])
225
+ organization: Organization.new(org_hash_to_bib(a[:organization])),
226
+ description: a[:description],
227
+ )
202
228
  end
203
229
  end
204
230
 
205
- def contacts_hash_to_bib(c)
206
- return [] unless c[:contact]
207
- array(c[:contact]).map do |a|
208
- (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]
209
236
  RelatonBib::Address.new(
210
237
  street: Array(a[:street]), city: a[:city], postcode: a[:postcode],
211
- country: a[:country], state: a[:state]) :
212
- 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
213
243
  end
214
244
  end
215
245
 
216
246
  def relations_hash_to_bib(ret)
217
247
  return unless ret[:relation]
248
+
218
249
  ret[:relation] = array(ret[:relation])
219
250
  ret[:relation]&.each_with_index do |r, i|
220
251
  relation_bibitem_hash_to_bib(ret, r, i)
@@ -222,91 +253,123 @@ module RelatonBib
222
253
  end
223
254
  end
224
255
 
225
- def relation_bibitem_hash_to_bib(ret, r, i)
226
- if r[:bibitem] then ret[:relation][i][:bibitem] =
227
- BibliographicItem.new(hash_to_bib(r[:bibitem], true))
256
+ # @param ret [Hash]
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))
228
262
  else
229
- warn "bibitem missing: #{r}"
230
- ret[:relation][i][:bibitem] = nil
263
+ warn "bibitem missing: #{rel}"
264
+ ret[:relation][idx][:bibitem] = nil
231
265
  end
232
266
  end
233
267
 
234
- def relation_biblocality_hash_to_bib(ret, r, i)
235
- ret[:relation][i][:bib_locality] =
236
- array(r[:bib_locality])&.map do |bl|
268
+ # @param item [Hash]
269
+ # @retirn [RelatonBib::BibliographicItem]
270
+ def bib_item(item)
271
+ BibliographicItem.new(item)
272
+ end
273
+
274
+ def relation_biblocality_hash_to_bib(ret, rel, idx)
275
+ ret[:relation][idx][:bib_locality] =
276
+ array(rel[:bib_locality])&.map do |bl|
237
277
  BibItemLocality.new(bl[:type], bl[:reference_from],
238
278
  bl[:reference_to])
239
279
  end
240
280
  end
241
281
 
242
282
  def series_hash_to_bib(ret)
243
- array(ret[:series])&.each_with_index do |s, i|
244
- 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])
245
285
  if s[:title]
246
286
  s[:title] = { content: s[:title] } unless s.is_a?(Hash)
247
- s[:title] = TypedTitleString.new(s[:title])
287
+ s[:title] = typed_title_strig(s[:title])
248
288
  end
249
- s[:abbreviation] and
250
- s[:abbreviation] = localizedstring(s[:abbreviation])
251
- ret[:series][i] = Series.new(s)
289
+ s[:abbreviation] && s[:abbreviation] = localizedstring(s[:abbreviation])
290
+ Series.new(s)
252
291
  end
253
292
  end
254
293
 
294
+ # @param title [Hash]
295
+ # @return [RelatonBib::TypedTitleString]
296
+ def typed_title_strig(title)
297
+ TypedTitleString.new title
298
+ end
299
+
255
300
  def medium_hash_to_bib(ret)
256
- ret[:medium] and ret[:medium] = Medium.new(ret[:medium])
301
+ ret[:medium] = Medium.new(ret[:medium]) if ret[:medium]
257
302
  end
258
303
 
259
304
  def classification_hash_to_bib(ret)
260
- #ret[:classification] = [ret[:classification]] unless ret[:classification].is_a?(Array)
261
- #ret[:classification]&.each_with_index do |c, i|
262
- #ret[:classification][i] = RelatonBib::Classification.new(c)
263
- #end
264
- ret[:classification] and
305
+ # ret[:classification] = [ret[:classification]] unless ret[:classification].is_a?(Array)
306
+ # ret[:classification]&.each_with_index do |c, i|
307
+ # ret[:classification][i] = RelatonBib::Classification.new(c)
308
+ # end
309
+ if ret[:classification]
265
310
  ret[:classification] = Classification.new(ret[:classification])
311
+ end
266
312
  end
267
313
 
268
314
  def validity_hash_to_bib(ret)
269
315
  return unless ret[:validity]
270
- ret[:validity][:begins] and b = Time.parse(ret[:validity][:begins])
271
- ret[:validity][:ends] and e = Time.parse(ret[:validity][:ends])
272
- 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])
273
320
  ret[:validity] = Validity.new(begins: b, ends: e, revision: r)
274
321
  end
275
322
 
323
+ # @param ogj [Hash, Array, String]
324
+ # @return [Hash, Array, String]
276
325
  def symbolize(obj)
277
- obj.is_a? Hash and
278
- return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize(v); memo}
279
- obj.is_a? Array and
280
- return obj.inject([]){|memo,v | memo << symbolize(v); memo}
281
- 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
282
336
  end
283
337
 
284
- def array(a)
285
- return [] unless a
286
- return [a] unless a.is_a?(Array)
287
- a
338
+ def array(arr)
339
+ return [] unless arr
340
+ return [arr] unless arr.is_a?(Array)
341
+
342
+ arr
288
343
  end
289
344
 
290
- def localname(f, c)
291
- return nil if f.nil?
292
- f.is_a?(Hash) and lang = f[:language]
293
- lang ||= c[:name][:language]
294
- f.is_a?(Hash) and script = f[:script]
295
- script ||= c[:name][:script]
296
- f.is_a?(Hash) ?
297
- RelatonBib::LocalizedString.new(f[:content], lang, script) :
298
- 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
299
357
  end
300
358
 
301
- def localizedstring(f)
302
- f.is_a?(Hash) ?
303
- RelatonBib::LocalizedString.new(f[:content], f[:language], f[:script]) :
304
- 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
305
365
  end
306
366
 
307
- def formattedref(f)
308
- f.is_a?(Hash) ? RelatonBib::FormattedRef.new(f) :
309
- 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
310
373
  end
311
374
  end
312
375
  end