Module: CocoaXML::NSXMLNodeExtras
- Defined in:
- lib/cocoa-xml/nsxmlnode_extras.rb
Overview
These are a set of methods added onto the Cocoa NSXMLNode class and children.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](attr) ⇒ String?
Get the value of an attribute of node.
-
#[]=(attr, value) ⇒ Object
Set the value of an attribute of node.
- #attribute(attr) ⇒ Object
-
#css(selector) ⇒ NodeSet<NSXMLNode>
Search from this node down using a css selector.
-
#path ⇒ String
An XPath formula to reach this node.
-
#remove ⇒ self
(also: #unlink)
Remove this node from its parent.
-
#text ⇒ String
(also: #inner_text)
Get the contained text from this node and children nodes.
-
#to_s ⇒ String
The xml of this node including children nodes with proper indentation.
-
#xpath(path) ⇒ NodeSet<NSXMLNode>
Search document using provided path.
-
#xquery(query) ⇒ NodeSet<NSXMLNode, String>
Process document using provided query.
Class Method Details
.included(klass) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 75 def self.included(klass) klass.class_eval do alias :remove :detach alias :unlink :detach alias :path :XPath # TODO: Why is this not working alias :old_children :children def children NodeSet.new(old_children) end end end |
Instance Method Details
#[](attr) ⇒ String?
Get the value of an attribute of node
49 50 51 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 49 def [](attr) xquery("data(@#{attr})").pop end |
#[]=(attr, value) ⇒ Object
Find out what this funtion will return
Set the value of an attribute of node
59 60 61 62 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 59 def []=(attr, value) node = attributeForName(attr.to_s) node && node.setStringValue(value) || addAttribute(::NSXMLNode.attributeWithName(attr.to_s, stringValue: value)) end |
#attribute(attr) ⇒ Object
65 66 67 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 65 def attribute(attr) attributeForName(attr.to_s) end |
#css(selector) ⇒ NodeSet<NSXMLNode>
Search from this node down using a css selector
8 9 10 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 8 def css(selector) xpath ::Nokogiri::CSS::xpath_for(selector, :prefix => ".//").join end |
#path ⇒ String
An XPath formula to reach this node
100 101 102 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 100 def path # Implemented as an alias to :XPath end |
#remove ⇒ self Also known as: unlink
Remove this node from its parent
92 93 94 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 92 def remove # Implemented as an alias to detach end |
#text ⇒ String Also known as: inner_text
Get the contained text from this node and children nodes
40 41 42 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 40 def text xquery('data(.)').join end |
#to_s ⇒ String
Returns the xml of this node including children nodes with proper indentation.
70 71 72 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 70 def to_s XMLStringWithOptions NSXMLNodePrettyPrint end |
#xpath(path) ⇒ NodeSet<NSXMLNode>
Search document using provided path
16 17 18 19 20 21 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 16 def xpath(path) error = Pointer.new(:object) results = nodesForXPath path, error: error return NodeSet.new(results) if error[0].nil? end |
#xquery(query) ⇒ NodeSet<NSXMLNode, String>
Process document using provided query
28 29 30 31 32 33 34 35 |
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 28 def xquery(query) error = Pointer.new(:object) results = objectsForXQuery query, error: error return NodeSet.new(results) if error[0].nil? #TODO Do something with the error end |