Class: Relaton::BibcollectionNew
- Inherits:
-
Object
- Object
- Relaton::BibcollectionNew
show all
- Extended by:
- ElementFinder
- Defined in:
- lib/relaton/bibcollectionnew.rb
Constant Summary
collapse
- ATTRIBS =
%i[
title
items
doctype
author
].freeze
Instance Attribute Summary
#document
Class Method Summary
collapse
Instance Method Summary
collapse
apply_namespace, find, find_text, find_xpath
Constructor Details
Returns a new instance of BibcollectionNew.
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/relaton/bibcollectionnew.rb', line 16
def initialize(options)
self.items = []
ATTRIBS.each do |k|
value = options[k] || options[k.to_s]
send("#{k}=", value)
end
self.items = (items || []).reduce([]) do |acc, item|
acc << if item.is_a?(::Relaton::BibcollectionNew) || item.is_a?(::Relaton::BibdataNew)
item
else new_bib_item_class(item)
end
end
end
|
Class Method Details
.from_xml(source) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/relaton/bibcollectionnew.rb', line 35
def self.from_xml(source)
title = find_text("./relaton-collection/title", source)
author = find_text("./relaton-collection/contributor[role/@type = 'author']/organization/name", source)
items = find_xpath("./relaton-collection/relation", source)&.map do |item|
bibdata = find("./bibdata", item)
klass = bibdata ? BibdataNew : BibcollectionNew
klass.from_xml(bibdata || item)
end
new(title: title, author: author, items: items)
end
|
Instance Method Details
#doc_number ⇒ Object
arbitrary number, must sort after all bib items
31
32
33
|
# File 'lib/relaton/bibcollectionnew.rb', line 31
def doc_number
9999999
end
|
#items_flattened ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/relaton/bibcollectionnew.rb', line 56
def items_flattened
items.sort_by! do |b|
b.docnumber
end
items.inject([]) do |acc,item|
if item.is_a? ::Relaton::BibcollectionNew
acc << item.items_flattened
else
acc << item
end
end
end
|
#new_bib_item_class(options) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/relaton/bibcollectionnew.rb', line 48
def new_bib_item_class(options)
if options.is_a?(Hash) && options["items"]
::Relaton::BibcollectionNew.new(options)
else
::Relaton::BibdataNew.new(options)
end
end
|
#to_h ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/relaton/bibcollectionnew.rb', line 100
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(opts) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/relaton/bibcollectionnew.rb', line 70
def to_xml(opts)
items.sort_by! do |b|
b.docnumber
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 author
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(opts)
ret += "</relation>\n"
end
end
ret += "</relaton-collection>\n"
end
|
#to_yaml ⇒ Object
96
97
98
|
# File 'lib/relaton/bibcollectionnew.rb', line 96
def to_yaml
to_h.to_yaml
end
|