Module: OM::XML::Document

Extended by:
ActiveSupport::Concern
Defined in:
lib/om/xml/document.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_name, *args)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/om/xml/document.rb', line 96

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

Instance Attribute Details

- (Object) ox_namespaces

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



85
86
87
# File 'lib/om/xml/document.rb', line 85

def ox_namespaces
  @ox_namespaces
end

Instance Method Details

- (Object) add_child_node(target_node, node_type, *args, &block)

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.



164
165
166
# File 'lib/om/xml/document.rb', line 164

def add_child_node(target_node, node_type, *args, &block)
  manipulate_node(:add_child, target_node, node_type, *args, &block)
end

- (Object) add_next_sibling_node(target_node, node_type, *args, &block)

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



170
171
172
# File 'lib/om/xml/document.rb', line 170

def add_next_sibling_node(target_node, node_type, *args, &block)
  manipulate_node(:add_next_sibling, target_node, node_type, *args, &block)
end

- (Object) add_previous_sibling_node(target_node, node_type, *args, &block)

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



176
177
178
# File 'lib/om/xml/document.rb', line 176

def add_previous_sibling_node(target_node, node_type, *args, &block)
  manipulate_node(:add_previous_sibling, target_node, node_type, *args, &block)
end

- (Object) after_node(target_node, node_type, *args, &block)

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



182
183
184
# File 'lib/om/xml/document.rb', line 182

def after_node(target_node, node_type, *args, &block)
  manipulate_node(:after, target_node, node_type, *args, &block)
end

- (Object) before_node(target_node, node_type, *args, &block)

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



188
189
190
# File 'lib/om/xml/document.rb', line 188

def before_node(target_node, node_type, *args, &block)
  manipulate_node(:before, target_node, node_type, *args, &block)
end

- (Object) find_by_terms(*term_pointer)

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.

Examples:

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

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")]')


137
138
139
140
# File 'lib/om/xml/document.rb', line 137

def find_by_terms(*term_pointer)
  xpath = self.class.terminology.xpath_with_indexes(*term_pointer)
  find_by_xpath(xpath) unless xpath.nil?
end

- (Object) find_by_terms_and_value(*term_pointer)

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



123
124
125
126
# File 'lib/om/xml/document.rb', line 123

def find_by_terms_and_value(*term_pointer)
  xpath = self.class.terminology.xpath_for(*term_pointer)
  find_by_xpath(xpath) unless xpath.nil?
end

- (Object) find_by_xpath(xpath)



118
119
120
# File 'lib/om/xml/document.rb', line 118

def find_by_xpath(xpath)
  ng_xml.xpath(xpath, ox_namespaces)
end

- (Boolean) ng_xml_changed?



92
93
94
# File 'lib/om/xml/document.rb', line 92

def ng_xml_changed?
  changed.include?('ng_xml')
end

- (Object) ng_xml_will_change!



87
88
89
90
# File 'lib/om/xml/document.rb', line 87

def ng_xml_will_change!
  # throw away older version.
  changed_attributes['ng_xml'] = nil
end

- (Boolean) node_exists?(*term_pointer)

Test whether the document has a node corresponding to the given term_pointer



144
145
146
# File 'lib/om/xml/document.rb', line 144

def node_exists?(*term_pointer)
  !find_by_terms(*term_pointer).empty?
end

- (Object) replace_node(target_node, node_type, *args, &block)

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



194
195
196
# File 'lib/om/xml/document.rb', line 194

def replace_node(target_node, node_type, *args, &block)
  manipulate_node(:replace, target_node, node_type, *args, &block)
end

- (Object) swap_node(target_node, node_type, *args, &block)

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



200
201
202
# File 'lib/om/xml/document.rb', line 200

def swap_node(target_node, node_type, *args, &block)
  manipulate_node(:swap, target_node, node_type, *args, &block)
end

- (Object) template(node_type, *args)



153
154
155
# File 'lib/om/xml/document.rb', line 153

def template(node_type, *args)
  template_registry.instantiate(node_type, *args)
end

- (Object) template_registry

Access the class's template registry



149
150
151
# File 'lib/om/xml/document.rb', line 149

def template_registry
  self.class.template_registry
end