Class: JSON::LD::API::REXML::NodeProxy
- Inherits:
-
Object
- Object
- JSON::LD::API::REXML::NodeProxy
- Defined in:
- lib/json/ld/html/rexml.rb
Overview
Proxy class to implement uniform element accessors
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#ancestors ⇒ Object
Ancestors of this element, in order.
- #at_xpath(*args) ⇒ Object
- #attribute_nodes ⇒ Object
-
#base ⇒ String
Return xml:base on element, if defined.
- #blank? ⇒ Boolean
-
#children ⇒ NodeSetProxy
Children of this node.
- #display_path ⇒ Object
- #element? ⇒ Boolean
-
#initialize(node, parent = nil) ⇒ NodeProxy
constructor
A new instance of NodeProxy.
-
#inner_html ⇒ String
Inner text of an element.
-
#inner_text ⇒ String
Inner text of an element.
-
#method_missing(method, *args) ⇒ Object
Proxy for everything else to @node.
-
#text? ⇒ Boolean
Node type accessors.
-
#text_content? ⇒ Array<:text, :element, :attribute>
Return true of all child elements are text.
- #to_s ⇒ Object
- #xpath(*args) ⇒ Object
Constructor Details
#initialize(node, parent = nil) ⇒ NodeProxy
Returns a new instance of NodeProxy.
25 26 27 28 |
# File 'lib/json/ld/html/rexml.rb', line 25 def initialize(node, parent = nil) @node = node @parent = parent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Proxy for everything else to @node
132 133 134 |
# File 'lib/json/ld/html/rexml.rb', line 132 def method_missing(method, *args) @node.send(method, *args) end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
23 24 25 |
# File 'lib/json/ld/html/rexml.rb', line 23 def node @node end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
23 24 25 |
# File 'lib/json/ld/html/rexml.rb', line 23 def parent @parent end |
Instance Method Details
#ancestors ⇒ Object
Ancestors of this element, in order
68 69 70 |
# File 'lib/json/ld/html/rexml.rb', line 68 def ancestors @ancestors ||= parent ? parent.ancestors + [parent] : [] end |
#at_xpath(*args) ⇒ Object
126 127 128 |
# File 'lib/json/ld/html/rexml.rb', line 126 def at_xpath(*args) xpath(*args).first end |
#attribute_nodes ⇒ Object
93 94 95 96 97 98 |
# File 'lib/json/ld/html/rexml.rb', line 93 def attribute_nodes attrs = @node.attributes.dup.keep_if do |name, _attr| !name.start_with?('xmlns') end @attribute_nodes ||= (attrs.empty? ? attrs : NodeSetProxy.new(attrs, self)) end |
#base ⇒ String
Return xml:base on element, if defined
34 35 36 |
# File 'lib/json/ld/html/rexml.rb', line 34 def base @node.attribute("base", RDF::XML.to_s) || @node.attribute('xml:base') end |
#blank? ⇒ Boolean
112 113 114 |
# File 'lib/json/ld/html/rexml.rb', line 112 def blank? @node.is_a?(::REXML::Text) && @node.empty? end |
#children ⇒ NodeSetProxy
Children of this node
63 64 65 |
# File 'lib/json/ld/html/rexml.rb', line 63 def children NodeSetProxy.new(@node.children, self) end |
#display_path ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/json/ld/html/rexml.rb', line 38 def display_path @display_path ||= begin path = [] path << parent.display_path if parent path << @node.name case @node when ::REXML::Element then path.join("/") when ::REXML::Attribute then path.join("@") else path.join("?") end end end |
#element? ⇒ Boolean
108 109 110 |
# File 'lib/json/ld/html/rexml.rb', line 108 def element? @node.is_a?(::REXML::Element) end |
#inner_html ⇒ String
Inner text of an element
89 90 91 |
# File 'lib/json/ld/html/rexml.rb', line 89 def inner_html @node.children.map(&:to_s).join end |
#inner_text ⇒ String
Inner text of an element
77 78 79 80 81 82 |
# File 'lib/json/ld/html/rexml.rb', line 77 def inner_text coder = HTMLEntities.new ::REXML::XPath.match(@node, './/text()').map do |e| coder.decode(e) end.join end |
#text? ⇒ Boolean
Node type accessors
104 105 106 |
# File 'lib/json/ld/html/rexml.rb', line 104 def text? @node.is_a?(::REXML::Text) end |
#text_content? ⇒ Array<:text, :element, :attribute>
Return true of all child elements are text
55 56 57 |
# File 'lib/json/ld/html/rexml.rb', line 55 def text_content? @node.children.all?(::REXML::Text) end |
#to_s ⇒ Object
116 117 118 |
# File 'lib/json/ld/html/rexml.rb', line 116 def to_s @node.to_s end |