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

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

Parameters:

  • attr (String)

    attribute of node to query

Returns:

  • (String, nil)

    string value of attribute or nil if no attribute



49
50
51
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 49

def [](attr)
  xquery("data(@#{attr})").pop
end

#[]=(attr, value) ⇒ Object

TODO:

Find out what this funtion will return

Set the value of an attribute of node

Parameters:

  • attr (String)

    attribute of node to set

  • value (#to_s)

    value to set attribute to

Returns:

  • ????



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

Parameters:

  • selector (String)

    selector used to select nodes from document

Returns:

  • (NodeSet<NSXMLNode>)

    array of nodes matched by 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

#pathString

An XPath formula to reach this node

Returns:

  • (String)

    XPath to this node



100
101
102
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 100

def path
  # Implemented as an alias to :XPath
end

#removeself Also known as: unlink

Remove this node from its parent

Returns:

  • (self)

    this node



92
93
94
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 92

def remove
  # Implemented as an alias to detach
end

#textString Also known as: inner_text

Get the contained text from this node and children nodes

Returns:

  • (String)

    text contents of node



40
41
42
# File 'lib/cocoa-xml/nsxmlnode_extras.rb', line 40

def text
  xquery('data(.)').join
end

#to_sString

Returns the xml of this node including children nodes with proper indentation.

Returns:

  • (String)

    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

Parameters:

  • path (String)

    path used to select nodes from document

Returns:

  • (NodeSet<NSXMLNode>)

    array of nodes matched by selector



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

Parameters:

  • query (String)

    query used process information in document

Returns:

  • (NodeSet<NSXMLNode, String>)

    results depends on query. Notice that unlike #xpath basic types can also be returned.



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