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.



117
118
119
120
# File 'lib/thinp_xml/thinp/xml_format.rb', line 117

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

Instance Method Details

#emit_line(str) ⇒ Object



135
136
137
# File 'lib/thinp_xml/thinp/xml_format.rb', line 135

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

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



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

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



143
144
145
# File 'lib/thinp_xml/thinp/xml_format.rb', line 143

def pop
  @indent -= 2
end

#pushObject



139
140
141
# File 'lib/thinp_xml/thinp/xml_format.rb', line 139

def push
  @indent += 2
end