Class: Genprovider::Output
- Inherits:
-
Object
- Object
- Genprovider::Output
- Defined in:
- lib/genprovider/output.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #comment(str = nil, lmargin = nil) ⇒ Object
- #dec ⇒ Object
- #def(name, *args) ⇒ Object
- #end ⇒ Object
- #inc ⇒ Object
-
#initialize(file, force = false) {|_self| ... } ⇒ Output
constructor
A new instance of Output.
- #printf(format, *args) ⇒ Object
- #puts(str = "") ⇒ Object
- #write(str) ⇒ Object
Constructor Details
#initialize(file, force = false) {|_self| ... } ⇒ Output
Returns a new instance of Output.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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.(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 |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
11 12 13 |
# File 'lib/genprovider/output.rb', line 11 def dir @dir end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/genprovider/output.rb', line 11 def name @name end |
Instance Method Details
#comment(str = nil, lmargin = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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)?lmargin:0) 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 ⇒ Object
36 37 38 39 40 |
# File 'lib/genprovider/output.rb', line 36 def dec @indent -= 1 @indent = 0 if @indent < 0 self end |
#def(name, *args) ⇒ Object
52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
60 61 62 63 |
# File 'lib/genprovider/output.rb', line 60 def end self.dec self.puts "end" end |
#inc ⇒ Object
32 33 34 35 |
# File 'lib/genprovider/output.rb', line 32 def inc @indent += 1 self end |
#printf(format, *args) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/genprovider/output.rb', line 97 def printf format, *args indent Kernel.printf @file, format, *args @newline = false self end |
#puts(str = "") ⇒ Object
46 47 48 49 50 51 |
# File 'lib/genprovider/output.rb', line 46 def puts str="" indent unless str.empty? @file.puts str @newline = true self end |
#write(str) ⇒ Object
41 42 43 44 45 |
# File 'lib/genprovider/output.rb', line 41 def write str @file.write str @newline = false self end |