Class: SRXML::XML
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- SRXML::XML
- Defined in:
- lib/srxml.rb
Overview
Main class. Use it to create the xml-output.
For example:
xml = SRXML::XML.new :xml => false
xml.people{
xml.person{
xml.name "Todd"
}
xml.person{
xml.name "Mary"
}
}
puts xml.to_s :formatted
Will give you:
<people>
<person>
<name>Todd</name>
</person>
<person>
<name>Mary</name>
</person
</people>
Constant Summary collapse
- @@html_singles =
[:br, :hr, :img, :input, :meta, :param, :link, :base, :embed, :dd, :dt]
Instance Attribute Summary collapse
-
#sep ⇒ Object
readonly
Returns the value of attribute sep.
-
#singles ⇒ Object
defines tags, which don’t have a closing-tag (e.g. <br/>).
-
#xml_tag ⇒ Object
readonly
indicates, if xml-tag is used (should be false for html-mode, for example).
Class Method Summary collapse
-
.html_singles ⇒ Object
holds a seperator-string, indicating, where a newline (for formatted-output) should be placed if not specified, its default value is ‘<>’.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ XML
constructor
Constructor.
-
#method_missing(method_name, *args) ⇒ Object
This method is used to create the xml-ouput based on the called methods on the XML-object.
-
#to_s(option = :non_formatted) ⇒ Object
Returns the xml-output string created by using the xml-object.
Constructor Details
#initialize(options = {}) ⇒ XML
Constructor. Takes a options-hash. Valid options are:
:xml => true/false # indicates if xml-tag should be used at top of xml-output
:sep => "<>" # custom specified seperator-string; '<>' if not set
:singles => [] # custom specified list of single-tags (no closing tag), e.g. '<br/>' in html
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/srxml.rb', line 53 def initialize( = {}) @xml_tag = [:xml].nil? ? true : [:xml] @sep = [:sep] || "<>" @singles = [:singles] || [] # if not specified, there are no single-tags if @xml_tag @output = ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>#{@sep}"] else if [:html] @singles += @@html_singles end @output = [] end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
This method is used to create the xml-ouput based on the called methods on the XML-object.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/srxml.rb', line 70 def method_missing(method_name, *args) @output << "<#{method_name}" attributes = [] value = "" args.each do |a| if a.class != Hash value = a else # shove all key-value-pairs in as attributes to the xml-tag a.keys.sort_by{|k| k.to_s}.each do |key| attributes << " #{key}=\"#{a[key]}\"" end end end attributes.each do |a| @output << a end @output << ">" @output << "#{value}" unless @singles.include?(method_name) if block_given? @output << @sep @output << yield end unless @singles.include?(method_name) @output << "</#{method_name}>" @output << @sep end end |
Instance Attribute Details
#sep ⇒ Object (readonly)
Returns the value of attribute sep.
41 42 43 |
# File 'lib/srxml.rb', line 41 def sep @sep end |
#singles ⇒ Object
defines tags, which don’t have a closing-tag (e.g. <br/>)
39 40 41 |
# File 'lib/srxml.rb', line 39 def singles @singles end |
#xml_tag ⇒ Object (readonly)
indicates, if xml-tag is used (should be false for html-mode, for example)
40 41 42 |
# File 'lib/srxml.rb', line 40 def xml_tag @xml_tag end |
Class Method Details
.html_singles ⇒ Object
holds a seperator-string, indicating, where a newline (for formatted-output) should be placed if not specified, its default value is ‘<>’
44 45 46 |
# File 'lib/srxml.rb', line 44 def self.html_singles @@html_singles end |
Instance Method Details
#to_s(option = :non_formatted) ⇒ Object
Returns the xml-output string created by using the xml-object. Default-output is non_formatted. Optional output-styles are:
:formatted # will put newlines in the correct places
:keep_sep # will leave seperator-string in place (probably non-valid xml then - mainly for debug purposes)
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/srxml.rb', line 109 def to_s(option = :non_formatted) @output = @output.select{|x| x.class == String} if option == :formatted # format here with newline etc. @output.join("").gsub(@sep, "\n") elsif option == :keep_sep @output.join("") else @output.join("").gsub(@sep, "") end end |