Class: RDF::RDFXML::Reader::Nokogiri::NodeProxy
- Defined in:
- vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.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
- #add_namespace(prefix, href) ⇒ Object
-
#ancestors ⇒ Object
Ancestors of this element, in order.
- #at_xpath(*args) ⇒ Object
-
#attribute_nodes ⇒ String
Inner text of an element.
-
#attribute_with_ns(name, namespace) ⇒ Nokogiri::XML::Attr
Monkey patch attribute_with_ns, to insure nil is returned for #null?.
-
#base ⇒ String
Get any xml:base in effect for this element.
-
#children ⇒ NodeSetProxy
Children of this node.
-
#create_node(name, children) ⇒ Object
Create a new element child of an existing node.
-
#direction ⇒ String
Element direction.
- #display_path ⇒ Object
-
#initialize(node, parent = nil) ⇒ NodeProxy
constructor
A new instance of NodeProxy.
-
#its_version ⇒ String
ITS version.
-
#language ⇒ String
Element language.
-
#method_missing(method, *args) ⇒ Object
Proxy for everything else to @node.
- #namespace ⇒ Object
-
#namespaces ⇒ Hash{String => String}
Retrieve XMLNS definitions for this element.
-
#text? ⇒ Array<:text, :element, :attribute>
Return true of this is a text node.
-
#text_content? ⇒ Array<:text, :element, :attribute>
Return true of all child elements are text.
- #to_s ⇒ Object
-
#uri ⇒ Object
URI of namespace + node_name.
-
#version ⇒ String
RDF version.
- #xpath(*args) ⇒ Object
Constructor Details
#initialize(node, parent = nil) ⇒ NodeProxy
Returns a new instance of NodeProxy.
21 22 23 24 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 21 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
203 204 205 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 203 def method_missing(method, *args) @node.send(method, *args) end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
18 19 20 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 18 def node @node end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
19 20 21 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 19 def parent @parent end |
Instance Method Details
#add_namespace(prefix, href) ⇒ Object
174 175 176 177 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 174 def add_namespace(prefix, href) @def_namespace = href if prefix.nil? @node.add_namespace(prefix, href) end |
#ancestors ⇒ Object
Ancestors of this element, in order
141 142 143 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 141 def ancestors @ancestors ||= parent ? parent.ancestors + [parent] : [] end |
#at_xpath(*args) ⇒ Object
168 169 170 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 168 def at_xpath(*args) xpath(*args).first end |
#attribute_nodes ⇒ String
Inner text of an element. Decode Entities
def inner_text coder = HTMLEntities.new coder.decode(@node.inner_text) end
154 155 156 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 154 def attribute_nodes @attribute_nodes ||= NodeSetProxy.new(@node.attribute_nodes, self) end |
#attribute_with_ns(name, namespace) ⇒ Nokogiri::XML::Attr
Monkey patch attribute_with_ns, to insure nil is returned for #null?
Get the attribute node with name and namespace
89 90 91 92 93 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 89 def attribute_with_ns(name, namespace) a = @node.attribute_with_ns(name, namespace) (a.respond_to?(:null?) && a.null?) ? nil : a # to ensure FFI Pointer compatibility end |
#base ⇒ String
Get any xml:base in effect for this element
47 48 49 50 51 52 53 54 55 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 47 def base if @base.nil? @base = attribute_with_ns("base", RDF::XML.to_s) || (parent && parent.element? && parent.base) || false end @base == false ? nil : @base end |
#children ⇒ NodeSetProxy
Children of this node
136 137 138 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 136 def children @children ||= NodeSetProxy.new(@node.children, self) end |
#create_node(name, children) ⇒ Object
Create a new element child of an existing node
27 28 29 30 31 32 33 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 27 def create_node(name, children) native = ::Nokogiri::XML::Element.new(name, @node.document) children.each do |c| native.add_child(c.node) end NodeProxy.new(native, self) end |
#direction ⇒ String
Element direction
61 62 63 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 61 def direction attribute_with_ns("dir", RDF::ITS.to_s) end |
#display_path ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 95 def display_path @display_path ||= begin path = [] path << parent.display_path if parent path << @node.name case @node when ::Nokogiri::XML::Element then path.join("/") when ::Nokogiri::XML::Attr then path.join("@") else path.join("?") end end end |
#its_version ⇒ String
ITS version
69 70 71 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 69 def its_version attribute_with_ns("version", RDF::ITS.to_s) end |
#language ⇒ String
Element language
39 40 41 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 39 def language attribute_with_ns("lang", RDF::XML.to_s) end |
#namespace ⇒ Object
179 180 181 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 179 def namespace @def_namespace || @node.namespace end |
#namespaces ⇒ Hash{String => String}
Retrieve XMLNS definitions for this element
128 129 130 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 128 def namespaces @namespaces ||= @node.namespace_definitions.inject({}) {|memo, ns| memo[ns.prefix] = ns.href.to_s; memo } end |
#text? ⇒ Array<:text, :element, :attribute>
Return true of this is a text node
112 113 114 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 112 def text? (@node.text? || @node.cdata?) && !@node.blank? end |
#text_content? ⇒ Array<:text, :element, :attribute>
Return true of all child elements are text
120 121 122 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 120 def text_content? @text_content ||= @node.children.all? {|c| c.text?} end |
#to_s ⇒ Object
199 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 199 def to_s; @node.to_s; end |
#uri ⇒ Object
URI of namespace + node_name
193 194 195 196 197 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 193 def uri ns = namespace || RDF::XML.to_s ns = ns.href if ns.respond_to?(:href) RDF::URI.intern(ns + self.node_name) end |
#version ⇒ String
RDF version
77 78 79 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 77 def version attribute_with_ns("version", RDF.to_uri.to_s) end |
#xpath(*args) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/nokogiri.rb', line 158 def xpath(*args) @node.xpath(*args).map do |n| # Get node ancestors parent = n.ancestors.reverse.inject(nil) do |p,node| NodeProxy.new(node, p) end NodeProxy.new(n, parent) end end |