Class: OpenURL::ScholarlyCommon
Instance Attribute Summary collapse
#abbr, #format, #identifiers, #label, #metadata, #private_data, #reference
Instance Method Summary
collapse
#add_identifier, #delete_identifier, #empty?, #get_metadata, #identifier, #kev, new_from_format, normalize_id, #set_format, #set_private_data, #set_reference, #to_hash, #xml, #xml_for_ref_entity
Constructor Details
Returns a new instance of ScholarlyCommon.
15
16
17
18
19
20
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 15
def initialize
super()
@authors = [OpenURL::Author.new]
@author_keys = ['aulast','aufirst','auinit','auinit1','auinitm','ausuffix',
'au', 'aucorp']
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(metadata, value = nil) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 22
def method_missing(metadata, value=nil)
meta = metadata.to_s.sub(/=$/,'')
raise ArgumentError, "#{meta.to_s} is not a valid #{self.class} metadata field." unless (@author_keys+@metadata_keys).index(meta)
if metadata.to_s.match(/=$/)
self.set_metadata(meta, value)
if @author_keys.index(meta)
@authors[0].instance_variable_set("@#{meta}", value)
end
else
return self.metadata[meta]
end
end
|
Instance Attribute Details
#authors ⇒ Object
Returns the value of attribute authors.
14
15
16
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 14
def authors
@authors
end
|
Instance Method Details
#add_author(author) ⇒ Object
58
59
60
61
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 58
def add_author(author)
raise ArgumentError, "Argument must be an OpenURL::Author!" unless author.is_a?(OpenURL::Author)
@authors << author
end
|
#genre ⇒ Object
54
55
56
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 54
def genre
return self.metadata["genre"]
end
|
#genre=(genre) ⇒ Object
49
50
51
52
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 49
def genre=(genre)
raise ArgumentError, "#{genre} is not a valid #{self.class} genre." unless @valid_genres.index(genre)
self.set_metadata('genre', genre)
end
|
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 88
def import_xml_metadata(node)
mbv = REXML::XPath.first(node, "./ctx:metadata-by-val/ctx:metadata/fmt:#{@format}", {"ctx"=>"info:ofi/fmt:xml:xsd:ctx", "fmt"=>@xml_ns})
if mbv
mbv.to_a.each do |m|
self.set_metadata(m.name(), m.get_text.value) if m.has_text?
if m.has_elements?
m.to_a.each do | md |
self.set_metadata(md.name(), md.get_text.value) if md.has_text?
end
end
end
auth_num = 0
REXML::XPath.each(mbv, "fmt:authors/fmt:author | fmt:authors/fmt:au | fmt:authors/fmt:aucorp", {"fmt"=>@xml_ns}) do | author |
empty_node = true
if author.name == "author"
author.elements.each do | auth_elem |
next unless @author_keys.index(auth_elem.name) and auth_elem.has_text?
empty_node = false
@authors << OpenURL::Author.new unless @authors[auth_num]
@authors[auth_num].instance_variable_set("@#{auth_elem.name}".to_sym, auth_elem.get_text.value)
self.set_metadata(auth_elem.name, auth_elem.get_text.value) if auth_num == 0
end
elsif author.name.match(/^au$|^aucorp$/)
next unless author.has_text?
empty_node = false
@authors << OpenURL::Author.new unless @authors[auth_num]
@authors[auth_num].instance_variable_set("@#{author.name}".to_sym, author.get_text.value)
self.set_metadata(author.name, author.get_text.value) if auth_num == 0
end
auth_num += 1 unless empty_node
end
end
end
|
#remove_author(author) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 63
def remove_author(author)
idx = author
idx = @authors.index(author)
raise ArgumentError unless idx
@authors.delete_at(idx)
end
|
#respond_to?(message, include_all = false) ⇒ Boolean
35
36
37
38
39
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 35
def respond_to?(message, include_all=false)
key = message.to_s.sub(/=$/,'')
super || (@author_keys+@metadata_keys).index(key)
end
|
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 70
def serialize_metadata(elem, label)
meta = {}
metadata = elem.add_element("ctx:metadata")
meta["format_container"] = metadata.add_element("#{label}:#{@format}")
meta["format_container"].add_namespace(label, @xml_ns)
meta["format_container"].add_attribute("xsi:schemaLocation", "#{@xml_ns} http://www.openurl.info/registry/docs/info:ofi/fmt:xml:xsd:#{@format}")
@metadata.each do |k,v|
next if ['au', 'aucorp', 'auinit', 'auinitm', 'aulast',
'aufirst', 'auinit1', 'ausuffix'].index(k)
meta[k] = meta["format_container"].add_element("#{label}:#{k}")
meta[k].text = v
end
meta["author_container"] = meta["format_container"].add_element("#{label}:authors")
@authors.each do | author |
author.xml(meta["author_container"])
end
end
|
42
43
44
45
46
47
|
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 42
def set_metadata(key, val)
@metadata[key] = val.to_s
if @author_keys.index(key)
@authors[0].instance_variable_set("@#{key}", val)
end
end
|