Skip to content

Commit

Permalink
Refactoring bin/pru.
Browse files Browse the repository at this point in the history
Backwards-incompatible changes:
- '-r' changed to '-e'
- map defaults to 'true'
- stop messing with $LOAD_PATH
  • Loading branch information
blackwinter committed Apr 21, 2011
1 parent 3eef096 commit 40216a7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
47 changes: 30 additions & 17 deletions bin/pru
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
#!/usr/bin/env ruby

autoload :Pru, 'pru'

This comment has been minimized.

Copy link
@grosser

grosser Apr 22, 2011

if you use ./bin/pru the installed rubygem would be used (insttead of the one in lib) ?

This comment has been minimized.

Copy link
@blackwinter

blackwinter Apr 22, 2011

Author Member

no, you would use -Ilib for that (see PRU_CMD in spec/spec_helper.rb) or set your RUBYOPT appropriately.

require 'optparse'

$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'pru'

options = {}

OptionParser.new { |opts|
opts.banner = <<-EOS
usage = <<-EOS
Pipeable Ruby
Use ruby in your pipes, forget about grep/sed/awk/wc...
Map works on each line as String.
Reduce works on all lines as Array (optional or via -r).
Reduce works on all lines as Array (optional or via -e).
Usage:
something | pru 'map' ['reduce']
something | pru -r 'reduce'
EOS
something | pru -e 'reduce'
EOS

options = {}

OptionParser.new { |opts|
opts.banner = usage

opts.separator ''
opts.separator 'Options:'

opts.on('-r', '--reduce CODE', 'Reduce via CODE') { |code|
opts.on('-e', '--execute CODE', 'Reduce via CODE') { |code|
options[:reduce] = code
}

opts.separator ''
opts.separator 'Load options:'

opts.on('-I', '--libdir LIBDIR', 'Include LIBDIR in the search path for required modules') { |dir|
$LOAD_PATH << dir
}

opts.on('-r', '--require MODULE', 'Require MODULE before executing any code') { |mod|
require mod
}

opts.separator ''
opts.separator 'Generic options:'

Expand All @@ -42,16 +53,18 @@ Usage:
}
}.parse!

abort usage if ARGV.size > 2
map, reduce = ARGV

map ||= options[:map]
reduce ||= options[:reduce]
map = nil if map and map.empty?

if map and not reduce
Pru.map($stdin, map) { |x| puts x }
elsif map and reduce
map = 'true' if map.nil? || map.strip.empty?

if reduce
results = []
Pru.map($stdin, map) { |x| results << x }
puts Pru.reduce(results, reduce)
elsif reduce
puts Pru.reduce($stdin.read.split($/), reduce)
else
Pru.map($stdin, map) { |x| puts x }
end
28 changes: 14 additions & 14 deletions spec/pru_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@
describe 'map' do

it 'selects' do
%x{ls -l | ./bin/pru 'include?("G")'}.split($/).size.should == 2
%x{ls -l | #{PRU_CMD} 'include?("G")'}.split($/).size.should == 2
end

it 'selects via regex' do
%x{ls -l | ./bin/pru /G/}.split($/).size.should == 2
%x{ls -l | #{PRU_CMD} /G/}.split($/).size.should == 2
end

it 'selects via $.' do
%x{cat spec/test.txt | ./bin/pru '$.'}.split($/)[0...3].should == %w[1 2 3]
%x{cat spec/test.txt | #{PRU_CMD} '$.'}.split($/)[0...3].should == %w[1 2 3]
end

it 'maps' do
%x{echo abc | ./bin/pru 'gsub(/a/, "b")'}.should == "bbc\n"
%x{echo abc | #{PRU_CMD} 'gsub(/a/, "b")'}.should == "bbc\n"
end

it 'selects and reduces' do
%x{cat spec/test.txt | ./bin/pru 'include?("abc")' 'size'}.should == "3\n"
%x{cat spec/test.txt | #{PRU_CMD} 'include?("abc")' 'size'}.should == "3\n"
end

it 'opens files' do
%x{echo spec/test.txt | ./bin/pru 'File.read(self)'}.should == File.read('spec/test.txt')
%x{echo spec/test.txt | #{PRU_CMD} 'File.read(self)'}.should == File.read('spec/test.txt')
end

it 'open preserves whitespaces' do
%x{echo ' ab\tcd ' | ./bin/pru 'self'}.should == " ab\tcd \n"
%x{echo ' ab\tcd ' | #{PRU_CMD} 'self'}.should == " ab\tcd \n"
end

it 'works with continuous input' do
results = %x{ruby -e 'STDOUT.sync = true; puts 1; sleep 2; puts 1' | ./bin/pru 'Time.now.to_i'}.split($/)
results = %x{ruby -e 'STDOUT.sync = true; puts 1; sleep 2; puts 1' | #{PRU_CMD} 'Time.now.to_i'}.split($/)
results.size.should == 2
results.uniq.size.should == 2 # called at a different time -> parses as you go
end
Expand All @@ -47,31 +47,31 @@
describe 'reduce' do

it 'reduces' do
%x{cat spec/test.txt | ./bin/pru -r 'size'}.should == "5\n"
%x{cat spec/test.txt | #{PRU_CMD} -e 'size'}.should == "5\n"
end

it 'prints arrays as newlines' do
%x{cat spec/test.txt | ./bin/pru -r 'self'}.should == File.read('spec/test.txt')
%x{cat spec/test.txt | #{PRU_CMD} -e 'self'}.should == File.read('spec/test.txt')
end

it 'can sum' do
%x{cat spec/test.txt | ./bin/pru -r 'sum(&:to_i)'}.should == "1212\n"
%x{cat spec/test.txt | #{PRU_CMD} -e 'sum(&:to_i)'}.should == "1212\n"
end

it 'can mean' do
%x{cat spec/test.txt | ./bin/pru -r 'mean(&:to_i)'}.should == "242.4\n"
%x{cat spec/test.txt | #{PRU_CMD} -e 'mean(&:to_i)'}.should == "242.4\n"
end

it 'can grouped' do
%x{cat spec/test.txt | ./bin/pru -r 'grouped.map { |a, b| b.size }'}.should include("2\n")
%x{cat spec/test.txt | #{PRU_CMD} -e 'grouped.map { |a, b| b.size }'}.should include("2\n")
end

end

describe 'map and reduce' do

it 'selects with empty string and reduces' do
%x{cat spec/test.txt | ./bin/pru '' 'size'}.should == "5\n"
%x{cat spec/test.txt | #{PRU_CMD} '' 'size'}.should == "5\n"
end

end
Expand Down
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
LIB_DIR = File.expand_path('../../lib', __FILE__)
PRU_CMD = "#{File.join(File.dirname(LIB_DIR), 'bin', 'pru')} -I#{LIB_DIR}"

$LOAD_PATH.unshift LIB_DIR
require 'pru'

0 comments on commit 40216a7

Please sign in to comment.