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.



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

def initialize(*a, &b)
  @doc = ::REXML::Document::new(*a, &b)
  @stack = [@doc]
  @size = 0
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



50
51
52
# File 'lib/rcov/xx.rb', line 50

def doc
  @doc
end

#sizeObject (readonly)

Returns the value of attribute size.



52
53
54
# File 'lib/rcov/xx.rb', line 52

def size
  @size
end

#stackObject (readonly)

Returns the value of attribute stack.



51
52
53
# File 'lib/rcov/xx.rb', line 51

def stack
  @stack
end

Instance Method Details

#<<(object) ⇒ Object



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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rcov/xx.rb', line 108

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rcov/xx.rb', line 90

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



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

def pop
  @stack.pop unless @stack.size == 1
end

#pretty(port = '') ⇒ Object



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

def pretty(port = '') 
  @doc.write port, indent=2, transitive=false, ie_hack=true
  port
end

#push(element) ⇒ Object



64
65
66
# File 'lib/rcov/xx.rb', line 64

def push(element)
  @stack.push element
end

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



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

def to_str(port = "")
  @doc.write port, indent=-1, transitive=false, ie_hack=true
  port
end

#topObject



60
61
62
# File 'lib/rcov/xx.rb', line 60

def top
  @stack.last
end

#tracking_additionsObject



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

def tracking_additions
  n = @size
  yield
  return @size - n
end