Class: Relaton::Bibcollection
- Inherits:
-
Object
- Object
- Relaton::Bibcollection
- Extended by:
- ElementFinder
- Defined in:
- lib/relaton/bibcollection.rb
Constant Summary collapse
- ATTRIBS =
%i[title items doctype author].freeze
Instance Attribute Summary
Attributes included from ElementFinder
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(item) ⇒ Object
Add a dcoument to the collection.
-
#doc_number ⇒ Object
arbitrary number, must sort after all bib items.
-
#initialize(options) ⇒ Bibcollection
constructor
A new instance of Bibcollection.
-
#items_flattened ⇒ Object
rubocop:enable Metrics/MethodLength.
- #new_bib_item_class(item) ⇒ Relaton::Bibdata, Relaton::Bibcollection
- #to_h ⇒ Object
-
#to_xml(opts = {}) ⇒ String
XML.
- #to_yaml ⇒ Object
Methods included from ElementFinder
apply_namespace, find, find_text, find_xpath
Constructor Details
#initialize(options) ⇒ Bibcollection
Returns a new instance of Bibcollection.
12 13 14 15 16 17 18 |
# File 'lib/relaton/bibcollection.rb', line 12 def initialize() ATTRIBS.each do |k| value = [k] || [k.to_s] send("#{k}=", value) end reduce_items end |
Class Method Details
.from_xml(source) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/relaton/bibcollection.rb', line 34 def self.from_xml(source) title = find_text("./relaton-collection/title", source) = find_text( "./relaton-collection/contributor[role/@type='author']/organization/"\ "name", source ) items = find_xpath("./relaton-collection/relation", source)&.map do |item| bibdata = find("./bibdata|./bibitem", item) klass = bibdata ? Bibdata : Bibcollection klass.from_xml(bibdata || item) end new(title: title, author: , items: items) end |
Instance Method Details
#<<(item) ⇒ Object
Add a dcoument to the collection
27 28 29 |
# File 'lib/relaton/bibcollection.rb', line 27 def <<(item) items << new_bib_item_class(item) end |
#doc_number ⇒ Object
arbitrary number, must sort after all bib items
21 22 23 |
# File 'lib/relaton/bibcollection.rb', line 21 def doc_number 9999999 end |
#items_flattened ⇒ Object
rubocop:enable Metrics/MethodLength
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/relaton/bibcollection.rb', line 98 def items_flattened items.sort_by! &:doc_number items.reduce([]) do |acc, item| acc << if item.is_a? ::Relaton::Bibcollection item.items_flattened else item end end end |
#new_bib_item_class(item) ⇒ Relaton::Bibdata, Relaton::Bibcollection
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/relaton/bibcollection.rb', line 83 def new_bib_item_class(item) if item.is_a?(Hash) if item["items"] ::Relaton::Bibcollection.new(item) else bibitem = ::Relaton::Cli::YAMLConvertor.convert_single_file item ::Relaton::Bibdata.new bibitem end elsif item.is_a?(Relaton::Bibdata) || item.is_a?(Relaton::Bibcollection) item else ::Relaton::Bibdata.new(item) end end |
#to_h ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/relaton/bibcollection.rb', line 114 def to_h items.sort_by! &:doc_number a = ATTRIBS.reduce({}) do |acc, k| acc[k.to_s] = send(k) acc end a["items"] = a["items"].map(&:to_h) { "root" => a } end |
#to_xml(opts = {}) ⇒ String
Returns XML.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/relaton/bibcollection.rb', line 54 def to_xml(opts = {}) items.sort_by! &:doc_number 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>"\ "#{}</name></organization></contributor>" end unless items.empty? items.each do |item| ret += "<relation type='partOf'>" ret += item.to_xml(opts) ret += "</relation>\n" end end ret += "</relaton-collection>\n" end |
#to_yaml ⇒ Object
110 111 112 |
# File 'lib/relaton/bibcollection.rb', line 110 def to_yaml to_h.to_yaml end |