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

Version Path
tracksperanto-2.1.0 lib/tracksperanto/flame_builder.rb
tracksperanto-2.0.2 lib/tracksperanto/flame_builder.rb
tracksperanto-2.0.1 lib/tracksperanto/flame_builder.rb
tracksperanto-2.0.0 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.9 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.8 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.6 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.5 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.4 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.3 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.2 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.1 lib/tracksperanto/flame_builder.rb
tracksperanto-1.9.0 lib/tracksperanto/flame_builder.rb
tracksperanto-1.8.4 lib/tracksperanto/flame_builder.rb
tracksperanto-1.8.2 lib/tracksperanto/flame_builder.rb