Sha256: aad5d93ef5973d0a553d15ade605e3761f4be737976cd32de66bffac56fe22de

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

# Author::    Nicolas Pouillard  <ertai@lrde.epita.fr>.
# Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved.
# License::   LGPL
# $Id: delegator.rb 567 2005-04-13 08:00:06Z polrop $


require 'io/filters'
require 'delegate'

class IO

  class Delegator < DelegateClass(IO)

    def syswrite(o)
      super o
    end

    def write(o)
      syswrite(o.to_s)
    end

    def <<(o)
      syswrite(o.to_s)
      self
    end

    def print(*args)
      args.each { |x| syswrite(x.to_s) }
    end
    def printf(fmt, *args)
      @output.printf fmt, args.map { |x| @block[x] }
    end
    def putc(c)
      @output.putc @block[c]
    end
    def puts(*args)
      @output.print args.map { |x| @block[x.to_s] }
    end

  end

end

class << IO

  def print_io(io)
    io.each do |line|
      self.puts line.chomp!
    end
  end

  def print_file(file)
    fh = File.new(file, 'r')
    self.print_io fh
    fh.close
  end

  def dash
    self.puts '-'
  end

  module ImplIndent
    def indent(x=nil, &block)
      lvl = (x and x.is_a?(Numeric)) ? '  ' * x : x.to_s
      l = IO::OutputFIlter.new(self) { |x| lvl + x.gsub!(/\n/, "\n#{lvl}") }
      block[l]
    end
  end
  include ImplIndent

end

#FIXME: adapt me to a real unit test suite
# if __FILE__ == $0

#   class A < DelegateClass(IO) # :nodoc:
#     def syswrite(x)
#       super('--'+x)
#     end
#     def write(x)
#       super('--'+x)
#     end
#   end

#   a = IOExample.new($stdout)
#   a << 'foo'

# end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ttk-0.1.576 lib/ttk/Tools/io/delegator.rb
ttk-0.1.579 lib/ttk/Tools/io/delegator.rb
ttk-0.1.580 lib/ttk/Tools/io/delegator.rb