Class: EimXML::Formatter
- Inherits:
-
Object
- Object
- EimXML::Formatter
- Defined in:
- lib/eim_xml/formatter.rb,
lib/eim_xml/formatter/element_wrapper.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ElementWrapper
Instance Attribute Summary collapse
-
#out ⇒ Object
readonly
Returns the value of attribute out.
Class Method Summary collapse
Instance Method Summary collapse
- #indent(&proc) ⇒ Object
-
#initialize(opt) ⇒ Formatter
constructor
A new instance of Formatter.
- #preserve_space_element?(elm) ⇒ Boolean
- #write(src) ⇒ Object
- #write_comment(c) ⇒ Object
- #write_contents_of(elm) ⇒ Object
- #write_element(elm) ⇒ Object
- #write_indent ⇒ Object
- #write_newline ⇒ Object
- #write_pcstring(pcs) ⇒ Object
- #write_string(str) ⇒ Object
- #write_wrapper(wrapper) ⇒ Object
Constructor Details
#initialize(opt) ⇒ Formatter
Returns a new instance of Formatter.
13 14 15 16 17 18 19 20 |
# File 'lib/eim_xml/formatter.rb', line 13 def initialize(opt) @out = opt[:out] @preservers = opt[:preservers] @preserve_space = false @indent_string = " " @indent_depth = 0 @option = opt.dup.tap{|h| [:out, :preservers].each{|k| h.delete(k)}} end |
Instance Attribute Details
#out ⇒ Object (readonly)
Returns the value of attribute out.
5 6 7 |
# File 'lib/eim_xml/formatter.rb', line 5 def out @out end |
Class Method Details
.write(element, opt = {}) ⇒ Object
7 8 9 10 11 |
# File 'lib/eim_xml/formatter.rb', line 7 def self.write(element, opt={}) opt = {:out=>""}.merge(opt) new(opt).write(element) opt[:out] end |
Instance Method Details
#indent(&proc) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/eim_xml/formatter.rb', line 37 def indent(&proc) @indent_depth += 1 proc.call ensure @indent_depth -= 1 end |
#preserve_space_element?(elm) ⇒ Boolean
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/eim_xml/formatter.rb', line 44 def preserve_space_element?(elm) @preservers && @preservers.any? do |e| case e when Symbol e==elm.name when Class e===elm end end end |
#write(src) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/eim_xml/formatter.rb', line 22 def write(src) case src when ElementWrapper write_wrapper(src) when Comment write_comment(src) when Element write_element(src) when PCString write_pcstring(src) else write_string(src.to_s) end end |
#write_comment(c) ⇒ Object
63 64 65 66 67 |
# File 'lib/eim_xml/formatter.rb', line 63 def write_comment(c) write_indent c.write_to(out) write_newline end |
#write_contents_of(elm) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/eim_xml/formatter.rb', line 69 def write_contents_of(elm) flag = @preserve_space @preserve_space = true if preserve_space_element?(elm) write_newline indent do elm.contents.each do |c| write(c) end end write_indent ensure @preserve_space = flag end |
#write_element(elm) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/eim_xml/formatter.rb', line 83 def write_element(elm) write_indent out << "<" elm.name_and_attributes(out) case elm.contents.size when 0 out << " />" write_newline else out << ">" write_contents_of(elm) out << "</#{elm.name}>" write_newline end end |
#write_indent ⇒ Object
55 56 57 |
# File 'lib/eim_xml/formatter.rb', line 55 def write_indent out << @indent_string*@indent_depth unless @preserve_space end |
#write_newline ⇒ Object
59 60 61 |
# File 'lib/eim_xml/formatter.rb', line 59 def write_newline out << "\n" unless @preserve_space end |
#write_pcstring(pcs) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/eim_xml/formatter.rb', line 99 def write_pcstring(pcs) pcs.encoded_string.each_line do |l| write_indent out << l end write_newline end |
#write_string(str) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/eim_xml/formatter.rb', line 107 def write_string(str) PCString.encode(str).each_line do |l| write_indent out << l end write_newline end |
#write_wrapper(wrapper) ⇒ Object
115 116 117 118 119 |
# File 'lib/eim_xml/formatter.rb', line 115 def write_wrapper(wrapper) wrapper.each(@option) do |i| write(i) end end |