module Exlibris::Primo::Pnx::Elements

Provides access to PNX elements

Public Instance Methods

method_missing(method, *args, &block) click to toggle source

Parses elements from PNX base on the pattern parentname_childname e.g.

record.display_title #=> "Travels with my aunt"

based on the XML

<record>
  ...
  <display>
    ...
    <title>Travels with my aunt</title>
    ...
  </display>
  ...
</record>
Calls superclass method
# File lib/exlibris/primo/pnx/elements.rb, line 23
def method_missing(method, *args, &block)
  if(attr_read(method))
    self.class.send(:define_method, method) {
      if "#{method}".start_with? "all_"
        eval("@#{method} ||= #{attr_read(method).inspect}")
      else
        eval("@#{method} ||= #{attr_read(method).inspect}")
      end
    }
    send method, *args, &block
  else
    super
  end
end
respond_to?(method, include_private=false) click to toggle source

Tell user we respond to PNX elements

Calls superclass method
# File lib/exlibris/primo/pnx/elements.rb, line 41
def respond_to?(method, include_private=false)
  (attr_read(method)) ? true : super
end

Private Instance Methods

attr_read(method) click to toggle source
# File lib/exlibris/primo/pnx/elements.rb, line 45
def attr_read method
  if("#{method}".start_with? "all_")
    (inner_text_all(xpathize(method)) || inner_text_all(controlize(method)))
  else
    (inner_text_at(xpathize(method)) || inner_text_at(controlize(method)))
  end
end
controlize(s) click to toggle source
# File lib/exlibris/primo/pnx/elements.rb, line 67
def controlize s
  "control/#{xpathize s.to_s}"
end
inner_text_all(xpath) click to toggle source
# File lib/exlibris/primo/pnx/elements.rb, line 54
def inner_text_all xpath
  xml.root.xpath(xpath).collect do |element|
    element.inner_text
  end
end
inner_text_at(xpath) click to toggle source
# File lib/exlibris/primo/pnx/elements.rb, line 61
def inner_text_at xpath
  xml_at = xml.root.at_xpath(xpath)
  xml_at.inner_text if xml_at
end
xpathize(s) click to toggle source
# File lib/exlibris/primo/pnx/elements.rb, line 72
def xpathize s
  "#{s.to_s}".gsub(/^all_/, "").gsub(/_/, "/").gsub(/[=\[\]]/, "")
end