class ActiveXML::Object
Public Instance Methods
delete()
click to toggle source
# File lib/active_xml/object.rb, line 5 def delete @path.delete end
delete_node(key)
click to toggle source
# File lib/active_xml/object.rb, line 9 def delete_node(key) find(key).remove end
read_attribute(*keys)
click to toggle source
# File lib/active_xml/object.rb, line 18 def read_attribute(*keys) result = find(keys.join('/')) is_text_node?(result) ? result.content : result end
write_attribute(*keys, value)
click to toggle source
# File lib/active_xml/object.rb, line 23 def write_attribute(*keys, value) append_missing_subtree(keys) append_value(keys, value) end
xml=(xml)
click to toggle source
# File lib/active_xml/object.rb, line 13 def xml=(xml) raise ArgumentError.new("Passed data must be Nokogiri::XML::Document") unless xml.is_a?(Nokogiri::XML::Document) @xml = xml end
Private Instance Methods
append_missing_subtree(keys)
click to toggle source
# File lib/active_xml/object.rb, line 38 def append_missing_subtree(keys) keys.each_with_index do |key, index| next if find(keys[0..index].join('/')) if index == 0 #there is no element at all, searching by css will fail, start from root xml.root.add_child(build_xml_structure(keys)) else find(keys[0..index-1].join('/')).add_child(build_xml_structure(keys[index..-1])) end end end
append_value(keys, value)
click to toggle source
# File lib/active_xml/object.rb, line 50 def append_value(keys, value) css_path = keys.join('/') if value.respond_to?(:to_xml) find(css_path).add_child(Nokogiri::XML(value.to_xml).root) else find(css_path).content = value end end
build_xml_structure(keys)
click to toggle source
# File lib/active_xml/object.rb, line 59 def build_xml_structure(keys) Nokogiri::XML::Builder.new do |xml| keys_to_xml_elements(keys.clone, xml) end.doc.root end
find(key)
click to toggle source
# File lib/active_xml/object.rb, line 71 def find(key) xml.at_css(key.to_s) end
is_text_node?(node)
click to toggle source
# File lib/active_xml/object.rb, line 34 def is_text_node?(node) node && node.children.none?(&:element?) end
keys_to_xml_elements(keys, xml)
click to toggle source
# File lib/active_xml/object.rb, line 65 def keys_to_xml_elements(keys, xml) while next_element = keys.shift xml.send(next_element.to_sym) {|x| keys_to_xml_elements(keys, x)} end end
root()
click to toggle source
# File lib/active_xml/object.rb, line 30 def root "object" end