Class: Relaton::Bibcollection
- Inherits:
-
Object
- Object
- Relaton::Bibcollection
- Defined in:
- lib/relaton/bibcollection.rb
Constant Summary collapse
- ATTRIBS =
i[ title items doctype ]
Class Method Summary collapse
Instance Method Summary collapse
-
#doc_number ⇒ Object
arbitrary number, must sort after all bib items.
-
#initialize(options) ⇒ Bibcollection
constructor
A new instance of Bibcollection.
- #items_flattened ⇒ Object
- #new_bib_item_class(options) ⇒ Object
- #to_h ⇒ Object
- #to_xml ⇒ Object
- #to_yaml ⇒ Object
Constructor Details
#initialize(options) ⇒ Bibcollection
Returns a new instance of Bibcollection.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/relaton/bibcollection.rb', line 20 def initialize() self.items = [] ATTRIBS.each do |k| value = [k] || [k.to_s] self.send("#{k}=", value) end self.items = (self.items || []).inject([]) do |acc,item| acc << if item.is_a?(::Relaton::Bibcollection) || item.is_a?(::Relaton::Bibdata) item else new_bib_item_class(item) end end self end |
Class Method Details
.from_xml(source) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/relaton/bibcollection.rb', line 42 def self.from_xml(source) title = source&.at(ns("./relaton-collection/title"))&.text = source&.at(ns("./relaton-collection/contributor[role/@type = 'author']/organization/name"))&.text items = source&.xpath(ns("./relaton-collection/relation"))&.map do |item| klass = item.at(ns("./bibdata")) ? Bibdata : Bibcollection klass.from_xml(item.at(ns("./bibdata")) || item) end opts = { title: title, author: , items: items } new(opts) end |
.ns(xpath) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/relaton/bibcollection.rb', line 13 def self.ns(xpath) xpath.gsub(%r{/([a-zA-z])}, "/xmlns:\\1"). gsub(%r{::([a-zA-z])}, "::xmlns:\\1"). gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1"). gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1") end |
Instance Method Details
#doc_number ⇒ Object
arbitrary number, must sort after all bib items
38 39 40 |
# File 'lib/relaton/bibcollection.rb', line 38 def doc_number 9999999 end |
#items_flattened ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/relaton/bibcollection.rb', line 61 def items_flattened items.sort_by! do |b| b.doc_number end items.inject([]) do |acc,item| if item.is_a? ::Relaton::Bibcollection acc << item.items_flattened else acc << item end end end |
#new_bib_item_class(options) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/relaton/bibcollection.rb', line 53 def new_bib_item_class() if ["items"] ::Relaton::Bibcollection.new() else ::Relaton::Bibdata.new() end end |
#to_h ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/relaton/bibcollection.rb', line 105 def to_h items.sort_by! do |b| b.doc_number end a = ATTRIBS.inject({}) do |acc, k| acc[k.to_s] = send(k) acc end a["items"] = a["items"].map(&:to_h) { "root" => a } end |
#to_xml ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/relaton/bibcollection.rb', line 75 def to_xml items.sort_by! do |b| b.doc_number end collection_type = if doctype "type=\"#{doctype}\"" else 'xmlns="https://open.ribose.com/relaton-xml"' end ret = "<relaton-collection #{collection_type}>" ret += "<title>#{title}</title>" if title if ret += "<contributor><role type='author'/><organization><name>#{author}</name></organization></contributor>" end unless items.empty? items.each do |item| ret += "<relation type='partOf'>" ret += item.to_xml ret += "</relation>\n" end end ret += "</relaton-collection>\n" end |
#to_yaml ⇒ Object
101 102 103 |
# File 'lib/relaton/bibcollection.rb', line 101 def to_yaml to_h.to_yaml end |