Module: OM::XML::Document
- Included in:
- Samples::ModsArticle
- Defined in:
- lib/om/xml/document.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#ox_namespaces ⇒ Object
Returns a hash combining the current documents namespaces (provided by nokogiri) and any namespaces that have been set up by your Terminology.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_child_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a
node_type
template and add it as a child oftarget_node
, wheretarget_node
is one of: * a Nokogiri::XML::Node * a single-element Nokogiri::XML::NodeSet * aterm_pointer
array resolving to a single-element Nokogiri::XML::NodeSet Additional arguments will be passed to the template unaltered. -
#add_next_sibling_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a
node_type
template and insert it as the following sibling oftarget_node
. -
#add_previous_sibling_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a
node_type
template and insert it as the preceding sibling oftarget_node
. -
#after_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a
node_type
template and insert it as the following sibling oftarget_node
. -
#before_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a
node_type
template and insert it as the preceding sibling oftarget_node
. -
#find_by_terms(*term_pointer) ⇒ Object
term_pointer
Variable length array of values in format [:accessor_name, :accessor_name …] or [:accessor_name=>index, :accessor_name …] @example: find_by_terms( => 1, :first_name ) Currently, indexes must be integers. -
#find_by_terms_and_value(*term_pointer) ⇒ Object
Applies the property’s corresponding xpath query, returning the result Nokogiri::XML::NodeSet.
- #find_by_xpath(xpath) ⇒ Object
- #method_missing(name, *args) ⇒ Object
-
#node_exists?(*term_pointer) ⇒ Boolean
Test whether the document has a node corresponding to the given term_pointer.
-
#replace_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a
node_type
template and replacetarget_node
with it. -
#swap_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a
node_type
template and replacetarget_node
with it. - #template(node_type, *args) ⇒ Object
-
#template_registry ⇒ Object
Access the class’s template registry.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/om/xml/document.rb', line 48 def method_missing(name, *args) if matches = /([^=]*)=$/.match(name.to_s) modified_name = matches[1].to_sym term = self.class.terminology.retrieve_term(modified_name) if (term) node = OM::XML::DynamicNode.new(modified_name, nil, self, term) node.val=args else super end else term = self.class.terminology.retrieve_term(name) if (term) OM::XML::DynamicNode.new(name, args.first, self, term) else super end end end |
Instance Attribute Details
#ox_namespaces ⇒ Object
Returns a hash combining the current documents namespaces (provided by nokogiri) and any namespaces that have been set up by your Terminology. Most importantly, this matches the ‘oxns’ namespace to the namespace you provided in your Terminology’s root term config
38 39 40 |
# File 'lib/om/xml/document.rb', line 38 def ox_namespaces @ox_namespaces end |
Class Method Details
.included(klass) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/om/xml/document.rb', line 40 def self.included(klass) klass.extend(ClassMethods) klass.send(:include, OM::XML::Container) klass.send(:include, OM::XML::TermValueOperators) klass.send(:include, OM::XML::Validation) end |
Instance Method Details
#add_child_node(target_node, node_type, *args, &block) ⇒ Object
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.
122 123 124 |
# File 'lib/om/xml/document.rb', line 122 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) ⇒ Object
Instantiate a node_type
template and insert it as the following sibling of target_node
. Returns the new Nokogiri::XML::Node.
128 129 130 |
# File 'lib/om/xml/document.rb', line 128 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) ⇒ Object
Instantiate a node_type
template and insert it as the preceding sibling of target_node
. Returns the new Nokogiri::XML::Node.
134 135 136 |
# File 'lib/om/xml/document.rb', line 134 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) ⇒ Object
Instantiate a node_type
template and insert it as the following sibling of target_node
. Returns target_node
.
140 141 142 |
# File 'lib/om/xml/document.rb', line 140 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) ⇒ Object
Instantiate a node_type
template and insert it as the preceding sibling of target_node
. Returns target_node
.
146 147 148 |
# File 'lib/om/xml/document.rb', line 146 def before_node(target_node, node_type, *args, &block) manipulate_node(:before, target_node, node_type, *args, &block) end |
#find_by_terms(*term_pointer) ⇒ Object
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 )
Currently, indexes must be integers.
95 96 97 98 |
# File 'lib/om/xml/document.rb', line 95 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) ⇒ Object
Applies the property’s corresponding xpath query, returning the result Nokogiri::XML::NodeSet
81 82 83 84 |
# File 'lib/om/xml/document.rb', line 81 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) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/om/xml/document.rb', line 71 def find_by_xpath(xpath) if ox_namespaces.values.compact.empty? ng_xml.xpath(xpath) else ng_xml.xpath(xpath, ox_namespaces) end end |
#node_exists?(*term_pointer) ⇒ Boolean
Test whether the document has a node corresponding to the given term_pointer
102 103 104 |
# File 'lib/om/xml/document.rb', line 102 def node_exists?(*term_pointer) !find_by_terms(*term_pointer).empty? end |
#replace_node(target_node, node_type, *args, &block) ⇒ Object
Instantiate a node_type
template and replace target_node
with it. Returns the new Nokogiri::XML::Node.
152 153 154 |
# File 'lib/om/xml/document.rb', line 152 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) ⇒ Object
Instantiate a node_type
template and replace target_node
with it. Returns target_node
.
158 159 160 |
# File 'lib/om/xml/document.rb', line 158 def swap_node(target_node, node_type, *args, &block) manipulate_node(:swap, target_node, node_type, *args, &block) end |
#template(node_type, *args) ⇒ Object
111 112 113 |
# File 'lib/om/xml/document.rb', line 111 def template(node_type, *args) template_registry.instantiate(node_type, *args) end |
#template_registry ⇒ Object
Access the class’s template registry
107 108 109 |
# File 'lib/om/xml/document.rb', line 107 def template_registry self.class.template_registry end |