Class: IdentityMetadata
- Inherits:
-
Object
- Object
- IdentityMetadata
- Defined in:
- lib/xml_models/identity_metadata/identity_metadata.rb
Instance Attribute Summary collapse
-
#adminPolicy ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#agreementId ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#citationCreators ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#citationTitle ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#ng_xml ⇒ Object
readonly
this stores the Nokogiri XML.
-
#objectCreators ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#objectId ⇒ Object
these are single values.
-
#objectLabels ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#objectTypes ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#otherIds ⇒ Object
these instance vars map to nodes in the identityMetadata XML.
-
#sourceId ⇒ Object
Returns the value of attribute sourceId.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
-
.from_xml(xml = "") ⇒ Object
takes a string of XML and constructs the object with all the instance variables added to the correct location.
Instance Method Summary collapse
-
#add_identifier(*args) ⇒ Object
Add a new name,value pair to the set of identifiers.
-
#add_tag(new_tag_value) ⇒ Object
(also: #tag)
Add a new tag to the IdentityMetadata instance.
-
#build_xml ⇒ Object
this builds the xml based on the instance variables in the object.
-
#citationCreator=(creator) ⇒ Object
another convience method to allow citationCreator=.
-
#get_id_pairs ⇒ Object
Return an array of strings where each entry consists of name:value.
-
#get_identifier_value(key) ⇒ Object
Return the identifier value for the specified identier name.
-
#get_other_id(name) ⇒ Object
Return the OtherId hash for the specified identier name.
-
#get_tags ⇒ Object
Returns an array of tag values.
-
#initialize(xml = nil) ⇒ IdentityMetadata
constructor
A new instance of IdentityMetadata.
-
#to_xml ⇒ Object
This method rebuilds the xml attr_accesor and returns it as a string.
- #xml ⇒ Object
- #xml=(value) ⇒ Object
Constructor Details
#initialize(xml = nil) ⇒ IdentityMetadata
Returns a new instance of IdentityMetadata.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 96 def initialize(xml = nil) @objectId, @citationTitle, @adminPolicy, @agreementId = "", "", "", "" #there can only be one of these values @sourceId = SourceId.new #there can be only one. @otherIds, @tags = [], [] # this is an array that will be filled with OtherId and Tag objects @objectTypes, @objectLabels, @objectCreators, @citationCreators = [], [], [], [] # if the new is given an xml string, store that in the xml attr_accessor and don't rebuild. # this will allow users to access the raw unprocessed XML string via @xml. if xml.nil? build_xml() else @xml = xml end end |
Instance Attribute Details
#adminPolicy ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def adminPolicy @adminPolicy end |
#agreementId ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def agreementId @agreementId end |
#citationCreators ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def citationCreators @citationCreators end |
#citationTitle ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def citationTitle @citationTitle end |
#ng_xml ⇒ Object (readonly)
this stores the Nokogiri XML
93 94 95 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 93 def ng_xml @ng_xml end |
#objectCreators ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def objectCreators @objectCreators end |
#objectId ⇒ Object
these are single values
87 88 89 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 87 def objectId @objectId end |
#objectLabels ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def objectLabels @objectLabels end |
#objectTypes ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def objectTypes @objectTypes end |
#otherIds ⇒ Object
these instance vars map to nodes in the identityMetadata XML
90 91 92 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 90 def otherIds @otherIds end |
#sourceId ⇒ Object
Returns the value of attribute sourceId.
88 89 90 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 88 def sourceId @sourceId end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
88 89 90 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 88 def @tags end |
Class Method Details
.from_xml(xml = "") ⇒ Object
takes a string of XML and constructs the object with all the instance variables added to the correct location.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 252 def self.from_xml(xml="") if xml.is_a?(File) xml = xml.read end im = self.new(xml) doc = Nokogiri::XML(xml) children = doc.root.element_children #iterate through the nodes and map them to instance vars in the object. children.each do |c| if im.instance_variables.include?("@#{c.name}") or im.instance_variables.include?("@#{c.name}s") if c.name == "sourceId" #SourceID already has a SourceID object made im.sourceId.source = c["source"] im.sourceId.value = c.text.strip elsif c.name == "otherId" #otherID needs to be cast as an object and stored in an array im.add_identifier(c['name'],c.text.strip) elsif c.name == "tag" #tags also need to have objects created and stored in an array im.add_tag(c.text.strip) elsif c.name == "objectId" # objectId needs to be mapped to objectId attr_access im.objectId = c.text.strip elsif c.name == "citationTitle" #citationTitle also needs to be mapped to citationTitle attr_accessor im.citationTitle = c.text.strip elsif c.name == "adminPolicy" im.adminPolicy = c.text.strip elsif c.name == "agreementId" im.agreementId = c.text.strip else # everything else gets put into an attr_accessor array (note the added 's' on the attr_accessor.) im.send("#{c.name}s").send("<<", c.text.strip) end #if end #if end #each return im end |
Instance Method Details
#add_identifier(*args) ⇒ Object
Add a new name,value pair to the set of identifiers
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 203 def add_identifier(*args) (key,value) = args.collect { |arg| arg.to_s } if value.nil? and key =~ /:/ (key,value) = key.split(/:/,2) end other_id = self.get_other_id(key) if (other_id != nil) other_id.value = value else other_id = OtherId.new other_id.name = key other_id.value = value @otherIds << other_id end end |
#add_tag(new_tag_value) ⇒ Object Also known as: tag
Add a new tag to the IdentityMetadata instance
165 166 167 168 169 170 171 172 173 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 165 def add_tag(new_tag_value) # Make sure tag is not already present unless self..include?(new_tag_value) tag = Tag.new tag.value = new_tag_value self. << tag end return self. end |
#build_xml ⇒ Object
this builds the xml based on the instance variables in the object. If it’s a hash, this assumes that we want to use attributes ==> {“text”=> “7f3da130-7b02-11de-8a39-0800200c9a66”, “name” => “uuid” }. If the instance var is an array, we assume we don’t need attrs, so all values get put into the text node.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 116 def build_xml() builder = Nokogiri::XML::Builder.new do |xml| xml.identityMetadata { field_names = self.instance_variables.sort { |a,b| "#{a}:#{self.instance_variable_get(a).to_s}" <=> "#{b}:#{self.instance_variable_get(b).to_s}"} field_names.each do |var_name| unless var_name == "@xml" var = self.instance_variable_get(var_name) tag_name = var_name[1..-1] if var.is_a?(Array) tag_name.chomp!('s') end # wrap the singleton properties in a one-element array var = Array(var) var.each do |v| if v.respond_to?(:xml_values) unless (v.respond_to?(:empty?) && v.empty?) xml.send(tag_name, *(v.xml_values)) end else xml.send(tag_name, v.to_s) end end #var.each end #unless end #instance_variables.each } end @ng_xml = builder.doc end |
#citationCreator=(creator) ⇒ Object
another convience method to allow citationCreator=
241 242 243 244 245 246 247 248 249 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 241 def citationCreator=(creator) if creator.is_a?(Array) self.citationCreators = creator elsif creator.is_a?(String) self.citationCreators = [creator] else raise "Identity_metadata.citationCreator requires either a string or array. " end end |
#get_id_pairs ⇒ Object
Return an array of strings where each entry consists of name:value
233 234 235 236 237 238 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 233 def get_id_pairs pairs=Array.new self.otherIds.collect do |other_id| other_id.to_s end end |
#get_identifier_value(key) ⇒ Object
Return the identifier value for the specified identier name
194 195 196 197 198 199 200 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 194 def get_identifier_value(key) other_id = self.get_other_id(key) if other_id != nil && other_id.value != nil return other_id.value end raise "No #{key} indentifier found for druid #{@objectId}" end |
#get_other_id(name) ⇒ Object
Return the OtherId hash for the specified identier name
184 185 186 187 188 189 190 191 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 184 def get_other_id(name) self.otherIds.each do |oi| if oi.name == name return oi end end return nil end |
#get_tags ⇒ Object
Returns an array of tag values
178 179 180 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 178 def () self..collect { |t| t.value } end |
#to_xml ⇒ Object
This method rebuilds the xml attr_accesor and returns it as a string.
154 155 156 157 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 154 def to_xml build_xml return self.xml end |
#xml ⇒ Object
145 146 147 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 145 def xml @ng_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION, :indent => 2) end |
#xml=(value) ⇒ Object
149 150 151 |
# File 'lib/xml_models/identity_metadata/identity_metadata.rb', line 149 def xml=(value) @ng_xml = Nokogiri::XML(value) end |