Class: XML::Smart::Dom::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/smart_domelement.rb

Instance Method Summary collapse

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



201
202
203
204
205
# File 'lib/xml/smart_domelement.rb', line 201

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

#attributesObject



176
# File 'lib/xml/smart_domelement.rb', line 176

def attributes; AttributeSet.new @element; end

#childrenObject



181
# File 'lib/xml/smart_domelement.rb', line 181

def children; find('*|text()'); end

#children?Boolean

Returns:

  • (Boolean)


182
# File 'lib/xml/smart_domelement.rb', line 182

def children?; find('*|text()').length > 0 end

#dumpObject



130
# File 'lib/xml/smart_domelement.rb', line 130

def dump; @element.to_s; end

#element_only?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/xml/smart_domelement.rb', line 195

def element_only?;
  @element.xpath_fast('*').length > 0 &&  @element.xpath_fast("string(text())") == '';
end

#empty?Boolean

Returns:

  • (Boolean)


188
# File 'lib/xml/smart_domelement.rb', line 188

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

Returns:

  • (Boolean)


189
190
191
# File 'lib/xml/smart_domelement.rb', line 189

def mixed?;
  @element.xpath_fast('*').length > 0 &&  @element.xpath_fast("string(text())") != '';
end

#namespaceObject



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

Returns:

  • (Boolean)


135
# File 'lib/xml/smart_domelement.rb', line 135

def namespace?; !@element.namespace.nil?; end

#namespacesObject



153
# File 'lib/xml/smart_domelement.rb', line 153

def namespaces; NamespaceSet.new(self,@element); end

#parentObject



183
184
185
# File 'lib/xml/smart_domelement.rb', line 183

def parent
  Dom::smart_helper(@element.parent)
end

#parent?Boolean

Returns:

  • (Boolean)


186
# File 'lib/xml/smart_domelement.rb', line 186

def parent?; !@element.parent.nil?; end

#pathObject



199
# File 'lib/xml/smart_domelement.rb', line 199

def path; @element.path; end

#qnameObject



175
# File 'lib/xml/smart_domelement.rb', line 175

def qname; QName.new @element; end

#replace_by(n) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/xml/smart_domelement.rb', line 166

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

#textObject



178
# File 'lib/xml/smart_domelement.rb', line 178

def text; @element.xpath_fast("string(text())"); end

#text=(t) ⇒ Object



179
# File 'lib/xml/smart_domelement.rb', line 179

def text=(t); @element.content = t.to_s if t.respond_to? :to_s; end

#text_only?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/xml/smart_domelement.rb', line 192

def text_only?;
  @element.xpath_fast('*').length == 0 &&  @element.xpath_fast("string(text())") != '';
end

#to_docObject



208
209
210
211
212
# File 'lib/xml/smart_domelement.rb', line 208

def to_doc
  doc = Nokogiri::XML::Document.new
  doc.root = @element
  dom = Dom.new(doc)
end

#to_fObject



133
# File 'lib/xml/smart_domelement.rb', line 133

def to_f; @element.content.to_f; end

#to_iObject



132
# File 'lib/xml/smart_domelement.rb', line 132

def to_i; @element.content.to_i; end

#to_sObject



131
# File 'lib/xml/smart_domelement.rb', line 131

def to_s; @element.content; end

#unique_idObject



206
# File 'lib/xml/smart_domelement.rb', line 206

def unique_id; @element.pointer_id; end

#xinclude!(basedir = nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/xml/smart_domelement.rb', line 155

def xinclude!(basedir=nil)
  if basedir.is_a?(String) && File.directory?(basedir)
    Dir.chdir(basedir) { @element.do_xinclude Nokogiri::XML::ParseOptions::STRICT }
  else
    @element.do_xinclude Nokogiri::XML::ParseOptions::STRICT
  end  
  @element.document.custom_namespace_prefixes_update
  @element.document.ns_update
  true
end