Class: XX::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rcov/xx.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ Document

Returns a new instance of Document.



60
61
62
63
64
65
66
67
# File 'lib/rcov/xx.rb', line 60

def initialize *a, &b
#--{{{
  @doc = ::REXML::Document::new *a, &b
  @stack = [@doc]
  @xmldecl = nil
  @size = 0
#--}}}
end

Instance Attribute Details

#docObject (readonly)

–{{{



55
56
57
# File 'lib/rcov/xx.rb', line 55

def doc
  @doc
end

#sizeObject (readonly)

Returns the value of attribute size.



57
58
59
# File 'lib/rcov/xx.rb', line 57

def size
  @size
end

#stackObject (readonly)

Returns the value of attribute stack.



56
57
58
# File 'lib/rcov/xx.rb', line 56

def stack
  @stack
end

#xmldeclObject

Returns the value of attribute xmldecl.



58
59
60
# File 'lib/rcov/xx.rb', line 58

def xmldecl
  @xmldecl
end

Instance Method Details

#<<(object) ⇒ Object

–}}}



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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rcov/xx.rb', line 128

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rcov/xx.rb', line 109

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

#popObject

–}}}



78
79
80
81
82
# File 'lib/rcov/xx.rb', line 78

def pop
#--{{{
  @stack.pop unless @stack.size == 1
#--}}}
end

#pretty(port = "") ⇒ Object



96
97
98
99
100
101
# File 'lib/rcov/xx.rb', line 96

def pretty port = ""
#--{{{
  @doc.write port, indent=0, transitive=false, ie_hack=true
  port
#--}}}
end

#push(element) ⇒ Object

–}}}



73
74
75
76
77
# File 'lib/rcov/xx.rb', line 73

def push element
#--{{{
  @stack.push element
#--}}}
end

#to_str(port = "") ⇒ Object Also known as: to_s

–}}}



90
91
92
93
94
# File 'lib/rcov/xx.rb', line 90

def to_str port = ""
#--{{{
  xmldecl ? ugly(port) : pretty(port)
#--}}}
end

#topObject

–}}}



68
69
70
71
72
# File 'lib/rcov/xx.rb', line 68

def top
#--{{{
  @stack.last
#--}}}
end

#tracking_additionsObject

–}}}



83
84
85
86
87
88
89
# File 'lib/rcov/xx.rb', line 83

def tracking_additions
#--{{{
  n = @size
  yield
  return @size - n
#--}}}
end

#ugly(port = '') ⇒ Object Also known as: compact

–}}}



102
103
104
105
106
107
# File 'lib/rcov/xx.rb', line 102

def ugly port = ''
#--{{{
  @doc.write port, indent=-1, transitive=false, ie_hack=true
  port
#--}}}
end