class Xdrgen::OutputFile

Constants

SPACES_PER_INDENT

Public Class Methods

new(path) click to toggle source
# File lib/xdrgen/output_file.rb, line 6
def initialize(path)
  @path           = path
  @current_indent = 0

  FileUtils.mkdir_p File.dirname(@path)
  @io = File.open(@path, 'w')
end

Public Instance Methods

balance_after(balance_regex, include_space=false) { || ... } click to toggle source
# File lib/xdrgen/output_file.rb, line 38
def balance_after(balance_regex, include_space=false)
  @old_io = @io
  @io = StringIO.new
  yield
  raw = @io.string
  @old_io.puts balance_string(raw, balance_regex, include_space)
ensure
  @io = @old_io
  @old_io = nil
end
break() click to toggle source
# File lib/xdrgen/output_file.rb, line 30
def break
  @break_needed = true
end
close() click to toggle source
# File lib/xdrgen/output_file.rb, line 14
def close
  @io.close
end
indent(step=1) { || ... } click to toggle source
# File lib/xdrgen/output_file.rb, line 23
def indent(step=1)
  @current_indent += step
  yield
ensure
  @current_indent -= step
end
puts(s="") click to toggle source
# File lib/xdrgen/output_file.rb, line 18
def puts(s="")
  write_break_if_needed
  @io.puts indented(s)
end
unbreak() click to toggle source
# File lib/xdrgen/output_file.rb, line 34
def unbreak
  @break_needed = false
end

Private Instance Methods

balance_string(raw, balance_regex, include_space) click to toggle source
# File lib/xdrgen/output_file.rb, line 61
def balance_string(raw, balance_regex, include_space)
  lines            = raw.split("\n")
  splits           = lines.map{|l| split_line_at(l, balance_regex)}

  max_split_length = splits.map{|s| s.first.length }.compact.max || -1
  max_split_length += 1 if include_space

  splits.map do |first, rest|
    next first if rest.blank?

    (first || "").ljust(max_split_length) + rest
  end.join("\n")
end
indented(s) click to toggle source
# File lib/xdrgen/output_file.rb, line 50
def indented(s)
  s.indent(@current_indent * SPACES_PER_INDENT)
end
split_line_at(line, regex) click to toggle source
# File lib/xdrgen/output_file.rb, line 75
def split_line_at(line, regex)
  match = regex.match(line)

  if match.blank?
    [line.rstrip, nil]
  else
    split_point = match.end(0)
    [line[0...split_point], line[split_point..-1]]
  end
end
write_break_if_needed() click to toggle source
# File lib/xdrgen/output_file.rb, line 54
def write_break_if_needed
  return unless @break_needed
  @io.puts ""
ensure
  @break_needed = false
end