Sha256: 37aa57160d38586d34cf7ece00aa1e30c1492e95c61aba001fecceff2d13ba54
Contents?: true
Size: 1.65 KB
Versions: 35
Compression:
Stored size: 1.65 KB
Contents
# A Builder-like class for exporting Flame setups class Tracksperanto::FlameBuilder INDENT = "\t" def initialize(io, indent = 0) @io, @indent = io, indent end def write_block!(name, value = nil, &blk) value.nil? ? write_loose!(name) : write_tuple!(name, value) yield(self.class.new(@io, @indent + 1)) @io.puts(INDENT * (@indent + 1) + "End") end def write_unterminated_block!(name, value = nil, &blk) value.nil? ? write_loose!(name) : write_tuple!(name, value) yield(self.class.new(@io, @indent + 1)) end def write_tuple!(key, value) @io.puts("%s%s %s" % [INDENT * @indent, __camelize(key), __flameize(value)]) end def write_loose!(value) @io.puts("%s%s" % [INDENT * @indent, __camelize(value)]) end def linebreak!(how_many = 1) @io.write("\n" * how_many) end def color_hash!(name, red, green, blue) write_unterminated_block!(name) do | b | b.red(red) b.green(green) b.blue(blue) end end def <<(some_verbatim_string) some_verbatim_string.split("\n").each do | line | @io.puts(["\t" * @indent, line].join) end end private def method_missing(meth, arg = nil) if block_given? write_block!(meth, arg) {|c| yield(c) } else if arg.nil? write_loose!(meth) else write_tuple!(meth, arg) end end end def __camelize(s) @@camelizations ||= {} @@camelizations[s] ||= s.to_s.gsub(/(^|_)(.)/) { $2.upcase } end def __flameize(v) case v when Float "%.3f" % v when TrueClass "yes" when FalseClass "no" else v.to_s end end end
Version data entries
35 entries across 35 versions & 1 rubygems