Class: XX::Document
- Inherits:
-
Object
- Object
- XX::Document
- Defined in:
- lib/xx.rb,
lib/xx-0.1.0.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
–{{{.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
-
#<<(object) ⇒ Object
–}}}.
-
#create(element) ⇒ Object
–}}}.
-
#initialize(*a, &b) ⇒ Document
constructor
A new instance of Document.
-
#pop ⇒ Object
–}}}.
- #pretty(port = '') ⇒ Object
-
#push(element) ⇒ Object
–}}}.
-
#to_str(port = "") ⇒ Object
(also: #to_s)
–}}}.
-
#top ⇒ Object
–}}}.
-
#tracking_additions ⇒ Object
–}}}.
Constructor Details
#initialize(*a, &b) ⇒ Document
Returns a new instance of Document.
23 24 25 26 27 28 29 |
# File 'lib/xx.rb', line 23 def initialize *a, &b #--{{{ @doc = ::REXML::Document::new *a, &b @stack = [@doc] @size = 0 #--}}} end |
Instance Attribute Details
#doc ⇒ Object (readonly)
–{{{
19 20 21 |
# File 'lib/xx.rb', line 19 def doc @doc end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
21 22 23 |
# File 'lib/xx.rb', line 21 def size @size end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
20 21 22 |
# File 'lib/xx.rb', line 20 def stack @stack end |
Instance Method Details
#<<(object) ⇒ Object
–}}}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/xx.rb', line 84 def << object #--{{{ t, x = top, object if x case t when ::REXML::Document begin t << case x when ::REXML::Document x.root || ::REXML::Text::new(x.to_s) when ::REXML::Element x when ::REXML::CData x when ::REXML::Text x else # string ::REXML::Text::new(x.to_s) end rescue if t.respond_to? "root" t = t.root retry else raise end end when ::REXML::Element t << case x when ::REXML::Document x.root || ::REXML::Text::new(x.to_s) when ::REXML::Element x when ::REXML::CData #::REXML::Text::new(x.write("")) x when ::REXML::Text x else # string ::REXML::Text::new(x.to_s) end when ::REXML::Text t << case x when ::REXML::Document x.write "" when ::REXML::Element x.write "" when ::REXML::CData x.write "" when ::REXML::Text x.write "" else # string x.to_s end else # other - try anyhow t << case x when ::REXML::Document x.write "" when ::REXML::Element x.write "" when ::REXML::CData x.write "" when ::REXML::Text x.write "" else # string x.to_s end end end @size += 1 self #--}}} end |
#create(element) ⇒ Object
–}}}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/xx.rb', line 65 def create element #--{{{ push element begin object = nil additions = tracking_additions do object = yield element if block_given? end if object and additions.zero? self << object end ensure pop end self << element element #--}}} end |
#pop ⇒ Object
–}}}
40 41 42 43 44 |
# File 'lib/xx.rb', line 40 def pop #--{{{ @stack.pop unless @stack.size == 1 #--}}} end |
#pretty(port = '') ⇒ Object
59 60 61 62 63 64 |
# File 'lib/xx.rb', line 59 def pretty port = '' #--{{{ @doc.write port, indent=2, transitive=false, ie_hack=true port #--}}} end |
#push(element) ⇒ Object
–}}}
35 36 37 38 39 |
# File 'lib/xx.rb', line 35 def push element #--{{{ @stack.push element #--}}} end |
#to_str(port = "") ⇒ Object Also known as: to_s
–}}}
52 53 54 55 56 57 |
# File 'lib/xx.rb', line 52 def to_str port = "" #--{{{ @doc.write port, indent=-1, transitive=false, ie_hack=true port #--}}} end |
#top ⇒ Object
–}}}
30 31 32 33 34 |
# File 'lib/xx.rb', line 30 def top #--{{{ @stack.last #--}}} end |
#tracking_additions ⇒ Object
–}}}
45 46 47 48 49 50 51 |
# File 'lib/xx.rb', line 45 def tracking_additions #--{{{ n = @size yield return @size - n #--}}} end |