Class: RECS4::XML::Element

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

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Element

Returns a new instance of Element.



7
8
9
10
11
12
13
# File 'lib/recs4/xml/element.rb', line 7

def initialize(element)
  @rexml_element = element
  @child_element_names = {}
  @attribute_names = {}
  extract_child_element_names
  extract_attribute_names
end

Instance Method Details

#[](element_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/recs4/xml/element.rb', line 15

def [](element_name)
  wanted_name = element_name.to_s
  wanted_elems = @rexml_element.get_elements(wanted_name)
  if wanted_elems.empty?
    if @child_element_names[wanted_name]
      wanted_elems = @rexml_element.get_elements(@child_element_names[wanted_name])
      if wanted_elems.empty?
        raise
      end
    else
      raise
    end
  end
  wanted_elems.map! do |e|
    Element.new(e)
  end
  return Elements.new(wanted_elems)
rescue
  return nil
end

#a(attribute_name) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/recs4/xml/element.rb', line 36

def a(attribute_name)
  attr = @rexml_element.attributes[attribute_name]
  unless attr
    attr = @rexml_element.attributes[@attribute_names[attribute_name]]
  end
  return attr
end

#textObject



44
45
46
# File 'lib/recs4/xml/element.rb', line 44

def text
  return @rexml_element.text
end

#to_sObject



48
49
50
# File 'lib/recs4/xml/element.rb', line 48

def to_s
  return @rexml_element.to_s
end