Class: REXML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/rexmladdons.rb

Overview

this class adds a few helper methods to REXML::Element

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(xmlelement) ⇒ Object



92
93
94
# File 'lib/xmpp4r/rexmladdons.rb', line 92

def self.import(xmlelement)
  self.new.import(xmlelement)
end

Instance Method Details

#delete_elements(element) ⇒ Object

Deletes one or more children elements, not just one like REXML::Element#delete_element



99
100
101
# File 'lib/xmpp4r/rexmladdons.rb', line 99

def delete_elements(element)
  while(delete_element(element)) do end
end

#first_element(e) ⇒ Object

Returns first element of name e



49
50
51
52
# File 'lib/xmpp4r/rexmladdons.rb', line 49

def first_element(e)
  each_element(e) { |el| return el }
  return nil
end

#first_element_text(e) ⇒ Object

Returns text of first element of name e



56
57
58
59
60
61
62
63
# File 'lib/xmpp4r/rexmladdons.rb', line 56

def first_element_text(e)
  el = first_element(e)
  if el
    return el.text
  else
    return nil
  end
end

#import(xmlelement) ⇒ Object

import this element’s children and attributes



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/xmpp4r/rexmladdons.rb', line 76

def import(xmlelement)
  if @name and @name != xmlelement.name
    raise "Trying to import an #{xmlelement.name} to a #{@name} !"
  end
  add_attributes(xmlelement.attributes.clone)
  @context = xmlelement.context
  xmlelement.each do |e|
    if e.kind_of? REXML::Element
      typed_add(e.deep_clone)
    else # text element, probably.
      add(e.clone)
    end
  end
  self
end

#replace_element_text(e, t) ⇒ Object

Replaces or add a child element of name e with text t.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xmpp4r/rexmladdons.rb', line 35

def replace_element_text(e, t)
  el = first_element(e)
  if el.nil?
    el = REXML::Element::new(e)
    add_element(el)
  end
  if t
    el.text = t
  end
  self
end

#typed_add(e) ⇒ Object

This method does exactly the same thing as add(), but it can be overriden by subclasses to provide on-the-fly object creations. For example, if you import a REXML::Element of name ‘plop’, and you have a Plop class that subclasses REXML::Element, with typed_add you can get your REXML::Element to be “magically” converted to Plop.



70
71
72
# File 'lib/xmpp4r/rexmladdons.rb', line 70

def typed_add(e)
  add(e)
end