Class: Niceogiri::XML::Node
- Inherits:
-
Nokogiri::XML::Node
- Object
- Nokogiri::XML::Node
- Niceogiri::XML::Node
- Defined in:
- lib/niceogiri/xml/node.rb
Overview
Base XML Node All XML classes subclass Node - it allows the addition of helpers
Class Method Summary collapse
-
.new(name = nil, doc = nil, ns = nil) ⇒ Object
Create a new Node object.
Instance Method Summary collapse
- #==(o) ⇒ Object
-
#content_from(name, ns = nil) ⇒ String?
The content of the named node.
-
#eql?(o, *fields) ⇒ Fixnum<-1,0,1>
Check that a set of fields are equal between nodes.
-
#inherit(node) ⇒ self
Inherit the attributes and children of an XML::Node.
-
#inherit_attrs(attrs) ⇒ self
Inherit a set of attributes.
- #inherit_children(node) ⇒ Object
- #inherit_namespaces(node) ⇒ Object
-
#inspect ⇒ String
The node as XML.
-
#namespace=(namespaces) ⇒ Object
Attach a namespace to the node.
-
#namespace_href ⇒ XML::Namespace?
Helper method to get the node's namespace.
- #nokogiri_namespace= ⇒ Object
-
#read_attr(attr_name, to_call = nil) ⇒ Object
Helper method to read an attribute.
-
#read_content(node, to_call = nil) ⇒ Object
Helper method to read the content of a node.
-
#remove_child(name, ns = nil) ⇒ Object
Remove a child with the name and (optionally) namespace given.
-
#remove_children(name) ⇒ Object
Remove all children with a given name regardless of namespace.
-
#set_content_for(node, content = nil) ⇒ Object
Sets the content for the specified node.
-
#write_attr(attr_name, value, to_call = nil) ⇒ Object
Helper method to write a value to an attribute.
Methods inherited from Nokogiri::XML::Node
#[]=, #attr_set, #find_first, #nokogiri_xpath, #xpath
Class Method Details
.new(name = nil, doc = nil, ns = nil) ⇒ Object
Create a new Node object
not provided one will be created
13 14 15 16 17 18 |
# File 'lib/niceogiri/xml/node.rb', line 13 def self.new(name = nil, doc = nil, ns = nil) super(name.to_s, (doc || Nokogiri::XML::Document.new)).tap do |node| node.document.root = node unless doc node.namespace = ns if ns end end |
Instance Method Details
#==(o) ⇒ Object
188 189 190 |
# File 'lib/niceogiri/xml/node.rb', line 188 def ==(o) eql?(o) end |
#content_from(name, ns = nil) ⇒ String?
The content of the named node
107 108 109 110 |
# File 'lib/niceogiri/xml/node.rb', line 107 def content_from(name, ns = nil) child = xpath(name, ns).first child.content if child end |
#eql?(o, *fields) ⇒ Fixnum<-1,0,1>
Check that a set of fields are equal between nodes
183 184 185 |
# File 'lib/niceogiri/xml/node.rb', line 183 def eql?(o, *fields) o.is_a?(self.class) && fields.all? { |f| self.__send__(f) == o.__send__(f) } end |
#inherit(node) ⇒ self
Inherit the attributes and children of an XML::Node
135 136 137 138 139 140 |
# File 'lib/niceogiri/xml/node.rb', line 135 def inherit(node) inherit_namespaces node inherit_attrs node.attributes inherit_children node self end |
#inherit_attrs(attrs) ⇒ self
Inherit a set of attributes
153 154 155 156 157 158 159 |
# File 'lib/niceogiri/xml/node.rb', line 153 def inherit_attrs(attrs) attrs.each do |name, value| attr_name = value.namespace && value.namespace.prefix ? [value.namespace.prefix, name].join(':') : name self.write_attr attr_name, value end self end |
#inherit_children(node) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/niceogiri/xml/node.rb', line 161 def inherit_children(node) node.children.each do |c| self << (n = c.dup) if c.respond_to?(:namespace) && c.namespace ns = n.add_namespace c.namespace.prefix, c.namespace.href n.namespace = ns end end end |
#inherit_namespaces(node) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/niceogiri/xml/node.rb', line 142 def inherit_namespaces(node) node.namespace_definitions.each do |ns| add_namespace ns.prefix, ns.href end self.namespace = node.namespace.href if node.namespace end |
#inspect ⇒ String
The node as XML
174 175 176 |
# File 'lib/niceogiri/xml/node.rb', line 174 def inspect self.to_xml end |
#namespace=(ns) ⇒ Object #namespace=(ns) ⇒ Object #namespace=(namespaces) ⇒ Object
Attach a namespace to the node
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/niceogiri/xml/node.rb', line 63 def namespace=(namespaces) case namespaces when Nokogiri::XML::Namespace self.nokogiri_namespace = namespaces when String ns = self.add_namespace nil, namespaces self.nokogiri_namespace = ns when Hash self.add_namespace nil, ns if ns = namespaces.delete(nil) namespaces.each do |p, n| ns = self.add_namespace p, n self.nokogiri_namespace = ns end end end |
#namespace_href ⇒ XML::Namespace?
Helper method to get the node's namespace
82 83 84 |
# File 'lib/niceogiri/xml/node.rb', line 82 def namespace_href namespace.href if namespace end |
#nokogiri_namespace= ⇒ Object
51 |
# File 'lib/niceogiri/xml/node.rb', line 51 alias_method :nokogiri_namespace=, :namespace= |
#read_attr(attr_name, to_call = nil) ⇒ Object
Helper method to read an attribute
the returned value
26 27 28 29 |
# File 'lib/niceogiri/xml/node.rb', line 26 def read_attr(attr_name, to_call = nil) val = self[attr_name.to_sym] val && to_call ? val.__send__(to_call) : val end |
#read_content(node, to_call = nil) ⇒ Object
Helper method to read the content of a node
the returned value
45 46 47 48 |
# File 'lib/niceogiri/xml/node.rb', line 45 def read_content(node, to_call = nil) val = content_from node.to_sym val && to_call ? val.__send__(to_call) : val end |
#remove_child(name, ns = nil) ⇒ Object
Remove a child with the name and (optionally) namespace given
90 91 92 93 |
# File 'lib/niceogiri/xml/node.rb', line 90 def remove_child(name, ns = nil) child = xpath(name, ns).first child.remove if child end |
#remove_children(name) ⇒ Object
Remove all children with a given name regardless of namespace
98 99 100 |
# File 'lib/niceogiri/xml/node.rb', line 98 def remove_children(name) xpath("./*[local-name()='#{name}']").remove end |
#set_content_for(node, content = nil) ⇒ Object
Sets the content for the specified node. If the node exists it is updated. If not a new node is created If the node exists and the content is nil, the node will be removed entirely
119 120 121 122 123 124 125 126 127 |
# File 'lib/niceogiri/xml/node.rb', line 119 def set_content_for(node, content = nil) if content child = xpath(node).first self << (child = Node.new(node, self.document)) unless child child.content = content else remove_child node end end |
#write_attr(attr_name, value, to_call = nil) ⇒ Object
Helper method to write a value to an attribute
35 36 37 |
# File 'lib/niceogiri/xml/node.rb', line 35 def write_attr(attr_name, value, to_call = nil) self[attr_name.to_sym] = value && to_call ? value.__send__(to_call) : value end |