class Genprovider::Output

Attributes

dir[R]
name[R]

Public Instance Methods

comment(str=nil, lmargin = nil) click to toggle source
# File lib/genprovider/output.rb, line 64
  def comment str=nil, lmargin = nil
    if str =~ /\n/
      comment $`, lmargin
      comment $', lmargin  #' <- colorize helper
      return self
    end
    wrap = @wrap - (@depth * @indent + 2) - ((lmargin)?lmargin00)
    if str && str.size > wrap # must wrap
#      STDERR.puts "#{str.size} > #{wrap}"
      pivot = wrap
      while pivot > 0 && str[pivot,1] != " " # search space left of wrap
        pivot -= 1
      end
      if pivot == 0 # no space left of wrap
        pivot = wrap
        while pivot < str.size && str[pivot,1] != " " # search space right of wrap
          pivot += 1
        end
      end
      if 0 < pivot && pivot < str.size
#        puts "-wrap @ #{pivot}-"
        comment str[0,pivot], lmargin
        comment str[pivot+1..-1], lmargin
        return self
      end
    end
    indent
    @file.write "#"
    @file.write(" " * lmargin) if lmargin
    @file.write " #{str}" if str
    @file.puts
    self
  end
dec() click to toggle source
# File lib/genprovider/output.rb, line 36
def dec
  @indent -= 1
  @indent = 0 if @indent < 0
  self
end
def(name, *args) click to toggle source
# File lib/genprovider/output.rb, line 52
def def name, *args
  if args.nil? || args.empty? || args[0].nil?
    self.puts "def #{name}"
  else
    self.puts "def #{name}( #{args.join(', ')} )"
  end
  self.inc
end
end() click to toggle source
# File lib/genprovider/output.rb, line 60
def end
  self.dec
  self.puts "end"
end
inc() click to toggle source
# File lib/genprovider/output.rb, line 32
def inc
  @indent += 1
  self
end
printf(format, *args) click to toggle source
# File lib/genprovider/output.rb, line 97
def printf format, *args
  indent
  Kernel.printf @file, format, *args
  @newline = false
  self
end
puts(str="") click to toggle source
# File lib/genprovider/output.rb, line 46
def puts str=""
  indent unless str.empty?
  @file.puts str
  @newline = true
  self
end
write(str) click to toggle source
# File lib/genprovider/output.rb, line 41
def write str
  @file.write str
  @newline = false
  self
end

Public Class Methods

new(file, force=false) { |self| ... } click to toggle source
# File lib/genprovider/output.rb, line 12
def initialize file, force=false
  if file.kind_of?(IO)
    @file = file
    @name = nil
  else
    if File.exist?(file) && !force
      STDERR.puts "Not overwriting existing #{file}"
      return
    end
    @file = File.open(file, "w+")
    @name = File.basename file
    @dir = File.expand_path(File.dirname file)
  end
  raise "Cannot create file at #{file}" unless @file
  @newline = true
  @indent = 0
  @wrap = 75 # wrap at this column
  @depth = 2 # indent depth
  yield self if block_given?
end