Class: XX::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/xx.rb,
lib/xx-2.1.0.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ Document

Returns a new instance of Document.



29
30
31
32
33
34
35
36
# File 'lib/xx.rb', line 29

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

Instance Attribute Details

#docObject (readonly)

–{{{



24
25
26
# File 'lib/xx.rb', line 24

def doc
  @doc
end

#sizeObject (readonly)

Returns the value of attribute size.



26
27
28
# File 'lib/xx.rb', line 26

def size
  @size
end

#stackObject (readonly)

Returns the value of attribute stack.



25
26
27
# File 'lib/xx.rb', line 25

def stack
  @stack
end

#xmldeclObject

Returns the value of attribute xmldecl.



27
28
29
# File 'lib/xx.rb', line 27

def xmldecl
  @xmldecl
end

Instance Method Details

#<<(object) ⇒ Object

–}}}



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
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/xx.rb', line 97

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/xx.rb', line 78

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

–}}}



47
48
49
50
51
# File 'lib/xx.rb', line 47

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

#pretty(port = "") ⇒ Object



65
66
67
68
69
70
# File 'lib/xx.rb', line 65

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

#push(element) ⇒ Object

–}}}



42
43
44
45
46
# File 'lib/xx.rb', line 42

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

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

–}}}



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

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

#topObject

–}}}



37
38
39
40
41
# File 'lib/xx.rb', line 37

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

#tracking_additionsObject

–}}}



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

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

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

–}}}



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

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