class Shibkit::MetaMeta::MetadataItem

Base class for all MetaMeta metadata classes

Constants

NAMESPACES

Additional namespaces that Nokogiri needs to know about

REQUIRED_QUACKS
ROOT_ELEMENT

Element and attribute used to select XML for new objects

TARGET_ATTR

Attributes

read_at[R]

Public Class Methods

new(xml=nil, target=nil, options={}, &block) click to toggle source

New object takes XML (as libXML object or text)

# File lib/shibkit/meta_meta/metadata_item.rb, line 50
def initialize(xml=nil, target=nil, options={}, &block)
  
  @read_at    = Time.new
  @noko       = nil
  @source_xml = nil
  
  ## Use XML to build object
  from_xml(xml) if xml
  
  ## Use block for further configuration or manual creation
  self.instance_eval(&block) if block

end

Public Instance Methods

filter() click to toggle source

Make sure the object is suitable. Return nil if bad, object if good

# File lib/shibkit/meta_meta/metadata_item.rb, line 65
def filter
  
  ## Make sure this object quacks like the suitable variety of duck
  self.class::REQUIRED_QUACKS.each do |method|
    
    return nil unless self.respond_to? method
    return nil unless self.send(method)
  
  end
  
  return self
  
end
from_xml(xml, target=nil, options={}) click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 140
def from_xml(xml, target=nil, options={})
  
  prepare_xml(xml)
  select_xml(target, options)
  parse_xml
  purge_xml if ::Shibkit::MetaMeta.config.purge_xml?
   
end
hashed_id() click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 79
def hashed_id
  
  return Digest::SHA1.hexdigest uri || url || to_s
  
end
parsed_xml() click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 115
def parsed_xml
  
  prepare_xml(@noko) if @noko.kind_of? String
  
  return @noko
  
end
purge_xml(cascade=true) click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 123
def purge_xml(cascade=true)
  
  @noko       = nil
  @source_xml = nil
  
  cascade_method(:purge_xml, true) if cascade
  
end
source_xml() click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 109
def source_xml
  
  return @source_xml
  
end
textify_xml(cascade=true) click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 132
def textify_xml(cascade=true)
  
  @noko = @noko.to_s
  
  cascade_method(:textify_xml, true) if cascade
  
end
to_hash() click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 85
def to_hash
  
  raise "Not Implemented!"
  
  return {}
  
end
to_rdf() click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 93
def to_rdf
  
  raise "Not Implemented!"
  
  return
  
end
to_xml() click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 101
def to_xml
  
  raise "Not Implemented!"
  
  return
  
end

Private Instance Methods

cascade_method(method_name, *params) click to toggle source
# File lib/shibkit/meta_meta/metadata_item.rb, line 151
def cascade_method(method_name, *params) 
  
  method_name = method_name.to_sym

  self.instance_variables.each do |attr_name|

    obj = instance_variable_get attr_name.to_sym
    
    values = obj.values if obj.respond_to? :values 
    values ||= [obj].flatten
    
    values.each do |value|
    
      if value.respond_to? method_name
      
        value.send method_name, *params
      
      end
    
    end
    
  end

end
log() click to toggle source

Logging

# File lib/shibkit/meta_meta/metadata_item.rb, line 177
def log

  return ::Shibkit::MetaMeta.config.logger
  
end
parse_xml() click to toggle source

Process XML to define object attributes

# File lib/shibkit/meta_meta/metadata_item.rb, line 234
def parse_xml

  raise "parse_xml method has not been implemented in this class"
  
end
prepare_xml(xml) click to toggle source

 Make sure we have consistent Nokogiri document whether string or Nokogiri passed

# File lib/shibkit/meta_meta/metadata_item.rb, line 184
def prepare_xml(xml)
  
  if xml.kind_of? String
    
    ## Parse the entire file as an XML document
    doc = Nokogiri::XML.parse(xml) do |config|
      #config.strict.noent.dtdvalid
      config.default_xml.nonet
    end
    
    @noko = doc.root

    ## Add exotic namespaces to make sure we can deal with all metadata
    NAMESPACES.each_pair { |label, uri| @noko.add_namespace_definition(label,uri) }
    
    @source_xml = xml if ::Shibkit::MetaMeta.config.remember_source_xml?
    
  end
  
  @noko ||= xml
  
  ## Make sure we get an element object...
  @noko = @noko.at('/') if @noko.kind_of? Nokogiri::XML::NodeSet
  @noko = @noko.root    if @noko.kind_of? Nokogiri::XML::Document

  raise "Unsuitable data!" unless @noko and @noko.respond_to? 'name'
  
end
select_xml(target=nil, options={}) click to toggle source

If a target is specified select first matching node, otherwise just grab first node of type

# File lib/shibkit/meta_meta/metadata_item.rb, line 214
def select_xml(target=nil, options={})

  unless @noko.name == self.class::ROOT_ELEMENT ## and check for target too
       
    if target and TARGET_ATTR
      selector = "xmlns:#{self.class::ROOT_ELEMENT}[@#{self.class::TARGET_ATTR}='#{target}'][1]"
      @noko = @noko.xpath(selector)[0]
    else
      selector = "xmlns:#{self.class::ROOT_ELEMENT}[1]"
      @noko = @noko.xpath(selector)[0]
    end  
    
    raise "No suitable XML was selected: using #{selector}" unless @noko and
      @noko.kind_of?(Nokogiri::XML::Element) and @noko.name
    
  end
  
end