Module: Icersplicer
- Defined in:
- lib/icersplicer.rb
Overview
Author: Brian Hood Name: Icersplicer
Description:
Main module for file processing
Defined Under Namespace
Modules: VERSION
Constant Summary collapse
- COLOURS =
{"black" => 0, "red" => 1, "green" => 2, "yellow" => 3, "blue" => 4, "purple" => 5, "cyan" => 6, "white" => 7}
- @@nfile =
0- @@exp =
nil- @@keywordsfile =
"keywords.ice"- @@debug =
false
Instance Method Summary collapse
- #closefile ⇒ Object
- #countlines(inputfile) ⇒ Object
- #load_keywords(file) ⇒ Object
- #openfile(outputfile) ⇒ Object
- #print_to_screen(linenum, text, quiet) ⇒ Object
- #processdata(data, outputfile, quietmode) ⇒ Object
- #skip(line) ⇒ Object
- #skip_processor(filter) ⇒ Object
- #stats(inputfile, outputfile) ⇒ Object
- #text_highlighter(text) ⇒ Object
- #text_processor(data) ⇒ Object
- #writefile(data) ⇒ Object
Instance Method Details
#closefile ⇒ Object
158 159 160 161 |
# File 'lib/icersplicer.rb', line 158 def closefile @@exp.close puts "Closing file: #{outputfile}" end |
#countlines(inputfile) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/icersplicer.rb', line 93 def countlines(inputfile) lines = 0 unless inputfile == nil if File.exist?(inputfile) File.open(inputfile) {|n| n.each_line { lines += 1 } } puts "Filename: #{inputfile} Total Line Count: #{lines}" end end end |
#load_keywords(file) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/icersplicer.rb', line 36 def load_keywords(file) keys = Hash.new linenum = 0 unless Dir.exists?("#{Dir.home}/.icersplicer") Dir.mkdir("#{Dir.home}/.icersplicer") end if File.exists?("#{Dir.home}/.icersplicer/#{file}") File.open("#{Dir.home}/.icersplicer/#{file}") {|n| n.each_line {|l| keys.update({linenum => "#{l.strip}"}) unless l.strip == "" puts "L: #{l.strip}" if @@debug == true linenum += 1 } } return keys else return false end end |
#openfile(outputfile) ⇒ Object
149 150 151 152 |
# File 'lib/icersplicer.rb', line 149 def openfile(outputfile) puts "Openfile: #{outputfile}" @@exp = File.open("#{outputfile}", 'w') end |
#print_to_screen(linenum, text, quiet) ⇒ Object
145 146 147 |
# File 'lib/icersplicer.rb', line 145 def print_to_screen(linenum, text, quiet) puts "\e[1;33mLn: #{linenum}:\e[0m\ #{text}" unless quiet == true end |
#processdata(data, outputfile, quietmode) ⇒ Object
163 164 165 166 167 |
# File 'lib/icersplicer.rb', line 163 def processdata(data, outputfile, quietmode) openfile(outputfile) if @@nfile == 0 writefile(data) @@nfile += 1 end |
#skip(line) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/icersplicer.rb', line 131 def skip(line) begin if instance_variable_defined?("@skip_lines") line_element = @skip_lines.index(line) if line_element != nil skiper = @skip_lines[line_element] end end rescue NoMethodError return nil end return skiper end |
#skip_processor(filter) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/icersplicer.rb', line 107 def skip_processor(filter) skip_lines = Array.new filter.to_s.split(",").each {|n| skip_lines << n.to_i # Allow line ranges min = n.split("-")[0].to_i max = n.split("-")[1].to_i unless n.split("-")[1] == nil begin if min > max and max != 0 return false end rescue puts "Range Error: Minimun value can't be more than Maxiumun Range value" exit end min.upto(max) {|s| skip_lines << s unless skip_lines[skip_lines.size - 1] == s } end } return skip_lines end |
#stats(inputfile, outputfile) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/icersplicer.rb', line 169 def stats(inputfile, outputfile) print "Inputfile lines: " countlines(inputfile) #print "Outputfile lines: " #countlines(outputfile) end |
#text_highlighter(text) ⇒ Object
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 |
# File 'lib/icersplicer.rb', line 65 def text_highlighter(text) @keys ||= load_keywords("#{@@keywordsfile}") if @keys == false @keys = {0 => "Ln:", 1 => "SELECT", 2 => "CREATE TABLE", 3 => "UPDATE", 4 => "DELETE", 5 => "INSERT"} end cpicker = [2,3,4,1,7,5,6] # Just a selection of colours @keys.each {|n| if n[1].split("##")[1] == nil text.gsub!("#{n[1]}", "\e[4;3#{cpicker[rand(cpicker.size)]}m#{n[1]}\e[0m\ \e[0;32m") else name = n[1].split("##")[1].split("=")[1] puts "Name: #{name}" if @@debug == true cnum = COLOURS[name].to_i puts "Colour Number: #{cnum}" if @@debug == true nval = n[1].split("##")[0] puts "Value: #{nval}" if @@debug == true text.gsub!("#{nval}", "\e[4;3#{cnum}m#{nval}\e[0m\ \e[0;32m") end text.gsub!(" \e[0;32m", "\e[0;32m") } return text end |
#text_processor(data) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/icersplicer.rb', line 56 def text_processor(data) unless instance_variable_defined?("@nohighlighter") data = text_highlighter(data) return data else return data end end |
#writefile(data) ⇒ Object
154 155 156 |
# File 'lib/icersplicer.rb', line 154 def writefile(data) @@exp.write(data) end |