Permalink
Browse files

more standard structure

  • Loading branch information...
1 parent c1ccd7a commit 2626d324f87dff4747129dfc8ed006d23d195b08 @practicingruby practicingruby committed Oct 16, 2011
Showing with 76 additions and 73 deletions.
  1. +2 −73 lib/rcat.rb
  2. +23 −0 lib/rcat/application.rb
  3. +51 −0 lib/rcat/display.rb
View
@@ -1,73 +1,2 @@
-module RCat
- class Application
- def initialize(argv)
- @params, @files = parse_options(argv)
- @display = Display.new(@params)
- end
-
- def run
- @files.each { |f| @display.render_file(f) }
- end
-
- def parse_options(argv)
- params = {}
- files = OptionParser.new do |parser|
- parser.on("-n") { params[:line_numbering] = :all_lines }
- parser.on("-b") { params[:line_numbering] = :significant_lines }
- parser.on("-s") { params[:squeeze] = true }
- end.parse(argv)
-
- [params, files]
- end
- end
-
- class Display
- def initialize(params)
- @line_numbering = params[:line_numbering]
- @squeeze = params[:squeeze]
- end
-
- def render_file(filename)
- @line_number = 1
-
- File.open(filename) do |f|
- lines = f.lines
- loop { render_line(lines) }
- end
- end
-
- def render_line(lines)
- line = lines.next
-
- current_line_empty = line.chomp.empty?
-
- case @line_numbering
- when :all_lines
- print "#{@line_number.to_s.rjust(6)} #{line}"
- @line_number += 1
- when :significant_lines
- if current_line_empty
- print line
- else
- print "#{@line_number.to_s.rjust(6)} #{line}"
- @line_number += 1
- end
- else
- print line
- @line_number += 1
- end
-
- if @squeeze && current_line_empty
- loop do
- next_line = lines.peek
-
- if next_line.chomp.empty?
- lines.next
- else
- break
- end
- end
- end
- end
- end
-end
+require_relative "rcat/application"
+require_relative "rcat/display"
View
@@ -0,0 +1,23 @@
+module RCat
+ class Application
+ def initialize(argv)
+ @params, @files = parse_options(argv)
+ @display = RCat::Display.new(@params)
+ end
+
+ def run
+ @files.each { |f| @display.render_file(f) }
+ end
+
+ def parse_options(argv)
+ params = {}
+ files = OptionParser.new do |parser|
+ parser.on("-n") { params[:line_numbering] = :all_lines }
+ parser.on("-b") { params[:line_numbering] = :significant_lines }
+ parser.on("-s") { params[:squeeze] = true }
+ end.parse(argv)
+
+ [params, files]
+ end
+ end
+end
View
@@ -0,0 +1,51 @@
+module RCat
+ class Display
+ def initialize(params)
+ @line_numbering = params[:line_numbering]
+ @squeeze = params[:squeeze]
+ end
+
+ def render_file(filename)
+ @line_number = 1
+
+ File.open(filename) do |f|
+ lines = f.lines
+ loop { render_line(lines) }
+ end
+ end
+
+ def render_line(lines)
+ line = lines.next
+
+ current_line_empty = line.chomp.empty?
+
+ case @line_numbering
+ when :all_lines
+ print "#{@line_number.to_s.rjust(6)} #{line}"
+ @line_number += 1
+ when :significant_lines
+ if current_line_empty
+ print line
+ else
+ print "#{@line_number.to_s.rjust(6)} #{line}"
+ @line_number += 1
+ end
+ else
+ print line
+ @line_number += 1
+ end
+
+ if @squeeze && current_line_empty
+ loop do
+ next_line = lines.peek
+
+ if next_line.chomp.empty?
+ lines.next
+ else
+ break
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit 2626d32

Please sign in to comment.