pru 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +14 -3
- data/VERSION +1 -1
- data/bin/pru +20 -10
- data/lib/pru/{core_ext.rb → core_ext/array.rb} +12 -6
- data/lib/pru/core_ext/symbol.rb +5 -0
- data/lib/pru.rb +10 -9
- data/pru.gemspec +6 -3
- data/spec/a_test.rb +2 -0
- data/spec/pru_spec.rb +17 -1
- metadata +8 -5
data/Readme.md
CHANGED
@@ -15,8 +15,18 @@ pru supports mapping and reducing.<br/><br/>
|
|
15
15
|
Map works on each line as String<br/>
|
16
16
|
Reduce works on all lines as Array<br/>
|
17
17
|
|
18
|
-
something | pru 'map'
|
19
|
-
something | pru
|
18
|
+
something | pru 'map'
|
19
|
+
something | pru 'map' 'reduce'
|
20
|
+
something | pru '' 'reduce'
|
21
|
+
something | pru --reduce 'reduce'
|
22
|
+
|
23
|
+
-r, --reduce CODE reduce via CODE
|
24
|
+
|
25
|
+
-I, --libdir DIR Add DIR to load path
|
26
|
+
--require LIB Require LIB (also comma-separated)
|
27
|
+
|
28
|
+
-h, --help Show this.
|
29
|
+
-v, --version Show Version
|
20
30
|
|
21
31
|
### Examples
|
22
32
|
|
@@ -42,7 +52,7 @@ Reduce works on all lines as Array<br/>
|
|
42
52
|
|
43
53
|
# wc --- count lines
|
44
54
|
ls -al | wc -l
|
45
|
-
ls -al | pru
|
55
|
+
ls -al | pru --reuduce 'size'
|
46
56
|
|
47
57
|
# sed -- replace a 5 with five
|
48
58
|
ls -al | sed 's/5/five/'
|
@@ -65,6 +75,7 @@ Authors
|
|
65
75
|
### [Contributors](http://github.com/grosser/pru/contributors)
|
66
76
|
- [John Hobbs](http://github.com/jmhobbs)
|
67
77
|
- [Vasiliy Ermolovich](http://github.com/nashby)
|
78
|
+
- [Jens Wille](http://blackwinter.de)
|
68
79
|
|
69
80
|
[Michael Grosser](http://grosser.it)<br/>
|
70
81
|
michael@grosser.it<br/>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/bin/pru
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require 'rubygems'
|
3
2
|
require 'optparse'
|
4
3
|
|
5
4
|
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
@@ -16,26 +15,37 @@ Map works on each line as String
|
|
16
15
|
Reduce works on all lines as Array (optional or via -r)
|
17
16
|
|
18
17
|
Usage:
|
19
|
-
something | pru 'map'
|
20
|
-
something | pru
|
18
|
+
something | pru 'map'
|
19
|
+
something | pru 'map' 'reduce'
|
20
|
+
something | pru '' 'reduce'
|
21
|
+
something | pru --reduce 'reduce'
|
21
22
|
|
22
23
|
Options:
|
23
24
|
BANNER
|
24
|
-
opts.on("-r", "--reduce
|
25
|
+
opts.on("-r", "--reduce CODE","reduce via CODE") {|code| options[:reduce] = code }
|
26
|
+
opts.separator ''
|
27
|
+
opts.on('-I', '--libdir DIR', 'Add DIR to load path') { |dir| $LOAD_PATH << dir }
|
28
|
+
opts.on('--require LIB', 'Require LIB (also comma-separated)') { |lib|
|
29
|
+
begin
|
30
|
+
require 'rubygems'
|
31
|
+
rescue LoadError
|
32
|
+
end
|
33
|
+
lib.split(',').each{|l| require l }
|
34
|
+
}
|
35
|
+
opts.separator ''
|
25
36
|
opts.on("-h", "--help","Show this.") { puts opts; exit }
|
26
37
|
opts.on('-v', '--version','Show Version'){ puts Pru::VERSION; exit}
|
27
38
|
end.parse!
|
28
39
|
|
40
|
+
abort "Too many arguments, see --help" if ARGV.size > 2
|
29
41
|
map, reduce = ARGV
|
30
42
|
reduce ||= options[:reduce]
|
31
|
-
map =
|
43
|
+
map = 'true' if not map or map.empty?
|
32
44
|
|
33
|
-
if
|
34
|
-
Pru.map($stdin, map){|x| puts x }
|
35
|
-
elsif map and reduce
|
45
|
+
if reduce
|
36
46
|
results = []
|
37
47
|
Pru.map($stdin, map){|x| results << x }
|
38
48
|
puts Pru.reduce(results, reduce)
|
39
|
-
|
40
|
-
|
49
|
+
else
|
50
|
+
Pru.map($stdin, map){|x| puts x }
|
41
51
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
# http://madeofcode.com/posts/74-ruby-core-extension-array-sum
|
2
1
|
class Array
|
2
|
+
# http://madeofcode.com/posts/74-ruby-core-extension-array-sum
|
3
3
|
def sum(method = nil, &block)
|
4
4
|
if block_given?
|
5
5
|
raise ArgumentError, "You cannot pass a block and a method!" if method
|
@@ -9,13 +9,19 @@ class Array
|
|
9
9
|
else
|
10
10
|
inject(0) { |sum, i| sum + i }
|
11
11
|
end
|
12
|
-
end
|
13
|
-
|
12
|
+
end unless method_defined?(:sum)
|
13
|
+
|
14
14
|
def mean(method = nil, &block)
|
15
15
|
sum(method, &block) / size.to_f
|
16
|
-
end
|
16
|
+
end unless method_defined?(:mean)
|
17
17
|
|
18
18
|
def grouped
|
19
|
-
group_by{|x|x}
|
20
|
-
end
|
19
|
+
group_by { |x| x }
|
20
|
+
end unless method_defined?(:grouped)
|
21
|
+
|
22
|
+
def group_by
|
23
|
+
hash = {}
|
24
|
+
each { |x| hash[yield(x)] = x }
|
25
|
+
hash
|
26
|
+
end unless method_defined?(:group_by)
|
21
27
|
end
|
data/lib/pru.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
require 'pru/core_ext'
|
1
|
+
require 'pru/core_ext/array'
|
2
|
+
require 'pru/core_ext/symbol'
|
2
3
|
|
3
|
-
|
4
|
+
module Pru
|
4
5
|
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
|
5
6
|
|
6
7
|
def self.map(io, code)
|
@@ -13,13 +14,13 @@ class Pru
|
|
13
14
|
i = 0
|
14
15
|
io.each_line do |line|
|
15
16
|
i += 1
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
line.chomp!
|
18
|
+
result = line._pru(i) or next
|
19
|
+
|
20
|
+
case result
|
21
|
+
when true then yield line
|
22
|
+
when Regexp then yield line if line =~ result
|
23
|
+
else yield result
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
data/pru.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{pru}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Michael Grosser"]
|
12
|
-
s.date = %q{2011-04-
|
12
|
+
s.date = %q{2011-04-22}
|
13
13
|
s.default_executable = %q{pru}
|
14
14
|
s.email = %q{michael@grosser.it}
|
15
15
|
s.executables = ["pru"]
|
@@ -21,8 +21,10 @@ Gem::Specification.new do |s|
|
|
21
21
|
"VERSION",
|
22
22
|
"bin/pru",
|
23
23
|
"lib/pru.rb",
|
24
|
-
"lib/pru/core_ext.rb",
|
24
|
+
"lib/pru/core_ext/array.rb",
|
25
|
+
"lib/pru/core_ext/symbol.rb",
|
25
26
|
"pru.gemspec",
|
27
|
+
"spec/a_test.rb",
|
26
28
|
"spec/pru_spec.rb",
|
27
29
|
"spec/spec_helper.rb",
|
28
30
|
"spec/test.txt"
|
@@ -32,6 +34,7 @@ Gem::Specification.new do |s|
|
|
32
34
|
s.rubygems_version = %q{1.4.2}
|
33
35
|
s.summary = %q{Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!}
|
34
36
|
s.test_files = [
|
37
|
+
"spec/a_test.rb",
|
35
38
|
"spec/pru_spec.rb",
|
36
39
|
"spec/spec_helper.rb"
|
37
40
|
]
|
data/spec/a_test.rb
ADDED
data/spec/pru_spec.rb
CHANGED
@@ -30,7 +30,7 @@ describe Pru do
|
|
30
30
|
`echo spec/test.txt | ./bin/pru 'File.read(self)'`.should == File.read('spec/test.txt')
|
31
31
|
end
|
32
32
|
|
33
|
-
it "
|
33
|
+
it "preserves whitespaces" do
|
34
34
|
`echo ' ab\tcd ' | ./bin/pru 'self'`.should == " ab\tcd \n"
|
35
35
|
end
|
36
36
|
|
@@ -68,4 +68,20 @@ describe Pru do
|
|
68
68
|
`cat spec/test.txt | ./bin/pru '' 'size'`.should == "5\n"
|
69
69
|
end
|
70
70
|
end
|
71
|
+
|
72
|
+
describe '-I / --libdir' do
|
73
|
+
it "adds a folder to the load-path" do
|
74
|
+
`echo 1 | ./bin/pru -I spec --reduce 'require "a_test"; ATest.to_s'`.should == "ATest\n"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '--require' do
|
79
|
+
it "requires these libs" do
|
80
|
+
`echo 1 | ./bin/pru --require rake --reduce 'Rake.to_s'`.should == "Rake\n"
|
81
|
+
end
|
82
|
+
|
83
|
+
it "requires these libs comma-separated" do
|
84
|
+
`echo 1 | ./bin/pru --require jeweler,rake --reduce 'Rake.to_s + Jeweler.to_s'`.should == "RakeJeweler\n"
|
85
|
+
end
|
86
|
+
end
|
71
87
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Grosser
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-22 00:00:00 +02:00
|
19
19
|
default_executable: pru
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -35,8 +35,10 @@ files:
|
|
35
35
|
- VERSION
|
36
36
|
- bin/pru
|
37
37
|
- lib/pru.rb
|
38
|
-
- lib/pru/core_ext.rb
|
38
|
+
- lib/pru/core_ext/array.rb
|
39
|
+
- lib/pru/core_ext/symbol.rb
|
39
40
|
- pru.gemspec
|
41
|
+
- spec/a_test.rb
|
40
42
|
- spec/pru_spec.rb
|
41
43
|
- spec/spec_helper.rb
|
42
44
|
- spec/test.txt
|
@@ -75,5 +77,6 @@ signing_key:
|
|
75
77
|
specification_version: 3
|
76
78
|
summary: Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!
|
77
79
|
test_files:
|
80
|
+
- spec/a_test.rb
|
78
81
|
- spec/pru_spec.rb
|
79
82
|
- spec/spec_helper.rb
|