Class: XMLFormat::Emitter

Inherits:
Object
  • Object
show all
Defined in:
lib/thinp_xml/thinp/xml_format.rb

Instance Method Summary collapse

Constructor Details

#initialize(out) ⇒ Emitter

Returns a new instance of Emitter.



115
116
117
118
# File 'lib/thinp_xml/thinp/xml_format.rb', line 115

def initialize(out)
  @out = out
  @indent = 0
end

Instance Method Details

#emit_line(str) ⇒ Object



133
134
135
# File 'lib/thinp_xml/thinp/xml_format.rb', line 133

def emit_line(str)
  @out.puts((' ' * @indent) + str)
end

#emit_tag(obj, tag, *fields, &block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/thinp_xml/thinp/xml_format.rb', line 120

def emit_tag(obj, tag, *fields, &block)
  expanded = fields.map {|fld| "#{fld}=\"#{obj.send(fld)}\""}
  if block.nil?
    emit_line "<#{tag} #{expanded.join(' ')}/>"
  else
    emit_line "<#{tag} #{expanded.join(' ')}>"
    push
    yield unless block.nil?
    pop
    emit_line "</#{tag}>"
  end
end

#popObject



141
142
143
# File 'lib/thinp_xml/thinp/xml_format.rb', line 141

def pop
  @indent -= 2
end

#pushObject



137
138
139
# File 'lib/thinp_xml/thinp/xml_format.rb', line 137

def push
  @indent += 2
end