class Partline::Partline

Public Class Methods

new() click to toggle source
# File lib/partline.rb, line 6
def initialize
end

Public Instance Methods

get_input(i = [ARGV[0], ARGV[1]]) click to toggle source
# File lib/partline.rb, line 9
def get_input(i = [ARGV[0], ARGV[1]])
  if i[1].nil?
    input_error("Not enough arguments or delimiter is not surrounded by quotes. ")
  else
    @temp_file  = Tempfile.new('tmp')
    @input_file = i[0]
    @delimiter  = i[1]

    input_to_tempfile
  end
end
input_error(p = "") click to toggle source
# File lib/partline.rb, line 47
def input_error(p = "")
  begin
    error = lambda do |e|
      puts e + "Please enter both a valid file name and character(s) to part on"
    end
    raise
  rescue
    error.call(p)
  end
end
input_to_tempfile() click to toggle source
# File lib/partline.rb, line 21
def input_to_tempfile
  if File.exist?(@input_file)
    @temp_file << IO.read(@input_file)
    @temp_file.rewind

    part_line
  else
    input_error("File does not exist. ")
  end
end
part_line() click to toggle source
# File lib/partline.rb, line 32
def part_line
  File.delete(@input_file)
  File.open(@input_file, "a") do |out|
    @temp_file.read.each_line do |current_line|
      if current_line.match(@delimiter)
        output_line = current_line.partition(@delimiter).first.strip
        out.puts output_line unless output_line.empty?
      else
        out.puts(current_line)
      end
    end
  end
  puts "partline has finished"
end