Module: HydraPbcore::Methods
- Included in:
- Datastream::Document, Datastream::Instantiation
- Defined in:
- lib/hydra_pbcore/methods.rb
Instance Method Summary collapse
-
#get_year(s) ⇒ Object
Returns the 4-digit year from a string.
-
#remove_node(type, index = 0, opts = {}) ⇒ Object
Used to delete nodes based on their OM term defintion.
-
#to_pbcore_xml(included_instantiations = Array.new, instantiations = Array.new) ⇒ Object
Returns a new Nokogiri::XML object with the contents of self, plus any additional instanstations, reordered and repackaged as a valid pbcore xml document.
-
#valid? ⇒ Boolean
Validates a PBCore document against an xsd Returns an array of errors – an empty array means it’s valid.
Instance Method Details
#get_year(s) ⇒ Object
Returns the 4-digit year from a string
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hydra_pbcore/methods.rb', line 62 def get_year(s) begin return DateTime.parse(s).year.to_s rescue if s.match(/^\d\d\d\d$/) return s.to_s elsif s.match(/^(\d\d\d\d)-\d\d$/) return $1.to_s else return nil end end end |
#remove_node(type, index = 0, opts = {}) ⇒ Object
Used to delete nodes based on their OM term defintion.
Default behavior is to delete on the xml defining the term. In some cases, terms can have dependent xml that is part of the term’s parent. For example, pbcoreReation templates use the pbcoreRelation xml node to define a term, however the term’s definition is dependent on the xml inside pbcoreRelation. When we delete the term, the pbcoreRelation has to go along with it.
In that case, we can pass the option:
:include_parent? => true
Which will delete the term as well as its closest parent
15 16 17 |
# File 'lib/hydra_pbcore/methods.rb', line 15 def remove_node(type, index = 0, opts = {}) opts[:include_parent?] ? self.find_by_terms(type.to_sym).slice(index.to_i).parent.remove : self.find_by_terms(type.to_sym).slice(index.to_i).remove end |
#to_pbcore_xml(included_instantiations = Array.new, instantiations = Array.new) ⇒ Object
Returns a new Nokogiri::XML object with the contents of self, plus any additional instanstations, reordered and repackaged as a valid pbcore xml document.
The original xml from the datastream is copied to a new Nokogiri object, then each node is added–in correct order–to a new blank, valid pbcore xml document. If additional instantiations are passed in as an array, those are correctly reordered as well, and included in the document
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/hydra_pbcore/methods.rb', line 26 def to_pbcore_xml included_instantiations = Array.new, instantiations = Array.new # Reorder any included instantations included_instantiations.each do |i| original_inst = Nokogiri::XML(i.to_xml) new_inst = Nokogiri::XML("<pbcoreInstantiation/>") HydraPbcore::InstantiationNodes.each do |node| original_inst.search(node).each do |n| new_inst.root.add_child(n) end end instantiations << new_inst end # Reorder the document original = Nokogiri::XML(self.to_xml) new_doc = HydraPbcore.blank HydraPbcore::DocumentNodes.each do |node| original.search(node).each do |n| new_doc.root.add_child(n) end if node.match("pbcoreInstantiation") && !instantiations.empty? instantiations.collect { |i| new_doc.root.add_child(i.root.to_xml) } end end return new_doc end |
#valid? ⇒ Boolean
Validates a PBCore document against an xsd Returns an array of errors – an empty array means it’s valid
56 57 58 59 |
# File 'lib/hydra_pbcore/methods.rb', line 56 def valid? xsd = Nokogiri::XML::Schema(open("http://pbcore.org/xsd/pbcore-2.0.xsd")) xsd.validate(self.to_pbcore_xml) end |