Class: Amanzi::XML::Document

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

Direct Known Subclasses

HTML::Document, SLD::Document

Instance Attribute Summary

Attributes inherited from Element

#attributes, #children, #depth, #name, #namespace

Instance Method Summary collapse

Methods inherited from Element

#<<, #[]=, #add_child, #attribute_text, camelize, #contents, #indent, #initialize, #insert, #insert_child, #make_child, #ns, #tag

Constructor Details

This class inherits a constructor from Amanzi::XML::Element

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



111
112
113
114
# File 'lib/amanzi/xml.rb', line 111

def method_missing(symbol,*args,&block)
  #puts "Method missing in XML::Document: #{symbol}"
  singleton_method(symbol,*args,&block) || super(symbol,*args,&block)
end

Instance Method Details

#add_singleton_elements(elements = []) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/amanzi/xml.rb', line 90

def add_singleton_elements(elements=[])
  prev_e = nil
  singleton_elements << elements.map do |e|
    parent = prev_e || self
    element = Element.new(Element.camelize(e), parent.depth + 1)
    parent << element
    prev_e = element
  end
end

#comment(text) ⇒ Object



87
88
89
# File 'lib/amanzi/xml.rb', line 87

def comment text
  !@singleton_elements.empty? && @singleton_elements.flatten[-1].comment(text)
end

#singleton_elementsObject



84
85
86
# File 'lib/amanzi/xml.rb', line 84

def singleton_elements
  @singleton_elements ||= []
end

#singleton_method(symbol, *args, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/amanzi/xml.rb', line 99

def singleton_method(symbol,*args,&block)
  name = Element.camelize symbol
  singleton_elements.flatten.each do |element|
    if element.name === name
      #puts "Found singleton: #{name}"
      block && block.call(element)
      return element
    end
  end
  #puts "Found no singletons for #{name}"
  nil
end

#to_sObject



115
116
117
# File 'lib/amanzi/xml.rb', line 115

def to_s
  to_xml
end

#to_xml(options = {}) ⇒ Object



81
82
83
# File 'lib/amanzi/xml.rb', line 81

def to_xml(options={})
  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" + super(options) + "\n"
end