Class: Samplelines
- Inherits:
-
Object
- Object
- Samplelines
- Defined in:
- lib/samplelines.rb,
lib/samplelines/version.rb
Defined Under Namespace
Classes: MultiFile
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#input ⇒ Object
Returns the value of attribute input.
-
#options ⇒ Object
Returns the value of attribute options.
-
#orig_argv ⇒ Object
Returns the value of attribute orig_argv.
-
#remaining_argv ⇒ Object
Returns the value of attribute remaining_argv.
-
#slop ⇒ Object
Returns the value of attribute slop.
Instance Method Summary collapse
- #create_picker(opts) ⇒ Object
- #create_slop! ⇒ Object
- #execute ⇒ Object
-
#initialize(argv = ARGV) ⇒ Samplelines
constructor
A new instance of Samplelines.
- #parse_options(argv) ⇒ Object
Constructor Details
#initialize(argv = ARGV) ⇒ Samplelines
Returns a new instance of Samplelines.
45 46 47 48 49 50 51 52 |
# File 'lib/samplelines.rb', line 45 def initialize(argv = ARGV) self.orig_argv = argv.dup self.remaining_argv = argv self.slop = self.create_slop! self. = (self.remaining_argv) end |
Instance Attribute Details
#input ⇒ Object
Returns the value of attribute input.
43 44 45 |
# File 'lib/samplelines.rb', line 43 def input @input end |
#options ⇒ Object
Returns the value of attribute options.
43 44 45 |
# File 'lib/samplelines.rb', line 43 def @options end |
#orig_argv ⇒ Object
Returns the value of attribute orig_argv.
43 44 45 |
# File 'lib/samplelines.rb', line 43 def orig_argv @orig_argv end |
#remaining_argv ⇒ Object
Returns the value of attribute remaining_argv.
43 44 45 |
# File 'lib/samplelines.rb', line 43 def remaining_argv @remaining_argv end |
#slop ⇒ Object
Returns the value of attribute slop.
43 44 45 |
# File 'lib/samplelines.rb', line 43 def slop @slop end |
Instance Method Details
#create_picker(opts) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/samplelines.rb', line 89 def create_picker(opts) if opts[:percent] cutoff = opts[:percent].to_i out_of = 100 elsif opts[:'one-in'] cutoff = 1 out_of = opts[:'one-in'].to_i else raise "Samplelines must take either -p or -1" end ->() do rand(out_of) < cutoff end end |
#create_slop! ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/samplelines.rb', line 104 def create_slop! return Slop.new(:strict=>true) do "samplelines [options] [filename(s) and/or STDIN and/or STDERR]" on 'v', 'version', 'print version info' on 'h', 'help', 'print usage information' on 'p', 'percent', 'set % chance of a line being output [trumps use of -1]', :argument=>true on '1', 'one-in', 'compute odds of outputing a line as 1 out of every N', :argument=>true on 'm', 'max', 'return at most this many lines', :argument => true on 't', 'time', 'stop running after N seconds', :argument => true end end |
#execute ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/samplelines.rb', line 55 def execute if [:version] $stderr.puts "Samplelines version #{Samplelines::VERSION}" return end if [:help] or orig_argv.empty? $stderr.puts slop.help return end input = Samplelines::MultiFile.new(self.remaining_argv) picker = create_picker() max = [:max] ? [:max].to_i : nil timer_finished = if [:time] seconds = [:time].to_i start_time = Time.new ->() { Time.new - start_time > seconds} else ->() { false } end total = 0 input.each do |l| if picker.call total += 1 print l end return if max and total == max return if timer_finished.call end end |
#parse_options(argv) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/samplelines.rb', line 117 def (argv) begin self.slop.parse!(argv) rescue Slop::Error => e $stderr.puts "Error: #{e.}" $stderr.puts "Exiting..." $stderr.puts $stderr.puts slop.help exit 1 end return self.slop.to_hash end |