module OM::XML::Document

Attributes

ox_namespaces[RW]

Instance Methods – These methods will be available on instances of classes that include this module

Public Instance Methods

add_child_node(target_node, node_type, *args, &block) click to toggle source

Instantiate a node_type template and add it as a child of target_node, where target_node is one of:

  • a Nokogiri::XML::Node

  • a single-element Nokogiri::XML::NodeSet

  • a term_pointer array resolving to a single-element Nokogiri::XML::NodeSet

Additional arguments will be passed to the template unaltered.

Returns the new Nokogiri::XML::Node.

# File lib/om/xml/document.rb, line 172
def add_child_node(target_node, node_type, *args, &block)
  manipulate_node(:add_child, target_node, node_type, *args, &block)
end
add_next_sibling_node(target_node, node_type, *args, &block) click to toggle source

Instantiate a node_type template and insert it as the following sibling of target_node. Returns the new Nokogiri::XML::Node.

# File lib/om/xml/document.rb, line 178
def add_next_sibling_node(target_node, node_type, *args, &block)
  manipulate_node(:add_next_sibling, target_node, node_type, *args, &block)
end
add_previous_sibling_node(target_node, node_type, *args, &block) click to toggle source

Instantiate a node_type template and insert it as the preceding sibling of target_node. Returns the new Nokogiri::XML::Node.

# File lib/om/xml/document.rb, line 184
def add_previous_sibling_node(target_node, node_type, *args, &block)
  manipulate_node(:add_previous_sibling, target_node, node_type, *args, &block)
end
after_node(target_node, node_type, *args, &block) click to toggle source

Instantiate a node_type template and insert it as the following sibling of target_node. Returns target_node.

# File lib/om/xml/document.rb, line 190
def after_node(target_node, node_type, *args, &block)
  manipulate_node(:after, target_node, node_type, *args, &block)
end
before_node(target_node, node_type, *args, &block) click to toggle source

Instantiate a node_type template and insert it as the preceding sibling of target_node. Returns target_node.

# File lib/om/xml/document.rb, line 196
def before_node(target_node, node_type, *args, &block)
  manipulate_node(:before, target_node, node_type, *args, &block)
end
find_by_terms(*term_pointer) click to toggle source

term_pointer Variable length array of values in format [:accessor_name, :accessor_name …] or [{:accessor_name=>index}, :accessor_name …] @example:

find_by_terms( {:person => 1}, :first_name )

@example

find_by_terms( [:person, 1, :first_name] )

Currently, indexes must be integers. @example Pass in your own xpath query if you don't want to bother with Term pointers but do want OM to handle namespaces for you.

find_by_terms('//oxns:name[@type="personal"][contains(oxns:role, "donor")]')
# File lib/om/xml/document.rb, line 145
def find_by_terms(*term_pointer)
  xpath = self.class.terminology.xpath_with_indexes(*term_pointer)
  find_by_xpath(xpath) unless xpath.nil?
end
find_by_terms_and_value(*term_pointer) click to toggle source

Applies the property's corresponding xpath query, returning the result Nokogiri::XML::NodeSet

# File lib/om/xml/document.rb, line 131
def find_by_terms_and_value(*term_pointer)
  xpath = self.class.terminology.xpath_for(*term_pointer)
  find_by_xpath(xpath) unless xpath.nil?
end
find_by_xpath(xpath) click to toggle source
# File lib/om/xml/document.rb, line 126
def find_by_xpath(xpath)
  ng_xml.xpath(xpath, ox_namespaces)
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/om/xml/document.rb, line 104
def method_missing(method_name, *args)
  if matches = /([^=]+)=$/.match(method_name.to_s)
    name = matches[1].to_sym
  end

  name ||= method_name

  begin
    term = self.class.terminology.retrieve_term(name)

    if /=$/.match(method_name.to_s)
      node = OM::XML::DynamicNode.new(name, nil, self, term)
      return node.val=args
    else
      return OM::XML::DynamicNode.new(name, args.first, self, term)
    end
  rescue OM::XML::Terminology::BadPointerError
    super
  end
end
ng_xml_changed?() click to toggle source
# File lib/om/xml/document.rb, line 100
def ng_xml_changed?
  changed.include?('ng_xml')
end
ng_xml_will_change!() click to toggle source

This ensures that the stored XML is deleted @see ActiveModel::Dirty#attribute_will_change! @see ActiveModel::Dirty#attribute_will_change!

# File lib/om/xml/document.rb, line 90
def ng_xml_will_change!
  # throw away older version.
  # This clears both @attributes_changed_by_setter and @mutations_from_database
  clear_attribute_changes [:ng_xml]
  # This clears the cached changes
  @cached_changed_attributes[:ng_xml] = nil if defined? @cached_changed_attributes

  attribute_will_change!(:ng_xml)
end
node_exists?(*term_pointer) click to toggle source

Test whether the document has a node corresponding to the given term_pointer @param [Array] term_pointer to test

# File lib/om/xml/document.rb, line 152
def node_exists?(*term_pointer)
  !find_by_terms(*term_pointer).empty?
end
replace_node(target_node, node_type, *args, &block) click to toggle source

Instantiate a node_type template and replace target_node with it. Returns the new Nokogiri::XML::Node.

# File lib/om/xml/document.rb, line 202
def replace_node(target_node, node_type, *args, &block)
  manipulate_node(:replace, target_node, node_type, *args, &block)
end
swap_node(target_node, node_type, *args, &block) click to toggle source

Instantiate a node_type template and replace target_node with it. Returns target_node.

# File lib/om/xml/document.rb, line 208
def swap_node(target_node, node_type, *args, &block)
  manipulate_node(:swap, target_node, node_type, *args, &block)
end
template(node_type, *args) click to toggle source
# File lib/om/xml/document.rb, line 161
def template(node_type, *args)
  template_registry.instantiate(node_type, *args)
end
template_registry() click to toggle source

Access the class's template registry

# File lib/om/xml/document.rb, line 157
def template_registry
  self.class.template_registry
end

Private Instance Methods

manipulate_node(method, target, *args, &block) click to toggle source
# File lib/om/xml/document.rb, line 219
def manipulate_node(method, target, *args, &block)
  if target.is_a?(Array)
    target = self.find_by_terms(*target)
  end
  raise "You must call define_template before calling #{method}" if template_registry.nil?
  ng_xml_will_change!
  template_registry.send(method, target, *args, &block)
end