Class: XML::Smart::Dom::Element
- Inherits:
-
Object
- Object
- XML::Smart::Dom::Element
- Defined in:
- lib/xml/smart_domelement.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #===(cls) ⇒ Object
- #add(*attrs) ⇒ Object
- #add_after(*attrs) ⇒ Object
- #add_before(*attrs) ⇒ Object
- #attributes ⇒ Object
- #children ⇒ Object
- #children? ⇒ Boolean
- #dump ⇒ Object
- #element_only? ⇒ Boolean
- #empty? ⇒ Boolean
- #find(xpath) ⇒ Object
-
#initialize(element) ⇒ Element
constructor
A new instance of Element.
- #mixed? ⇒ Boolean
- #namespace ⇒ Object
- #namespace=(n) ⇒ Object
- #namespace? ⇒ Boolean
- #namespaces ⇒ Object
- #parent ⇒ Object
- #parent? ⇒ Boolean
- #path ⇒ Object
- #qname ⇒ Object
- #replace_by(n) ⇒ Object
- #text ⇒ Object
- #text=(t) ⇒ Object
- #text_only? ⇒ Boolean
- #to_doc ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #unique_id ⇒ Object
- #xinclude! ⇒ Object
Constructor Details
#initialize(element) ⇒ Element
Returns a new instance of Element.
6 7 8 |
# File 'lib/xml/smart_domelement.rb', line 6 def initialize(element) @element = element end |
Instance Method Details
#==(other) ⇒ Object
197 198 199 200 201 |
# File 'lib/xml/smart_domelement.rb', line 197 def ==(other) return false unless other return false unless other.respond_to?(:unique_id) unique_id == other.unique_id end |
#===(cls) ⇒ Object
10 |
# File 'lib/xml/smart_domelement.rb', line 10 def ===(cls); self.is_a? cls; end |
#add(*attrs) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/xml/smart_domelement.rb', line 102 def add(*attrs) tmp, update = add_helper(attrs) res = Dom::smart_helper(@element.add_child tmp) if update @element.document.custom_namespace_prefixes_update @element.document.ns_update end res end |
#add_after(*attrs) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/xml/smart_domelement.rb', line 120 def add_after(*attrs) tmp, update = add_helper(attrs) res = Dom::smart_helper(@element.add_next_sibling tmp) if update @element.document.custom_namespace_prefixes_update @element.document.ns_update end res end |
#add_before(*attrs) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/xml/smart_domelement.rb', line 111 def add_before(*attrs) tmp, update = add_helper(attrs) res = Dom::smart_helper(@element.add_previous_sibling tmp) if update @element.document.custom_namespace_prefixes_update @element.document.ns_update end res end |
#attributes ⇒ Object
172 |
# File 'lib/xml/smart_domelement.rb', line 172 def attributes; AttributeSet.new @element; end |
#children ⇒ Object
177 |
# File 'lib/xml/smart_domelement.rb', line 177 def children; find('*'); end |
#children? ⇒ Boolean
178 |
# File 'lib/xml/smart_domelement.rb', line 178 def children?; find('*').length > 0 end |
#dump ⇒ Object
130 |
# File 'lib/xml/smart_domelement.rb', line 130 def dump; @element.to_s; end |
#element_only? ⇒ Boolean
191 192 193 |
# File 'lib/xml/smart_domelement.rb', line 191 def element_only?; @element.xpath_fast('*').length > 0 && @element.xpath_fast("string(text())") == ''; end |
#empty? ⇒ Boolean
184 |
# File 'lib/xml/smart_domelement.rb', line 184 def empty?; @element.blank?; end |
#find(xpath) ⇒ Object
12 13 14 |
# File 'lib/xml/smart_domelement.rb', line 12 def find(xpath) Dom::smart_helper(@element.xpath_fast(xpath)) end |
#mixed? ⇒ Boolean
185 186 187 |
# File 'lib/xml/smart_domelement.rb', line 185 def mixed?; @element.xpath_fast('*').length > 0 && @element.xpath_fast("string(text())") != ''; end |
#namespace ⇒ Object
136 |
# File 'lib/xml/smart_domelement.rb', line 136 def namespace; namespace? ? Namespace.new(@element.namespace) : nil; end |
#namespace=(n) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/xml/smart_domelement.rb', line 137 def namespace=(n) n = case n when Namespace n.prefix when String n else return end tmp = @element.document.custom_namespace_prefixes[n] || @element.document.user_custom_namespace_prefixes[n] unless tmp.nil? @element.namespace_scopes.each do |nss| @element.namespace = nss if nss.href == tmp end end end |
#namespace? ⇒ Boolean
135 |
# File 'lib/xml/smart_domelement.rb', line 135 def namespace?; !@element.namespace.nil?; end |
#namespaces ⇒ Object
153 |
# File 'lib/xml/smart_domelement.rb', line 153 def namespaces; NamespaceSet.new(self,@element); end |
#parent ⇒ Object
179 180 181 |
# File 'lib/xml/smart_domelement.rb', line 179 def parent Dom::smart_helper(@element.parent) end |
#parent? ⇒ Boolean
182 |
# File 'lib/xml/smart_domelement.rb', line 182 def parent?; !@element.parent.nil?; end |
#path ⇒ Object
195 |
# File 'lib/xml/smart_domelement.rb', line 195 def path; @element.path; end |
#qname ⇒ Object
171 |
# File 'lib/xml/smart_domelement.rb', line 171 def qname; QName.new @element; end |
#replace_by(n) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/xml/smart_domelement.rb', line 162 def replace_by(n) case n when Element; Element.new @element.replace(n.instance_variable_get(:@element)) when NodeSet; NodeSet.new @element.replace(n.instance_variable_get(:@nodeset)) else nil end end |
#text ⇒ Object
174 |
# File 'lib/xml/smart_domelement.rb', line 174 def text; @element.xpath_fast("string(text())"); end |
#text=(t) ⇒ Object
175 |
# File 'lib/xml/smart_domelement.rb', line 175 def text=(t); @element.content = t.to_s if t.respond_to? :to_s; end |
#text_only? ⇒ Boolean
188 189 190 |
# File 'lib/xml/smart_domelement.rb', line 188 def text_only?; @element.xpath_fast('*').length == 0 && @element.xpath_fast("string(text())") != ''; end |
#to_doc ⇒ Object
204 205 206 207 208 |
# File 'lib/xml/smart_domelement.rb', line 204 def to_doc doc = Nokogiri::XML::Document.new doc.root = @element dom = Dom.new(doc) end |
#to_f ⇒ Object
133 |
# File 'lib/xml/smart_domelement.rb', line 133 def to_f; @element.content.to_f; end |
#to_i ⇒ Object
132 |
# File 'lib/xml/smart_domelement.rb', line 132 def to_i; @element.content.to_i; end |
#to_s ⇒ Object
131 |
# File 'lib/xml/smart_domelement.rb', line 131 def to_s; @element.content; end |
#unique_id ⇒ Object
202 |
# File 'lib/xml/smart_domelement.rb', line 202 def unique_id; @element.pointer_id; end |
#xinclude! ⇒ Object
155 156 157 158 159 160 |
# File 'lib/xml/smart_domelement.rb', line 155 def xinclude! @element.do_xinclude @element.document.custom_namespace_prefixes_update @element.document.ns_update true end |