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.
23 24 25 26 |
# File 'lib/json/ld/html/rexml.rb', line 23 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
128 129 130 |
# File 'lib/json/ld/html/rexml.rb', line 128 def method_missing(method, *args) @node.send(method, *args) end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
20 21 22 |
# File 'lib/json/ld/html/rexml.rb', line 20 def node @node end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
21 22 23 |
# File 'lib/json/ld/html/rexml.rb', line 21 def parent @parent end |
Instance Method Details
#ancestors ⇒ Object
Ancestors of this element, in order
66 67 68 |
# File 'lib/json/ld/html/rexml.rb', line 66 def ancestors @ancestors ||= parent ? parent.ancestors + [parent] : [] end |
#at_xpath(*args) ⇒ Object
122 123 124 |
# File 'lib/json/ld/html/rexml.rb', line 122 def at_xpath(*args) xpath(*args).first end |
#attribute_nodes ⇒ Object
91 92 93 94 95 96 |
# File 'lib/json/ld/html/rexml.rb', line 91 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
32 33 34 |
# File 'lib/json/ld/html/rexml.rb', line 32 def base @node.attribute("base", RDF::XML.to_s) || @node.attribute('xml:base') end |
#blank? ⇒ Boolean
110 111 112 |
# File 'lib/json/ld/html/rexml.rb', line 110 def blank? @node.is_a?(::REXML::Text) && @node.empty? end |
#children ⇒ NodeSetProxy
Children of this node
61 62 63 |
# File 'lib/json/ld/html/rexml.rb', line 61 def children NodeSetProxy.new(@node.children, self) end |
#display_path ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/json/ld/html/rexml.rb', line 36 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
106 107 108 |
# File 'lib/json/ld/html/rexml.rb', line 106 def element? @node.is_a?(::REXML::Element) end |
#inner_html ⇒ String
Inner text of an element
87 88 89 |
# File 'lib/json/ld/html/rexml.rb', line 87 def inner_html @node.children.map(&:to_s).join end |
#inner_text ⇒ String
Inner text of an element
75 76 77 78 79 80 |
# File 'lib/json/ld/html/rexml.rb', line 75 def inner_text coder = HTMLEntities.new ::REXML::XPath.match(@node,'.//text()').map { |e| coder.decode(e) }.join end |
#text? ⇒ Boolean
Node type accessors
102 103 104 |
# File 'lib/json/ld/html/rexml.rb', line 102 def text? @node.is_a?(::REXML::Text) end |
#text_content? ⇒ Array<:text, :element, :attribute>
Return true of all child elements are text
53 54 55 |
# File 'lib/json/ld/html/rexml.rb', line 53 def text_content? @node.children.all? {|c| c.is_a?(::REXML::Text)} end |
#to_s ⇒ Object
114 |
# File 'lib/json/ld/html/rexml.rb', line 114 def to_s; @node.to_s; end |