Class: IronNails::Core::IronXml

Inherits:
Object
  • Object
show all
Defined in:
lib/ironnails/iron_xml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_doc, parent = nil) ⇒ IronXml

Returns a new instance of IronXml.



9
10
11
12
# File 'lib/ironnails/iron_xml.rb', line 9

def initialize(xml_doc, parent=nil)
  @xml_node = xml_doc
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &b) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/ironnails/iron_xml.rb', line 69

def method_missing(sym, *args, &b)
  # this is what gives us the syntactic sugar over the 
  # Xlinq like syntax
  if block_given?
    self.elements(sym.to_s, &b)
  else
    self.element(sym.to_s).value
  end

end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/ironnails/iron_xml.rb', line 7

def parent
  @parent
end

#xml_nodeObject (readonly)

Returns the value of attribute xml_node.



7
8
9
# File 'lib/ironnails/iron_xml.rb', line 7

def xml_node
  @xml_node
end

Class Method Details

.parse(content, content_type = :string, &b) ⇒ Object

parses an xml document. content can be either a string containing xml, a path to an xml document or a System::IO::Reader. You tell IronXml which type of content you have by passing in a content_type. content_type is a symbol that defaults to :string it expects a block to traverse the xml provided.



58
59
60
61
62
63
64
65
66
67
# File 'lib/ironnails/iron_xml.rb', line 58

def self.parse(content, content_type = :string, &b)
  xdoc = XmlDocument.new

  if content_type == :string
    xdoc.load_xml(content)
  else
    xdoc.load(content)
  end
  b.call(IronXml.new(xdoc))
end

Instance Method Details

#element(name) ⇒ Object

selects a single child node from the tree



28
29
30
31
32
33
34
35
# File 'lib/ironnails/iron_xml.rb', line 28

def element(name)
  if has_element? name
    ele = @xml_node.select_single_node xp_pref(name)
    xml = IronXml.new(ele, self)
    yield xml if block_given?
    xml
  end
end

#elements(name, &b) ⇒ Object

selects multiple child nodes from the tree



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ironnails/iron_xml.rb', line 15

def elements(name, &b)
  if has_element? name
    ele = @xml_node.select_nodes xp_pref(name)
    ele.each do |el|
      raise "Expected a block for multiple elements" unless block_given?
      b.call IronXml.new(el, self) unless el.nil?
    end
  else
    []
  end
end

#has_element?(name) ⇒ Boolean

indicates whether the element is present in the selected nodes.

Returns:

  • (Boolean)


38
39
40
# File 'lib/ironnails/iron_xml.rb', line 38

def has_element?(name)
  !@xml_node.nil? && @xml_node.select_nodes("#{name}").count > 0
end

#valueObject

the value of the selected node



48
49
50
# File 'lib/ironnails/iron_xml.rb', line 48

def value
  @xml_node.inner_text
end

#xp_pref(name) ⇒ Object

the XPath prefix (rooted or not?)



43
44
45
# File 'lib/ironnails/iron_xml.rb', line 43

def xp_pref(name)
  @parent.nil? ? "//#{name}" : name
end