Class: RelatonBib::HashConverter
- Inherits:
-
Object
- Object
- RelatonBib::HashConverter
- Defined in:
- lib/relaton_bib/hash_converter.rb
Class Method Summary collapse
- .abstract_hash_to_bib(ret) ⇒ Object
- .accesslocation_hash_to_bib(ret) ⇒ Object
- .affiliation_hash_to_bib(person) ⇒ Object
- .array(arr) ⇒ Object
- .bib_item(item) ⇒ Object
- .biblionote_hash_to_bib(ret) ⇒ Object
- .classification_hash_to_bib(ret) ⇒ Object
- .contacts_hash_to_bib(person) ⇒ Object
- .contributors_hash_to_bib(ret) ⇒ Object
- .dates_hash_to_bib(ret) ⇒ Object
- .docid_hash_to_bib(ret) ⇒ Object
- .docstatus_hash_to_bib(ret) ⇒ Object
- .extent_hash_to_bib(ret) ⇒ Object
- .formattedref(frf) ⇒ Object
- .formattedref_hash_to_bib(ret) ⇒ Object
- .fullname_hash_to_bib(person) ⇒ Object
- .hash_to_bib(args, nested = false) ⇒ Object
- .language_hash_to_bib(ret) ⇒ Object
- .link_hash_to_bib(ret) ⇒ Object
- .localizedstring(lst) ⇒ Object
- .localname(name, person) ⇒ Object
- .medium_hash_to_bib(ret) ⇒ Object
- .org_hash_to_bib(org) ⇒ Object
- .person_hash_to_bib(person) ⇒ Object
- .person_identifiers_hash_to_bib(person) ⇒ Object
- .place_hash_to_bib(ret) ⇒ Object
- .relation_bibitem_hash_to_bib(ret, rel, idx) ⇒ Object
- .relation_biblocality_hash_to_bib(ret, rel, idx) ⇒ Object
- .relations_hash_to_bib(ret) ⇒ Object
- .script_hash_to_bib(ret) ⇒ Object
- .series_hash_to_bib(ret) ⇒ Object
- .symbolize(obj) ⇒ Hash, ...
- .timestamp_hash(ret) ⇒ Object
- .title_hash_to_bib(ret) ⇒ Object
- .typed_title_strig(title) ⇒ RelatonBib::TypedTitleString
- .validity_hash_to_bib(ret) ⇒ Object
- .version_hash_to_bib(ret) ⇒ Object
Class Method Details
.abstract_hash_to_bib(ret) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/relaton_bib/hash_converter.rb', line 72 def abstract_hash_to_bib(ret) return unless ret[:abstract] ret[:abstract] = array(ret[:abstract]).map do |a| a.is_a?(String) ? FormattedString.new(content: a) : a end end |
.accesslocation_hash_to_bib(ret) ⇒ Object
94 95 96 97 98 |
# File 'lib/relaton_bib/hash_converter.rb', line 94 def accesslocation_hash_to_bib(ret) return unless ret[:accesslocation] ret[:accesslocation] = array(ret[:accesslocation]) end |
.affiliation_hash_to_bib(person) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/relaton_bib/hash_converter.rb', line 221 def affiliation_hash_to_bib(person) return [] unless person[:affiliation] array(person[:affiliation]).map do |a| a[:description] = array(a[:description])&.map do |d| cnt = if d.is_a?(Hash) { content: d[:content], language: d[:language], script: d[:script], format: d[:format] } else { content: d } end FormattedString.new cnt end Affilation.new( organization: Organization.new(org_hash_to_bib(a[:organization])), description: a[:description], ) end end |
.array(arr) ⇒ Object
347 348 349 350 351 352 |
# File 'lib/relaton_bib/hash_converter.rb', line 347 def array(arr) return [] unless arr return [arr] unless arr.is_a?(Array) arr end |
.bib_item(item) ⇒ Object
279 280 281 |
# File 'lib/relaton_bib/hash_converter.rb', line 279 def bib_item(item) BibliographicItem.new(item) end |
.biblionote_hash_to_bib(ret) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/relaton_bib/hash_converter.rb', line 132 def biblionote_hash_to_bib(ret) return unless ret[:biblionote] ret[:biblionote] = array(ret[:biblionote]) (ret[:biblionote])&.each_with_index do |n, i| ret[:biblionote][i] = if n.is_a?(String) BiblioNote.new content: n else BiblioNote.new( content: n[:content], type: n[:type], language: n[:language], script: n[:script], format: n[:format] ) end end end |
.classification_hash_to_bib(ret) ⇒ Object
313 314 315 316 317 318 319 320 321 |
# File 'lib/relaton_bib/hash_converter.rb', line 313 def classification_hash_to_bib(ret) # ret[:classification] = [ret[:classification]] unless ret[:classification].is_a?(Array) # ret[:classification]&.each_with_index do |c, i| # ret[:classification][i] = RelatonBib::Classification.new(c) # end if ret[:classification] ret[:classification] = array(ret[:classification]).map { |cls| Classification.new cls } end end |
.contacts_hash_to_bib(person) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/relaton_bib/hash_converter.rb', line 240 def contacts_hash_to_bib(person) return [] unless person[:contact] array(person[:contact]).map do |a| if a[:city] || a[:country] RelatonBib::Address.new( street: Array(a[:street]), city: a[:city], postcode: a[:postcode], country: a[:country], state: a[:state] ) else RelatonBib::Contact.new(type: a[:type], value: a[:value]) end end end |
.contributors_hash_to_bib(ret) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/relaton_bib/hash_converter.rb', line 160 def contributors_hash_to_bib(ret) return unless ret[:contributor] ret[:contributor] = array(ret[:contributor]) ret[:contributor]&.each_with_index do |c, i| roles = array(ret[:contributor][i][:role]).map do |r| if r.is_a? Hash { type: r[:type], description: array(r[:description]) } # elsif r.is_a? Array # { type: r[0], description: r.fetch(1) } else { type: r } end end ret[:contributor][i][:role] = roles ret[:contributor][i][:entity] = if c[:person] person_hash_to_bib(c[:person]) else org_hash_to_bib(c[:organization]) end ret[:contributor][i].delete(:person) ret[:contributor][i].delete(:organization) end end |
.dates_hash_to_bib(ret) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/relaton_bib/hash_converter.rb', line 100 def dates_hash_to_bib(ret) return unless ret[:date] ret[:date] = array(ret[:date]) ret[:date].each_with_index do |d, i| # value is synonym of on: it is reserved word in YAML if d[:value] ret[:date][i][:on] ||= d[:value] ret[:date][i].delete(:value) end end end |
.docid_hash_to_bib(ret) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/relaton_bib/hash_converter.rb', line 113 def docid_hash_to_bib(ret) return unless ret[:docid] ret[:docid] = array(ret[:docid]) ret[:docid]&.each_with_index do |id, i| type = id[:type] || id[:id].match(/^\w+/)&.to_s ret[:docid][i] = DocumentIdentifier.new(id: id[:id], type: type) end end |
.docstatus_hash_to_bib(ret) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/relaton_bib/hash_converter.rb', line 152 def docstatus_hash_to_bib(ret) ret[:docstatus] && ret[:docstatus] = DocumentStatus.new( stage: ret[:docstatus][:stage], substage: ret[:docstatus][:substage], iteration: ret[:docstatus][:iteration], ) end |
.extent_hash_to_bib(ret) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/relaton_bib/hash_converter.rb', line 38 def extent_hash_to_bib(ret) return unless ret[:extent] ret[:extent] = array(ret[:extent]) ret[:extent]&.each_with_index do |e, i| ret[:extent][i] = BibItemLocality.new(e[:type], e[:reference_from], e[:reference_to]) end end |
.formattedref(frf) ⇒ Object
376 377 378 379 380 381 382 |
# File 'lib/relaton_bib/hash_converter.rb', line 376 def formattedref(frf) if frf.is_a?(Hash) RelatonBib::FormattedRef.new(frf) else RelatonBib::FormattedRef.new(content: frf) end end |
.formattedref_hash_to_bib(ret) ⇒ Object
148 149 150 |
# File 'lib/relaton_bib/hash_converter.rb', line 148 def formattedref_hash_to_bib(ret) ret[:formattedref] && ret[:formattedref] = formattedref(ret[:formattedref]) end |
.fullname_hash_to_bib(person) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/relaton_bib/hash_converter.rb', line 203 def fullname_hash_to_bib(person) n = person[:name] FullName.new( forename: array(n[:forename])&.map { |f| localname(f, person) }, initial: array(n[:initial])&.map { |f| localname(f, person) }, addition: array(n[:addition])&.map { |f| localname(f, person) }, prefix: array(n[:prefix])&.map { |f| localname(f, person) }, surname: localname(n[:surname], person), completename: localname(n[:completename], person), ) end |
.hash_to_bib(args, nested = false) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/relaton_bib/hash_converter.rb', line 4 def hash_to_bib(args, nested = false) return nil unless args.is_a?(Hash) ret = Marshal.load(Marshal.dump(symbolize(args))) # deep copy (ret) unless nested 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) 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) accesslocation_hash_to_bib(ret) classification_hash_to_bib(ret) validity_hash_to_bib(ret) ret[:keyword] = array(ret[:keyword]) ret[:license] = array(ret[:license]) ret end |
.language_hash_to_bib(ret) ⇒ Object
60 61 62 63 64 |
# File 'lib/relaton_bib/hash_converter.rb', line 60 def language_hash_to_bib(ret) return unless ret[:language] ret[:language] = array(ret[:language]) end |
.link_hash_to_bib(ret) ⇒ Object
80 81 82 83 84 |
# File 'lib/relaton_bib/hash_converter.rb', line 80 def link_hash_to_bib(ret) return unless ret[:link] ret[:link] = array(ret[:link]) end |
.localizedstring(lst) ⇒ Object
368 369 370 371 372 373 374 |
# File 'lib/relaton_bib/hash_converter.rb', line 368 def localizedstring(lst) if lst.is_a?(Hash) RelatonBib::LocalizedString.new(lst[:content], lst[:language], lst[:script]) else RelatonBib::LocalizedString.new(lst) end end |
.localname(name, person) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/relaton_bib/hash_converter.rb', line 354 def localname(name, person) return nil if name.nil? lang = name[:language] if name.is_a?(Hash) lang ||= person[:name][:language] script = name[:script] if name.is_a?(Hash) script ||= person[:name][:script] if name.is_a?(Hash) RelatonBib::LocalizedString.new(name[:content], lang, script) else RelatonBib::LocalizedString.new(name, lang, script) end end |
.medium_hash_to_bib(ret) ⇒ Object
309 310 311 |
# File 'lib/relaton_bib/hash_converter.rb', line 309 def medium_hash_to_bib(ret) ret[:medium] = Medium.new(ret[:medium]) if ret[:medium] end |
.org_hash_to_bib(org) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/relaton_bib/hash_converter.rb', line 185 def org_hash_to_bib(org) return nil if org.nil? org[:identifier] = array(org[:identifier])&.map do |a| OrgIdentifier.new(a[:type], a[:id]) end org end |
.person_hash_to_bib(person) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/relaton_bib/hash_converter.rb', line 194 def person_hash_to_bib(person) Person.new( name: fullname_hash_to_bib(person), affiliation: affiliation_hash_to_bib(person), contact: contacts_hash_to_bib(person), identifier: person_identifiers_hash_to_bib(person), ) end |
.person_identifiers_hash_to_bib(person) ⇒ Object
215 216 217 218 219 |
# File 'lib/relaton_bib/hash_converter.rb', line 215 def person_identifiers_hash_to_bib(person) array(person[:identifier])&.map do |a| PersonIdentifier.new(a[:type], a[:id]) end end |
.place_hash_to_bib(ret) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/relaton_bib/hash_converter.rb', line 86 def place_hash_to_bib(ret) return unless ret[:place] ret[:place] = array(ret[:place]).map do |pl| pl.is_a?(String) ? Place.new(name: pl) : Place.new(pl) end end |
.relation_bibitem_hash_to_bib(ret, rel, idx) ⇒ Object
268 269 270 271 272 273 274 275 |
# File 'lib/relaton_bib/hash_converter.rb', line 268 def relation_bibitem_hash_to_bib(ret, rel, idx) if rel[:bibitem] ret[:relation][idx][:bibitem] = bib_item(hash_to_bib(rel[:bibitem], true)) else warn "bibitem missing: #{rel}" ret[:relation][idx][:bibitem] = nil end end |
.relation_biblocality_hash_to_bib(ret, rel, idx) ⇒ Object
283 284 285 286 287 288 289 |
# File 'lib/relaton_bib/hash_converter.rb', line 283 def relation_biblocality_hash_to_bib(ret, rel, idx) ret[:relation][idx][:bib_locality] = array(rel[:bib_locality])&.map do |bl| BibItemLocality.new(bl[:type], bl[:reference_from], bl[:reference_to]) end end |
.relations_hash_to_bib(ret) ⇒ Object
255 256 257 258 259 260 261 262 263 |
# File 'lib/relaton_bib/hash_converter.rb', line 255 def relations_hash_to_bib(ret) return unless ret[:relation] ret[:relation] = array(ret[:relation]) ret[:relation]&.each_with_index do |r, i| relation_bibitem_hash_to_bib(ret, r, i) relation_biblocality_hash_to_bib(ret, r, i) end end |
.script_hash_to_bib(ret) ⇒ Object
66 67 68 69 70 |
# File 'lib/relaton_bib/hash_converter.rb', line 66 def script_hash_to_bib(ret) return unless ret[:script] ret[:script] = array(ret[:script]) end |
.series_hash_to_bib(ret) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/relaton_bib/hash_converter.rb', line 291 def series_hash_to_bib(ret) ret[:series] = array(ret[:series])&.map do |s| s[:formattedref] && s[:formattedref] = formattedref(s[:formattedref]) if s[:title] s[:title] = { content: s[:title] } unless s.is_a?(Hash) s[:title] = typed_title_strig(s[:title]) end s[:abbreviation] && s[:abbreviation] = localizedstring(s[:abbreviation]) Series.new(s) end end |
.symbolize(obj) ⇒ Hash, ...
334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/relaton_bib/hash_converter.rb', line 334 def symbolize(obj) if obj.is_a? Hash obj.reduce({}) do |memo, (k, v)| memo[k.to_sym] = symbolize(v) memo end elsif obj.is_a? Array obj.reduce([]) { |memo, v| memo << symbolize(v) } else obj end end |
.timestamp_hash(ret) ⇒ Object
34 35 36 |
# File 'lib/relaton_bib/hash_converter.rb', line 34 def (ret) ret[:fetched] ||= Date.today.to_s end |
.title_hash_to_bib(ret) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/relaton_bib/hash_converter.rb', line 48 def title_hash_to_bib(ret) return unless ret[:title] ret[:title] = array(ret[:title]) ret[:title] = ret[:title].map do |t| if t.is_a?(Hash) then t else { content: t, language: "en", script: "Latn", format: "text/plain", type: "main" } end end end |
.typed_title_strig(title) ⇒ RelatonBib::TypedTitleString
305 306 307 |
# File 'lib/relaton_bib/hash_converter.rb', line 305 def typed_title_strig(title) TypedTitleString.new title end |
.validity_hash_to_bib(ret) ⇒ Object
323 324 325 326 327 328 329 330 |
# File 'lib/relaton_bib/hash_converter.rb', line 323 def validity_hash_to_bib(ret) return unless ret[:validity] ret[:validity][:begins] && b = Time.parse(ret[:validity][:begins]) ret[:validity][:ends] && e = Time.parse(ret[:validity][:ends]) ret[:validity][:revision] && r = Time.parse(ret[:validity][:revision]) ret[:validity] = Validity.new(begins: b, ends: e, revision: r) end |
.version_hash_to_bib(ret) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/relaton_bib/hash_converter.rb', line 123 def version_hash_to_bib(ret) return unless ret[:version] ret[:version][:draft] = array(ret[:version][:draft]) ret[:version] && ret[:version] = BibliographicItem::Version.new( ret[:version][:revision_date], ret[:version][:draft] ) end |