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(c) ⇒ Object
- .array(a) ⇒ Object
- .bib_item(item) ⇒ Object
- .biblionote_hash_to_bib(ret) ⇒ Object
- .classification_hash_to_bib(ret) ⇒ Object
- .contacts_hash_to_bib(c) ⇒ 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(f) ⇒ Object
- .formattedref_hash_to_bib(ret) ⇒ Object
- .fullname_hash_to_bib(c) ⇒ Object
- .hash_to_bib(args, nested = false) ⇒ Object
- .language_hash_to_bib(ret) ⇒ Object
- .link_hash_to_bib(ret) ⇒ Object
- .localizedstring(f) ⇒ Object
- .localname(f, c) ⇒ Object
- .medium_hash_to_bib(ret) ⇒ Object
- .org_hash_to_bib(c) ⇒ Object
- .person_hash_to_bib(c) ⇒ Object
- .person_identifiers_hash_to_bib(c) ⇒ Object
- .place_hash_to_bib(ret) ⇒ Object
- .relation_bibitem_hash_to_bib(ret, r, i) ⇒ Object
- .relation_biblocality_hash_to_bib(ret, r, i) ⇒ Object
- .relations_hash_to_bib(ret) ⇒ Object
- .script_hash_to_bib(ret) ⇒ Object
- .series_hash_to_bib(ret) ⇒ Object
- .symbolize(obj) ⇒ Object
- .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
64 65 66 67 |
# File 'lib/relaton_bib/hash_converter.rb', line 64 def abstract_hash_to_bib(ret) return unless ret[:abstract] ret[:abstract] = array(ret[:abstract]) end |
.accesslocation_hash_to_bib(ret) ⇒ Object
79 80 81 82 |
# File 'lib/relaton_bib/hash_converter.rb', line 79 def accesslocation_hash_to_bib(ret) return unless ret[:accesslocation] ret[:accesslocation] = array(ret[:accesslocation]) end |
.affiliation_hash_to_bib(c) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/relaton_bib/hash_converter.rb', line 191 def affiliation_hash_to_bib(c) return [] unless c[:affiliation] array(c[:affiliation]).map do |a| a[:description] = array(a[:description])&.map do |d| FormattedString.new( d.is_a?(Hash) ? { content: d[:content], language: d[:language], script: d[:script], format: d[:format] } : { content: d }) end Affilation.new( organization: Organization.new(org_hash_to_bib(a[:organization])), description: a[:description] ) end end |
.array(a) ⇒ Object
302 303 304 305 306 |
# File 'lib/relaton_bib/hash_converter.rb', line 302 def array(a) return [] unless a return [a] unless a.is_a?(Array) a end |
.bib_item(item) ⇒ Object
243 244 245 |
# File 'lib/relaton_bib/hash_converter.rb', line 243 def bib_item(item) BibliographicItem.new(item) end |
.biblionote_hash_to_bib(ret) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/relaton_bib/hash_converter.rb', line 111 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] = BiblioNote.new(content: n[:content], type: n[:type], language: n[:language], script: n[:script], format: n[:format]) end end |
.classification_hash_to_bib(ret) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/relaton_bib/hash_converter.rb', line 277 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 ret[:classification] and ret[:classification] = Classification.new(ret[:classification]) end |
.contacts_hash_to_bib(c) ⇒ Object
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/relaton_bib/hash_converter.rb', line 208 def contacts_hash_to_bib(c) return [] unless c[:contact] array(c[:contact]).map do |a| (a[:city] || a[:country]) ? RelatonBib::Address.new( street: Array(a[:street]), city: a[:city], postcode: a[:postcode], country: a[:country], state: a[:state]) : RelatonBib::Contact.new(type: a[:type], value: a[:value]) end end |
.contributors_hash_to_bib(ret) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/relaton_bib/hash_converter.rb', line 134 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] = c[:person] ? person_hash_to_bib(c[:person]) : org_hash_to_bib(c[:organization]) ret[:contributor][i].delete(:person) ret[:contributor][i].delete(:organization) end end |
.dates_hash_to_bib(ret) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/relaton_bib/hash_converter.rb', line 84 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
96 97 98 99 100 101 102 |
# File 'lib/relaton_bib/hash_converter.rb', line 96 def docid_hash_to_bib(ret) return unless ret[:docid] ret[:docid] = array(ret[:docid]) ret[:docid]&.each_with_index do |id, i| ret[:docid][i] = DocumentIdentifier.new(id: id[:id], type: id[:type]) end end |
.docstatus_hash_to_bib(ret) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/relaton_bib/hash_converter.rb', line 127 def docstatus_hash_to_bib(ret) ret[:docstatus] and ret[:docstatus] = DocumentStatus.new(stage: ret[:docstatus][:stage], substage: ret[:docstatus][:substage], iteration: ret[:docstatus][:iteration]) end |
.extent_hash_to_bib(ret) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/relaton_bib/hash_converter.rb', line 36 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(f) ⇒ Object
325 326 327 328 |
# File 'lib/relaton_bib/hash_converter.rb', line 325 def formattedref(f) f.is_a?(Hash) ? RelatonBib::FormattedRef.new(f) : RelatonBib::FormattedRef.new(content: f) end |
.formattedref_hash_to_bib(ret) ⇒ Object
122 123 124 125 |
# File 'lib/relaton_bib/hash_converter.rb', line 122 def formattedref_hash_to_bib(ret) ret[:formattedref] and ret[:formattedref] = formattedref(ret[:formattedref]) end |
.fullname_hash_to_bib(c) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/relaton_bib/hash_converter.rb', line 173 def fullname_hash_to_bib(c) n = c[:name] FullName.new( forename: array(n[:forename])&.map { |f| localname(f, c) }, initial: array(n[:initial])&.map { |f| localname(f, c) }, addition: array(n[:addition])&.map { |f| localname(f, c) }, prefix: array(n[:prefix])&.map { |f| localname(f, c) }, surname: localname(n[:surname], c), completename: localname(n[:completename], c), ) 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 |
# 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 end |
.language_hash_to_bib(ret) ⇒ Object
54 55 56 57 |
# File 'lib/relaton_bib/hash_converter.rb', line 54 def language_hash_to_bib(ret) return unless ret[:language] ret[:language] = array(ret[:language]) end |
.link_hash_to_bib(ret) ⇒ Object
69 70 71 72 |
# File 'lib/relaton_bib/hash_converter.rb', line 69 def link_hash_to_bib(ret) return unless ret[:link] ret[:link] = array(ret[:link]) end |
.localizedstring(f) ⇒ Object
319 320 321 322 323 |
# File 'lib/relaton_bib/hash_converter.rb', line 319 def localizedstring(f) f.is_a?(Hash) ? RelatonBib::LocalizedString.new(f[:content], f[:language], f[:script]) : RelatonBib::LocalizedString.new(f) end |
.localname(f, c) ⇒ Object
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/relaton_bib/hash_converter.rb', line 308 def localname(f, c) return nil if f.nil? f.is_a?(Hash) and lang = f[:language] lang ||= c[:name][:language] f.is_a?(Hash) and script = f[:script] script ||= c[:name][:script] f.is_a?(Hash) ? RelatonBib::LocalizedString.new(f[:content], lang, script) : RelatonBib::LocalizedString.new(f, lang, script) end |
.medium_hash_to_bib(ret) ⇒ Object
273 274 275 |
# File 'lib/relaton_bib/hash_converter.rb', line 273 def medium_hash_to_bib(ret) ret[:medium] and ret[:medium] = Medium.new(ret[:medium]) end |
.org_hash_to_bib(c) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/relaton_bib/hash_converter.rb', line 156 def org_hash_to_bib(c) return nil if c.nil? c[:identifier] = array(c[:identifier])&.map do |a| OrgIdentifier.new(a[:type], a[:id]) end c end |
.person_hash_to_bib(c) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/relaton_bib/hash_converter.rb', line 164 def person_hash_to_bib(c) Person.new( name: fullname_hash_to_bib(c), affiliation: affiliation_hash_to_bib(c), contact: contacts_hash_to_bib(c), identifier: person_identifiers_hash_to_bib(c), ) end |
.person_identifiers_hash_to_bib(c) ⇒ Object
185 186 187 188 189 |
# File 'lib/relaton_bib/hash_converter.rb', line 185 def person_identifiers_hash_to_bib(c) array(c[:identifier])&.map do |a| PersonIdentifier.new(a[:type], a[:id]) end end |
.place_hash_to_bib(ret) ⇒ Object
74 75 76 77 |
# File 'lib/relaton_bib/hash_converter.rb', line 74 def place_hash_to_bib(ret) return unless ret[:place] ret[:place] = array(ret[:place]) end |
.relation_bibitem_hash_to_bib(ret, r, i) ⇒ Object
232 233 234 235 236 237 238 239 |
# File 'lib/relaton_bib/hash_converter.rb', line 232 def relation_bibitem_hash_to_bib(ret, r, i) if r[:bibitem] ret[:relation][i][:bibitem] = bib_item(hash_to_bib(r[:bibitem], true)) else warn "bibitem missing: #{r}" ret[:relation][i][:bibitem] = nil end end |
.relation_biblocality_hash_to_bib(ret, r, i) ⇒ Object
247 248 249 250 251 252 253 |
# File 'lib/relaton_bib/hash_converter.rb', line 247 def relation_biblocality_hash_to_bib(ret, r, i) ret[:relation][i][:bib_locality] = array(r[:bib_locality])&.map do |bl| BibItemLocality.new(bl[:type], bl[:reference_from], bl[:reference_to]) end end |
.relations_hash_to_bib(ret) ⇒ Object
219 220 221 222 223 224 225 226 227 |
# File 'lib/relaton_bib/hash_converter.rb', line 219 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
59 60 61 62 |
# File 'lib/relaton_bib/hash_converter.rb', line 59 def script_hash_to_bib(ret) return unless ret[:script] ret[:script] = array(ret[:script]) end |
.series_hash_to_bib(ret) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/relaton_bib/hash_converter.rb', line 255 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) ⇒ Object
294 295 296 297 298 299 300 |
# File 'lib/relaton_bib/hash_converter.rb', line 294 def symbolize(obj) obj.is_a? Hash and return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize(v); memo} obj.is_a? Array and return obj.inject([]){|memo,v | memo << symbolize(v); memo} return obj end |
.timestamp_hash(ret) ⇒ Object
32 33 34 |
# File 'lib/relaton_bib/hash_converter.rb', line 32 def (ret) ret[:fetched] ||= Date.today.to_s end |
.title_hash_to_bib(ret) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/relaton_bib/hash_converter.rb', line 45 def title_hash_to_bib(ret) return unless ret[:title] ret[:title] = array(ret[:title]) ret[:title] = ret[:title].map do |t| t.is_a?(Hash) ? t : { content: t, language: "en", script: "Latn", format: "text/plain", type: "main" } end end |
.typed_title_strig(title) ⇒ RelatonBib::TypedTitleString
269 270 271 |
# File 'lib/relaton_bib/hash_converter.rb', line 269 def typed_title_strig(title) TypedTitleString.new title end |
.validity_hash_to_bib(ret) ⇒ Object
286 287 288 289 290 291 292 |
# File 'lib/relaton_bib/hash_converter.rb', line 286 def validity_hash_to_bib(ret) return unless ret[:validity] ret[:validity][:begins] and b = Time.parse(ret[:validity][:begins]) ret[:validity][:ends] and e = Time.parse(ret[:validity][:ends]) ret[:validity][:revision] and r = Time.parse(ret[:validity][:revision]) ret[:validity] = Validity.new(begins: b, ends: e, revision: r) end |
.version_hash_to_bib(ret) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/relaton_bib/hash_converter.rb', line 104 def version_hash_to_bib(ret) return unless ret[:version] ret[:version][:draft] = array(ret[:version][:draft]) ret[:version] and ret[:version] = BibliographicItem::Version.new( ret[:version][:revision_date], ret[:version][:draft]) end |