Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

starting to trim down code in the executable

  • Loading branch information...
commit 04bfbb00a05d4a342051c0e4fad495db9fa7c6ff 1 parent e551c0b
@noahd1 authored
Showing with 97 additions and 81 deletions.
  1. +1 −69 bin/oink
  2. +2 −1  lib/oink.rb
  3. +94 −11 lib/oink/cli.rb
View
70 bin/oink
@@ -1,73 +1,5 @@
#!/usr/bin/env ruby
-require 'optparse'
-
require File.dirname(__FILE__) + "/../lib/oink.rb"
-require "oink/cli"
-
-options = { :format => :short_summary, :type => :memory }
-
-opts = OptionParser.new do |opts|
- opts.banner = "Usage: oink [options] files"
-
- opts.on("-t", "--threshold [INTEGER]", Integer,
- "Memory threshold in MB") do |threshold|
- options[:threshold] = threshold
- end
-
- opts.on("-f", "--file filepath", "Output to file") do |filename|
- options[:output_file] = filename
- end
-
- format_list = (OinkForMemory::FORMAT_ALIASES.keys + OinkForMemory::FORMATS).join(',')
- opts.on("--format FORMAT", OinkForMemory::FORMATS, OinkForMemory::FORMAT_ALIASES, "Select format",
- " (#{format_list})") do |format|
- options[:format] = format.to_sym
- end
-
- opts.on("-m", "--memory", "Check for Memory Threshold (default)") do |v|
- options[:type] = :memory
- end
-
- opts.on("-r", "--active-record", "Check for Active Record Threshold") do |v|
- options[:type] = :active_record
- end
-
-end
-
-opts.parse!
-
-if ARGV.empty?
- puts opts
- exit
-end
-
-output = nil
-
-if options[:output_file]
- output = File.open(options[:output_file], 'w')
-else
- output = STDOUT
-end
-
-files = get_file_listing(ARGV)
-
-handles = files.map { |f| File.open(f) }
-
-if options[:type] == :memory
-
- options[:threshold] ||= 75
- options[:threshold] *= 1024
-
- OinkForMemory.new(handles, options[:threshold], :format => options[:format]).print(output)
-
-elsif options[:type] == :active_record
-
- options[:threshold] ||= 500
-
- OinkForActiveRecord.new(handles, options[:threshold], :format => options[:format]).print(output)
-
-end
+Cli.new(ARGV.dup).process
-output.close
-handles.each { |h| h.close }
View
3  lib/oink.rb
@@ -1,4 +1,5 @@
$:.unshift(File.dirname(__FILE__ + '.rb') + '/../lib') unless $:.include?(File.dirname(__FILE__ + '.rb') + '/../lib')
require "oink/oink_for_memory"
-require "oink/oink_for_active_record"
+require "oink/oink_for_active_record"
+require "oink/cli"
View
105 lib/oink/cli.rb
@@ -1,14 +1,97 @@
-def get_file_listing(args)
- listing = []
- args.each do |file|
- unless File.exist?(file)
- raise "Could not find \"#{file}\""
- end
- if File.directory?(file)
- listing += Dir.glob("#{file}/**")
+require 'optparse'
+
+class Cli
+
+ def initialize(args)
+ @args = args
+ end
+
+ def process
+ options = { :format => :short_summary, :type => :memory }
+
+ op = OptionParser.new do |opts|
+ opts.banner = "Usage: oink [options] files"
+
+ opts.on("-t", "--threshold [INTEGER]", Integer,
+ "Memory threshold in MB") do |threshold|
+ options[:threshold] = threshold
+ end
+
+ opts.on("-f", "--file filepath", "Output to file") do |filename|
+ options[:output_file] = filename
+ end
+
+ format_list = (OinkForMemory::FORMAT_ALIASES.keys + OinkForMemory::FORMATS).join(',')
+ opts.on("--format FORMAT", OinkForMemory::FORMATS, OinkForMemory::FORMAT_ALIASES, "Select format",
+ " (#{format_list})") do |format|
+ options[:format] = format.to_sym
+ end
+
+ opts.on("-m", "--memory", "Check for Memory Threshold (default)") do |v|
+ options[:type] = :memory
+ end
+
+ opts.on("-r", "--active-record", "Check for Active Record Threshold") do |v|
+ options[:type] = :active_record
+ end
+
+ end
+
+ op.parse!(@args)
+
+ if @args.empty?
+ puts op
+ exit
+ end
+
+ output = nil
+
+ if options[:output_file]
+ output = File.open(options[:output_file], 'w')
else
- listing << file
+ output = STDOUT
+ end
+
+ files = get_file_listing(@args)
+
+ handles = files.map { |f| File.open(f) }
+
+ if options[:type] == :memory
+
+ options[:threshold] ||= 75
+ options[:threshold] *= 1024
+
+ OinkForMemory.new(handles, options[:threshold], :format => options[:format]).print(output)
+
+ elsif options[:type] == :active_record
+
+ options[:threshold] ||= 500
+
+ OinkForActiveRecord.new(handles, options[:threshold], :format => options[:format]).print(output)
+
+ end
+
+ output.close
+ handles.each { |h| h.close }
+ end
+
+protected
+
+ def get_file_listing(args)
+ listing = []
+ args.each do |file|
+ unless File.exist?(file)
+ raise "Could not find \"#{file}\""
+ end
+ if File.directory?(file)
+ listing += Dir.glob("#{file}/**")
+ else
+ listing << file
+ end
end
+ listing
end
- listing
-end
+
+end
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.