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



77
78
79
# File 'lib/xmpp4r/rexmladdons.rb', line 77

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



84
85
86
# File 'lib/xmpp4r/rexmladdons.rb', line 84

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

#first_element(e) ⇒ Object

Returns first element of name e



34
35
36
37
# File 'lib/xmpp4r/rexmladdons.rb', line 34

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



41
42
43
44
45
46
47
48
# File 'lib/xmpp4r/rexmladdons.rb', line 41

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/xmpp4r/rexmladdons.rb', line 61

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.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xmpp4r/rexmladdons.rb', line 20

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.



55
56
57
# File 'lib/xmpp4r/rexmladdons.rb', line 55

def typed_add(e)
  add(e)
end