Class: OpenURL::ContextObjectEntity
- Inherits:
-
Object
- Object
- OpenURL::ContextObjectEntity
- Defined in:
- lib/openurl/context_object_entity.rb
Overview
The ContextObjectEntity is a generic class to define an entity. It should not be initialized directly, only through one of its children:
ReferentEntity, ReferrerEntity, ReferringEntity, ResolverEntity,
ServiceTypeEntity, or CustomEntity
Direct Known Subclasses
DublinCore, Marc, Patent, ScholarlyCommon, ScholarlyServiceType
Instance Attribute Summary collapse
-
#abbr ⇒ Object
readonly
identifiers should always be an array, but it might be an empty one.
-
#format ⇒ Object
readonly
identifiers should always be an array, but it might be an empty one.
-
#identifiers ⇒ Object
readonly
identifiers should always be an array, but it might be an empty one.
-
#label ⇒ Object
readonly
identifiers should always be an array, but it might be an empty one.
-
#metadata ⇒ Object
readonly
identifiers should always be an array, but it might be an empty one.
-
#private_data ⇒ Object
readonly
identifiers should always be an array, but it might be an empty one.
-
#reference ⇒ Object
readonly
identifiers should always be an array, but it might be an empty one.
Class Method Summary collapse
- .new_from_format(format) ⇒ Object
-
.normalize_id(value) ⇒ Object
Switch old 0.1 style ids to new 1.0 style ids.
Instance Method Summary collapse
-
#add_identifier(val) ⇒ Object
(also: #set_identifier)
Should really be called “add identifier”, since we can have more than one.
- #delete_identifier(val) ⇒ Object
-
#empty? ⇒ Boolean
Checks to see if the entity has any metadata set.
- #get_metadata(key) ⇒ Object
-
#identifier ⇒ Object
We can actually have more than one, but certain code calls this method as if there’s only one.
- #import_xml_metadata(node) ⇒ Object
-
#initialize ⇒ ContextObjectEntity
constructor
A new instance of ContextObjectEntity.
-
#kev(abbr) ⇒ Object
Outputs the entity as a KEV array.
- #serialize_metadata(elem, label) ⇒ Object
- #set_format(format) ⇒ Object
- #set_metadata(key, val) ⇒ Object
- #set_private_data(val) ⇒ Object
-
#set_reference(loc, fmt) ⇒ Object
Sets the location and format of a by-reference context object entity.
-
#to_hash(abbr) ⇒ Object
Outputs the entity as a hash Outputting a context object as a hash is imperfect, because context objects can have multiple elements with the same key.
-
#xml(co_elem, label) ⇒ Object
Serializes the entity to XML and attaches it to the supplied REXML element.
-
#xml_for_ref_entity(co_elem) ⇒ Object
Serializes the metadata values for Referent and ReferringEntity entities since their schema is a little different.
Constructor Details
#initialize ⇒ ContextObjectEntity
Returns a new instance of ContextObjectEntity.
14 15 16 17 18 19 20 |
# File 'lib/openurl/context_object_entity.rb', line 14 def initialize @identifiers = [] @reference = {"format"=>nil, "location"=>nil} @format = nil @metadata = {} @private_data = nil end |
Instance Attribute Details
#abbr ⇒ Object (readonly)
identifiers should always be an array, but it might be an empty one.
12 13 14 |
# File 'lib/openurl/context_object_entity.rb', line 12 def abbr @abbr end |
#format ⇒ Object (readonly)
identifiers should always be an array, but it might be an empty one.
12 13 14 |
# File 'lib/openurl/context_object_entity.rb', line 12 def format @format end |
#identifiers ⇒ Object (readonly)
identifiers should always be an array, but it might be an empty one.
12 13 14 |
# File 'lib/openurl/context_object_entity.rb', line 12 def identifiers @identifiers end |
#label ⇒ Object (readonly)
identifiers should always be an array, but it might be an empty one.
12 13 14 |
# File 'lib/openurl/context_object_entity.rb', line 12 def label @label end |
#metadata ⇒ Object (readonly)
identifiers should always be an array, but it might be an empty one.
12 13 14 |
# File 'lib/openurl/context_object_entity.rb', line 12 def @metadata end |
#private_data ⇒ Object (readonly)
identifiers should always be an array, but it might be an empty one.
12 13 14 |
# File 'lib/openurl/context_object_entity.rb', line 12 def private_data @private_data end |
#reference ⇒ Object (readonly)
identifiers should always be an array, but it might be an empty one.
12 13 14 |
# File 'lib/openurl/context_object_entity.rb', line 12 def reference @reference end |
Class Method Details
.new_from_format(format) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/openurl/context_object_entity.rb', line 70 def self.new_from_format(format) if format.match(/^info:ofi\/fmt/) return ContextObjectEntityFactory.format( format ) else return self.new end end |
.normalize_id(value) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/openurl/context_object_entity.rb', line 247 def self.normalize_id(value) value =~ /^(\w+)(\:|\/)(.*)/ prefix = $1 remainder = $3 # info ones if ["doi", "pmid", "oclcnum", "sici", "lccn", "sid"].include?(prefix) value = "info:#{prefix}/#{remainder}" end # urn ones if ["isbn", "issn"].include?(prefix) value = "urn:#{prefix}:#{remainder}" end return value end |
Instance Method Details
#add_identifier(val) ⇒ Object Also known as: set_identifier
Should really be called “add identifier”, since we can have more than one. But for legacy, it’s “set_identifier”.
31 32 33 34 |
# File 'lib/openurl/context_object_entity.rb', line 31 def add_identifier(val) return if val.nil? @identifiers.push( self.class.normalize_id(val) ) unless @identifiers.index(self.class.normalize_id(val)) end |
#delete_identifier(val) ⇒ Object
37 38 39 |
# File 'lib/openurl/context_object_entity.rb', line 37 def delete_identifier(val) return @identifiers.delete(val) end |
#empty? ⇒ Boolean
Checks to see if the entity has any metadata set.
191 192 193 194 |
# File 'lib/openurl/context_object_entity.rb', line 191 def empty? return false if (@identifiers.length > 0 ) or @reference["format"] or @reference["location"] or @metadata.length > 0 or @format or @private_data return true end |
#get_metadata(key) ⇒ Object
57 58 59 |
# File 'lib/openurl/context_object_entity.rb', line 57 def (key) return @metadata[key] end |
#identifier ⇒ Object
We can actually have more than one, but certain code calls this method as if there’s only one. We return the first.
44 45 46 |
# File 'lib/openurl/context_object_entity.rb', line 44 def identifier return @identifiers[0] end |
#import_xml_metadata(node) ⇒ Object
263 264 265 266 267 268 269 270 271 |
# File 'lib/openurl/context_object_entity.rb', line 263 def (node) mbv = REXML::XPath.first(node, "./ctx:metadata-by-val", {"ctx"=>"info:ofi/fmt:xml:xsd:ctx"}) if mbv mbv.to_a.each do |m| self.(m.name(), m.get_text.value) if m && m.get_text end end end |
#kev(abbr) ⇒ Object
Outputs the entity as a KEV array
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/openurl/context_object_entity.rb', line 127 def kev(abbr) kevs = [] @metadata.each do |k,v| kevs << "#{abbr}.#{k}=#{CGI.escape(v.to_str)}" if v end if @kev_ns kevs << "#{abbr}_val_fmt="+CGI.escape(@kev_ns) elsif @format kevs << "#{abbr}_val_fmt="+CGI.escape("info:ofi/fmt:kev:mtx:#{@format}") end if @reference["format"] kevs << "#{abbr}_ref_fmt="+CGI.escape(@reference["format"]) kevs << "#{abbr}_ref="+CGI.escape(@reference["location"]) end @identifiers.each do |id| kevs << "#{abbr}_id="+CGI.escape(id) end kevs << "#{abbr}_dat="+CGI.escape(@private_data) if @private_data return kevs end |
#serialize_metadata(elem, label) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/openurl/context_object_entity.rb', line 115 def (elem, label) = {} = elem.add_element("ctx:metadata") @metadata.each do |k,v| [k] = .add_element("#{label}:"+k) [k].add_namespace(label, (@xml_ns||"info:ofi/fmt:xml:xsd:#{@format}")) [k].text = v end end |
#set_format(format) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/openurl/context_object_entity.rb', line 61 def set_format(format) return unless format if format.match(/^info:ofi\/fmt/) @format = format.split(":").last else @format = format end end |
#set_metadata(key, val) ⇒ Object
53 54 55 |
# File 'lib/openurl/context_object_entity.rb', line 53 def (key, val) @metadata[key] = val end |
#set_private_data(val) ⇒ Object
49 50 51 |
# File 'lib/openurl/context_object_entity.rb', line 49 def set_private_data(val) @private_data = val end |
#set_reference(loc, fmt) ⇒ Object
Sets the location and format of a by-reference context object entity
24 25 26 27 |
# File 'lib/openurl/context_object_entity.rb', line 24 def set_reference(loc, fmt) @reference["location"] = loc @reference["format"] = fmt end |
#to_hash(abbr) ⇒ Object
Outputs the entity as a hash Outputting a context object as a hash is imperfect, because context objects can have multiple elements with the same key. So this function is really deprecated, but here because we have so much code dependent on it.
self does not know it’s own entity abbreviation prefix, so must be passed in.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/openurl/context_object_entity.rb', line 161 def to_hash(abbr) co_hash = {} @metadata.each do |k,v| co_hash["#{abbr}.#{k}"]=v if v end # Not sure what this should be? Can we know what it is set to, or # it's output dependent? If the latter, can't really know # anything, since this is just a hash! co_hash["#{abbr}_val_fmt"]="info:ofi/fmt:kev:mtx:#{@format}" if @format if @reference["format"] co_hash["#{abbr}_ref_fmt"]=@reference["format"] co_hash["#{abbr}_ref"]=@reference["location"] end @identifiers.each do |id| # Put em in a list. co_hash["#{abbr}_id"] ||= Array.new co_hash["#{abbr}_id"].push( id ) end co_hash["#{abbr}_dat"]=@private_data if @private_data return co_hash end |
#xml(co_elem, label) ⇒ Object
Serializes the entity to XML and attaches it to the supplied REXML element.
80 81 82 83 84 85 86 87 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 |
# File 'lib/openurl/context_object_entity.rb', line 80 def xml(co_elem, label) full_label = OpenURL::ContextObject.entities(label) = {"container"=>co_elem.add_element("ctx:#{full_label}")} if @metadata.length > 0 or @format ["metadata-by-val"] = ["container"].add_element("ctx:metadata-by-val") if @format ["format"] = ["metadata-by-val"].add_element("ctx:format") ["format"].text = (@xml_ns||"info:ofi/fmt:xml:xsd:#{@format}") end if @metadata.length > 0 self.(["metadata-by-val"], label) end end if @reference["format"] ["metadata-by-ref"] = ["container"].add_element("ctx:metadata-by-ref") ["ref_format"] = ["metadata-by-ref"].add_element("ctx:format") ["ref_format"].text = @reference["format"] ["ref_loc"] = ["metadata-by-ref"].add_element("ctx:location") ["ref_loc"].text = @reference["location"] end @identifiers.each do |id| # Yes, meta["identifier"] will get over-written if there's more than # one identifier. But I dont' think this meta hash is used for much # I don't think it's a problem. -JR ["identifier"] = ["container"].add_element("ctx:identifier") ["identifier"].text = id end if @private_data ["private-data"] = ["container"].add_element("ctx:private-data") ["private-data"].text = @private_data end return co_elem end |
#xml_for_ref_entity(co_elem) ⇒ Object
Serializes the metadata values for Referent and ReferringEntity entities since their schema is a little different.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/openurl/context_object_entity.rb', line 199 def xml_for_ref_entity(co_elem) = {"container"=>co_elem.add_element("ctx:#{@label}")} if @metadata.length > 0 or @format ["metadata-by-val"] = ["container"].add_element("ctx:metadata-by-val") if @format ["format"] = ["metadata-by-val"].add_element("ctx:format") ["format"].text = "info:ofi/fmt:xml:xsd:#{@format}" if @metadata.length > 0 ["metadata"] = ["metadata-by-val"].add_element("ctx:metadata") ["format_container"] = ["metadata"].add_element("rft:#{@format}") ["format_container"].add_namespace(@abbr, ["format"].text) ["format_container"].add_attribute("xsi:schemaLocation", ["format"].text+" http://www.openurl.info/registry/docs/info:ofi/fmt:xml:xsd:"+@format) @metadata.each do |k,v| [k] = ["format_container"].add_element("#{@abbr}:#{k}") [k].text = v end end end end if @reference["format"] ["metadata-by-ref"] = ["container"].add_element("ctx:metadata-by-ref") ["ref_format"] = ["metadata-by-ref"].add_element("ctx:format") ["ref_format"].text = @reference["format"] ["ref_loc"] = ["metadata-by-ref"].add_element("ctx:location") ["ref_loc"].text = @reference["location"] end @identifiers.each do |id| # Yes, if there's more than one, meta["identifier"] will get # overwritten with last. I don't think this is a problem, cause # meta["identifier"] isn't used anywhere. ["identifier"] = ["container"].add_element("ctx:identifier") ["identifier"].text = id end if @private_data ["private-data"] = ["container"].add_element("ctx:private-data") ["private-data"].text = @private_data end return co_elem end |