pru 0.1.6 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/pru +37 -29
- data/lib/pru/core_ext/array.rb +5 -7
- data/lib/pru/version.rb +3 -0
- data/lib/pru.rb +25 -26
- metadata +26 -60
- data/Gemfile +0 -7
- data/Gemfile.lock +0 -26
- data/Rakefile +0 -18
- data/Readme.md +0 -89
- data/VERSION +0 -1
- data/lib/pru/core_ext/symbol.rb +0 -5
- data/pru.gemspec +0 -51
- data/spec/a_test.rb +0 -2
- data/spec/pru_spec.rb +0 -119
- data/spec/spec_helper.rb +0 -2
- data/spec/test.txt +0 -5
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5e3aadfba1442b50864edd1817ede514cf9592cd5cda84df67969958160d4053
|
4
|
+
data.tar.gz: 56047bf61575504fc0c3c489b084c3537ca715d20498c2571d01df92585fe684
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0995c519eaee3cf6b9fd41fa21578b3b4138214d2a5cbc6848fa03d37f9ad0328f16bef8dfbee6c274744408708d7ac47eeb545b5c228f89cf41cc2c42336de0'
|
7
|
+
data.tar.gz: fd7de3b48e9291ab01ad4478b65bc76a02f97841f4d84aa93ae624a600e2c188f921f32e5f2fc90e10cd76579f7fb2434771a9fb586fdebf9fe96dab29c28513
|
data/bin/pru
CHANGED
@@ -1,47 +1,44 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'optparse'
|
3
3
|
|
4
|
-
|
4
|
+
# enable local usage from cloned repo
|
5
|
+
root = File.expand_path("../..", __FILE__)
|
6
|
+
$LOAD_PATH << "#{root}/lib" if File.exist?("#{root}/Gemfile")
|
7
|
+
|
5
8
|
require 'pru'
|
6
9
|
|
7
10
|
usage = nil
|
8
11
|
options = {}
|
9
12
|
|
10
13
|
OptionParser.new do |opts|
|
11
|
-
opts.banner =
|
12
|
-
Pipeable Ruby
|
14
|
+
opts.banner = <<-BANNER.gsub(/^ /, "")
|
15
|
+
Pipeable Ruby
|
13
16
|
|
14
|
-
Use ruby in your pipes, forget about grep / sed / awk / wc ...
|
17
|
+
Use ruby in your pipes, forget about grep / sed / awk / wc ...
|
15
18
|
|
16
|
-
Map works on each line as String
|
17
|
-
Reduce works on all lines as Array (optional or via -r)
|
19
|
+
Map works on each line as String
|
20
|
+
Reduce works on all lines as Array (optional or via -r)
|
18
21
|
|
19
|
-
Usage:
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
Usage:
|
23
|
+
something | pru 'map'
|
24
|
+
something | pru 'map' 'reduce'
|
25
|
+
something | pru '' 'reduce'
|
26
|
+
something | pru --reduce 'reduce'
|
24
27
|
|
25
|
-
Options:
|
26
|
-
BANNER
|
28
|
+
Options:
|
29
|
+
BANNER
|
27
30
|
opts.on("-r", "--reduce CODE","reduce via CODE") {|code| options[:reduce] = code }
|
28
31
|
opts.separator ''
|
29
32
|
opts.on('-I', '--libdir DIR', 'Add DIR to load path') { |dir| $LOAD_PATH << dir }
|
30
|
-
opts.on('--require LIB', 'Require LIB (also comma-separated)') { |lib|
|
31
|
-
begin
|
32
|
-
require 'rubygems'
|
33
|
-
rescue LoadError
|
34
|
-
end
|
35
|
-
lib.split(',').each{|l| require l }
|
36
|
-
}
|
33
|
+
opts.on('--require LIB', 'Require LIB (also comma-separated)') { |lib| lib.split(',').each{|l| require l } }
|
37
34
|
opts.on('-i', '--inplace-edit FILE', 'Edit FILE inplace') { |file| options[:file] = file }
|
38
35
|
opts.separator ''
|
39
36
|
opts.on("-h", "--help","Show this.") { puts opts; exit }
|
40
|
-
opts.on('-v', '--version','Show Version'){ puts Pru::VERSION; exit}
|
37
|
+
opts.on('-v', '--version','Show Version'){ require 'pru/version'; puts Pru::VERSION; exit}
|
41
38
|
usage = opts
|
42
39
|
end.parse!
|
43
40
|
|
44
|
-
if ARGV.empty?
|
41
|
+
if ARGV.empty? && options.empty? # no arguments -> show usage
|
45
42
|
puts usage
|
46
43
|
exit
|
47
44
|
end
|
@@ -50,28 +47,39 @@ abort "Too many arguments, see --help" if ARGV.size > 2
|
|
50
47
|
|
51
48
|
map, reduce = ARGV
|
52
49
|
reduce ||= options[:reduce]
|
53
|
-
map = 'true' if
|
50
|
+
map = 'true' if !map || map.empty?
|
54
51
|
|
55
52
|
if options[:file]
|
56
53
|
output_lines = []
|
57
54
|
input = File.read(options[:file])
|
58
55
|
newline = input[/\r\n|\r|\n/]
|
56
|
+
trailing_newline = (input =~ /#{newline}\Z/)
|
59
57
|
else
|
60
58
|
input = $stdin
|
61
59
|
end
|
62
60
|
|
63
|
-
collector = lambda
|
64
|
-
|
65
|
-
|
61
|
+
collector = lambda do |line|
|
62
|
+
if output_lines
|
63
|
+
output_lines << line
|
64
|
+
else
|
65
|
+
begin
|
66
|
+
puts(line)
|
67
|
+
rescue Errno::EPIPE
|
68
|
+
exit 0
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
66
72
|
|
67
73
|
if reduce
|
68
74
|
results = []
|
69
|
-
Pru.map(input, map){|out| results << out }
|
75
|
+
Pru.map(input, map) { |out| results << out }
|
70
76
|
collector.call Pru.reduce(results, reduce)
|
71
77
|
else
|
72
|
-
Pru.map(input, map){|out| collector.call out }
|
78
|
+
Pru.map(input, map) { |out| collector.call out }
|
73
79
|
end
|
74
80
|
|
75
81
|
if options[:file]
|
76
|
-
|
82
|
+
content = output_lines.join(newline)
|
83
|
+
content << newline if trailing_newline
|
84
|
+
File.write options[:file], content
|
77
85
|
end
|
data/lib/pru/core_ext/array.rb
CHANGED
@@ -11,17 +11,15 @@ class Array
|
|
11
11
|
end
|
12
12
|
end unless method_defined?(:sum)
|
13
13
|
|
14
|
-
def mean(
|
15
|
-
sum(
|
14
|
+
def mean(*args, &block)
|
15
|
+
sum(*args, &block) / size.to_f
|
16
16
|
end unless method_defined?(:mean)
|
17
17
|
|
18
18
|
def grouped
|
19
19
|
group_by { |x| x }
|
20
20
|
end unless method_defined?(:grouped)
|
21
21
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
hash
|
26
|
-
end unless method_defined?(:group_by)
|
22
|
+
def counted
|
23
|
+
grouped.sort_by{|d, f| -1 * f.size }.map{|d, f| "#{d} : #{f.size}" }
|
24
|
+
end unless method_defined?(:counted)
|
27
25
|
end
|
data/lib/pru/version.rb
ADDED
data/lib/pru.rb
CHANGED
@@ -1,36 +1,35 @@
|
|
1
1
|
require 'pru/core_ext/array'
|
2
|
-
require 'pru/core_ext/symbol'
|
3
2
|
|
4
3
|
module Pru
|
5
|
-
|
4
|
+
class << self
|
5
|
+
def map(io, code)
|
6
|
+
String.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
7
|
+
def _pru(i)
|
8
|
+
#{code}
|
9
|
+
end
|
10
|
+
RUBY
|
6
11
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
RUBY
|
13
|
-
|
14
|
-
i = 0
|
15
|
-
io.each_line do |line|
|
16
|
-
i += 1
|
17
|
-
line.chomp!
|
18
|
-
result = line._pru(i) or next
|
12
|
+
i = 0
|
13
|
+
io.each_line do |line|
|
14
|
+
i += 1
|
15
|
+
line.chomp!
|
16
|
+
result = line._pru(i) or next
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
case result
|
19
|
+
when true then yield line
|
20
|
+
when Regexp then yield line if line =~ result
|
21
|
+
else yield result
|
22
|
+
end
|
24
23
|
end
|
25
24
|
end
|
26
|
-
end
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
26
|
+
def reduce(array, code)
|
27
|
+
Array.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
28
|
+
def _pru
|
29
|
+
#{code}
|
30
|
+
end
|
31
|
+
RUBY
|
32
|
+
array._pru
|
33
|
+
end
|
35
34
|
end
|
36
35
|
end
|
metadata
CHANGED
@@ -1,82 +1,48 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: pru
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 6
|
10
|
-
version: 0.1.6
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Michael Grosser
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-08-27 00:00:00 +02:00
|
19
|
-
default_executable: pru
|
11
|
+
date: 2021-11-18 00:00:00.000000000 Z
|
20
12
|
dependencies: []
|
21
|
-
|
22
13
|
description:
|
23
14
|
email: michael@grosser.it
|
24
|
-
executables:
|
15
|
+
executables:
|
25
16
|
- pru
|
26
17
|
extensions: []
|
27
|
-
|
28
18
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
31
|
-
- Gemfile
|
32
|
-
- Gemfile.lock
|
33
|
-
- Rakefile
|
34
|
-
- Readme.md
|
35
|
-
- VERSION
|
19
|
+
files:
|
36
20
|
- bin/pru
|
37
21
|
- lib/pru.rb
|
38
22
|
- lib/pru/core_ext/array.rb
|
39
|
-
- lib/pru/
|
40
|
-
|
41
|
-
|
42
|
-
-
|
43
|
-
|
44
|
-
- spec/test.txt
|
45
|
-
has_rdoc: true
|
46
|
-
homepage: http://github.com/grosser/pru
|
47
|
-
licenses: []
|
48
|
-
|
23
|
+
- lib/pru/version.rb
|
24
|
+
homepage: https://github.com/grosser/pru
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
49
28
|
post_install_message:
|
50
29
|
rdoc_options: []
|
51
|
-
|
52
|
-
require_paths:
|
30
|
+
require_paths:
|
53
31
|
- lib
|
54
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
-
|
56
|
-
requirements:
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
57
34
|
- - ">="
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
version: "0"
|
63
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
-
none: false
|
65
|
-
requirements:
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '2.5'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
66
39
|
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
|
69
|
-
segments:
|
70
|
-
- 0
|
71
|
-
version: "0"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
72
42
|
requirements: []
|
73
|
-
|
74
|
-
rubyforge_project:
|
75
|
-
rubygems_version: 1.6.2
|
43
|
+
rubygems_version: 3.2.16
|
76
44
|
signing_key:
|
77
|
-
specification_version:
|
78
|
-
summary: Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable
|
79
|
-
|
80
|
-
|
81
|
-
- spec/pru_spec.rb
|
82
|
-
- spec/spec_helper.rb
|
45
|
+
specification_version: 4
|
46
|
+
summary: Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable
|
47
|
+
Ruby!
|
48
|
+
test_files: []
|
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
diff-lcs (1.1.2)
|
5
|
-
git (1.2.5)
|
6
|
-
jeweler (1.5.2)
|
7
|
-
bundler (~> 1.0.0)
|
8
|
-
git (>= 1.2.5)
|
9
|
-
rake
|
10
|
-
rake (0.8.7)
|
11
|
-
rspec (2.4.0)
|
12
|
-
rspec-core (~> 2.4.0)
|
13
|
-
rspec-expectations (~> 2.4.0)
|
14
|
-
rspec-mocks (~> 2.4.0)
|
15
|
-
rspec-core (2.4.0)
|
16
|
-
rspec-expectations (2.4.0)
|
17
|
-
diff-lcs (~> 1.1.2)
|
18
|
-
rspec-mocks (2.4.0)
|
19
|
-
|
20
|
-
PLATFORMS
|
21
|
-
ruby
|
22
|
-
|
23
|
-
DEPENDENCIES
|
24
|
-
jeweler
|
25
|
-
rake
|
26
|
-
rspec (~> 2)
|
data/Rakefile
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
task :default do
|
2
|
-
sh "rspec spec/"
|
3
|
-
end
|
4
|
-
|
5
|
-
begin
|
6
|
-
require 'jeweler'
|
7
|
-
Jeweler::Tasks.new do |gem|
|
8
|
-
gem.name = 'pru'
|
9
|
-
gem.summary = "Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!"
|
10
|
-
gem.email = "michael@grosser.it"
|
11
|
-
gem.homepage = "http://github.com/grosser/#{gem.name}"
|
12
|
-
gem.authors = ["Michael Grosser"]
|
13
|
-
end
|
14
|
-
|
15
|
-
Jeweler::GemcutterTasks.new
|
16
|
-
rescue LoadError
|
17
|
-
puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
|
18
|
-
end
|
data/Readme.md
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!
|
2
|
-
|
3
|
-
Install
|
4
|
-
=======
|
5
|
-
sudo gem install pru
|
6
|
-
|
7
|
-
Working with rvm / many gemsets -> only install once
|
8
|
-
|
9
|
-
rvm 1.9.2 exec gem install pru
|
10
|
-
echo 'alias pru="rvm 1.9.2 exec pru"' >> ~/.bash_profile
|
11
|
-
|
12
|
-
Usage
|
13
|
-
=====
|
14
|
-
pru supports mapping and reducing.<br/><br/>
|
15
|
-
Map works on each line as String<br/>
|
16
|
-
Reduce works on all lines as Array<br/>
|
17
|
-
|
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
|
-
-i, --inplace-edit FILE Edit FILE inplace
|
28
|
-
|
29
|
-
-h, --help Show this.
|
30
|
-
-v, --version Show Version
|
31
|
-
|
32
|
-
### Examples
|
33
|
-
|
34
|
-
# grep --- all lines including foo
|
35
|
-
ls -al | grep foo
|
36
|
-
ls -al | pru /foo/
|
37
|
-
|
38
|
-
# grep --- all lines including current date
|
39
|
-
ls -al | grep $(date +"%Y-%m-%d")
|
40
|
-
ls -al | pru 'include?(Time.now.strftime("%Y-%m-%d"))'
|
41
|
-
|
42
|
-
# grep --- all lines including foo but not self
|
43
|
-
ps -ef | grep foo | grep -v grep
|
44
|
-
ps -ef | pru 'include?("foo") and not include?("pru")'
|
45
|
-
|
46
|
-
# awk --- return second item
|
47
|
-
ls -al | awk '{print $2}'
|
48
|
-
ls -al | pru 'split(" ")[1]'
|
49
|
-
|
50
|
-
# awk --- count and average of all integers on second position
|
51
|
-
ls -al | awk '{ s += $2; } END {print "average" ,int(s/NR);print "count ",int(NR)}'
|
52
|
-
ls -al | pru 'split(" ")[1]' '"average #{mean(&:to_i)}\ncount #{size}"'
|
53
|
-
|
54
|
-
# wc --- count lines
|
55
|
-
ls -al | wc -l
|
56
|
-
ls -al | pru --reduce 'size'
|
57
|
-
|
58
|
-
# sed -- replace a 5 with five
|
59
|
-
ls -al | sed 's/5/five/'
|
60
|
-
ls -al | pru 'gsub(/5/,"five")'
|
61
|
-
|
62
|
-
# every second line
|
63
|
-
ls -al | pru 'i % 2 == 0'
|
64
|
-
|
65
|
-
# paste-friendly mime-types
|
66
|
-
curl https://github.com/mattetti/mimetype-fu/raw/master/lib/mime_types.yml | grep image | pru 'gsub(/(.*): (.*)/, %{"\\1" => "\\2",})'
|
67
|
-
|
68
|
-
# number of files by date:
|
69
|
-
ls -al | pru 'split(" ")[5]' 'grouped.map{|d, f| "#{d} : #{f.size}" }'
|
70
|
-
|
71
|
-
# quotes inside a string
|
72
|
-
something | pru 'include?(%{"string"})'
|
73
|
-
|
74
|
-
# Find a gem version matching a requirement e.g. ~> 1.0.0
|
75
|
-
curl http://rubygems.org/api/v1/versions/bundler | pru --require json 'JSON.parse(self).map{|g|g["number"]}.find{|v| Gem::Requirement.new("~>1.0.1").satisfied_by? Gem::Version.new(v) }'
|
76
|
-
|
77
|
-
# Cleanup strange whitespace in a file
|
78
|
-
pru -i Rakefile 'gsub(/\r\n/,"\n").gsub(/\t/," ")'
|
79
|
-
|
80
|
-
Authors
|
81
|
-
=======
|
82
|
-
### [Contributors](http://github.com/grosser/pru/contributors)
|
83
|
-
- [John Hobbs](http://github.com/jmhobbs)
|
84
|
-
- [Vasiliy Ermolovich](http://github.com/nashby)
|
85
|
-
- [Jens Wille](http://blackwinter.de)
|
86
|
-
|
87
|
-
[Michael Grosser](http://grosser.it)<br/>
|
88
|
-
michael@grosser.it<br/>
|
89
|
-
Hereby placed under public domain, do what you want, just do not hold me accountable...
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.6
|
data/lib/pru/core_ext/symbol.rb
DELETED
data/pru.gemspec
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{pru}
|
8
|
-
s.version = "0.1.6"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Michael Grosser"]
|
12
|
-
s.date = %q{2011-08-27}
|
13
|
-
s.default_executable = %q{pru}
|
14
|
-
s.email = %q{michael@grosser.it}
|
15
|
-
s.executables = ["pru"]
|
16
|
-
s.files = [
|
17
|
-
"Gemfile",
|
18
|
-
"Gemfile.lock",
|
19
|
-
"Rakefile",
|
20
|
-
"Readme.md",
|
21
|
-
"VERSION",
|
22
|
-
"bin/pru",
|
23
|
-
"lib/pru.rb",
|
24
|
-
"lib/pru/core_ext/array.rb",
|
25
|
-
"lib/pru/core_ext/symbol.rb",
|
26
|
-
"pru.gemspec",
|
27
|
-
"spec/a_test.rb",
|
28
|
-
"spec/pru_spec.rb",
|
29
|
-
"spec/spec_helper.rb",
|
30
|
-
"spec/test.txt"
|
31
|
-
]
|
32
|
-
s.homepage = %q{http://github.com/grosser/pru}
|
33
|
-
s.require_paths = ["lib"]
|
34
|
-
s.rubygems_version = %q{1.6.2}
|
35
|
-
s.summary = %q{Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!}
|
36
|
-
s.test_files = [
|
37
|
-
"spec/a_test.rb",
|
38
|
-
"spec/pru_spec.rb",
|
39
|
-
"spec/spec_helper.rb"
|
40
|
-
]
|
41
|
-
|
42
|
-
if s.respond_to? :specification_version then
|
43
|
-
s.specification_version = 3
|
44
|
-
|
45
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
-
else
|
47
|
-
end
|
48
|
-
else
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
data/spec/a_test.rb
DELETED
data/spec/pru_spec.rb
DELETED
@@ -1,119 +0,0 @@
|
|
1
|
-
require File.expand_path('spec/spec_helper')
|
2
|
-
|
3
|
-
describe Pru do
|
4
|
-
it "has a VERSION" do
|
5
|
-
Pru::VERSION.should =~ /^\d+\.\d+\.\d+$/
|
6
|
-
end
|
7
|
-
|
8
|
-
it "shows help when no arguments are given" do
|
9
|
-
`./bin/pru`.should include('Usage:')
|
10
|
-
end
|
11
|
-
|
12
|
-
describe 'map' do
|
13
|
-
it "selects" do
|
14
|
-
`ls -l | ./bin/pru 'include?("G")'`.split("\n").size.should == 2
|
15
|
-
end
|
16
|
-
|
17
|
-
it "can selects via regex" do
|
18
|
-
`ls -l | ./bin/pru /G/`.split("\n").size.should == 2
|
19
|
-
end
|
20
|
-
|
21
|
-
it "can selects via i" do
|
22
|
-
`cat spec/test.txt | ./bin/pru 'i'`.split("\n")[0...3].should == ["1","2","3"]
|
23
|
-
end
|
24
|
-
|
25
|
-
it "maps" do
|
26
|
-
`echo abc | ./bin/pru 'gsub(/a/,"b")'`.should == "bbc\n"
|
27
|
-
end
|
28
|
-
|
29
|
-
it "selects and reduces" do
|
30
|
-
`cat spec/test.txt | ./bin/pru 'include?("abc")' 'size'`.should == "3\n"
|
31
|
-
end
|
32
|
-
|
33
|
-
it "can open files" do
|
34
|
-
`echo spec/test.txt | ./bin/pru 'File.read(self)'`.should == File.read('spec/test.txt')
|
35
|
-
end
|
36
|
-
|
37
|
-
it "preserves whitespaces" do
|
38
|
-
`echo ' ab\tcd ' | ./bin/pru 'self'`.should == " ab\tcd \n"
|
39
|
-
end
|
40
|
-
|
41
|
-
it "works with continuous input" do
|
42
|
-
results = `ruby -e 'STDOUT.sync = true; puts 1; sleep 2; puts 1' | ./bin/pru 'Time.now.to_i'`.split("\n")
|
43
|
-
results.size.should == 2
|
44
|
-
results.uniq.size.should == 2 # called at a different time -> parses as you go
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe 'reduce' do
|
49
|
-
it "reduces" do
|
50
|
-
`cat spec/test.txt | ./bin/pru -r 'size'`.should == "5\n"
|
51
|
-
end
|
52
|
-
|
53
|
-
it "prints arrays as newlines" do
|
54
|
-
`cat spec/test.txt | ./bin/pru -r 'self'`.should == File.read('spec/test.txt')
|
55
|
-
end
|
56
|
-
|
57
|
-
it "can sum" do
|
58
|
-
`cat spec/test.txt | ./bin/pru -r 'sum(&:to_i)'`.should == "1212\n"
|
59
|
-
end
|
60
|
-
|
61
|
-
it "can mean" do
|
62
|
-
`cat spec/test.txt | ./bin/pru -r 'mean(&:to_i)'`.should == "242.4\n"
|
63
|
-
end
|
64
|
-
|
65
|
-
it "can grouped" do
|
66
|
-
`cat spec/test.txt | ./bin/pru -r 'grouped.map{|a,b| b.size }'`.should include("2\n")
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe 'map and reduce' do
|
71
|
-
it "selects with empty string and reduces" do
|
72
|
-
`cat spec/test.txt | ./bin/pru '' 'size'`.should == "5\n"
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe '-I / --libdir' do
|
77
|
-
it "adds a folder to the load-path" do
|
78
|
-
`echo 1 | ./bin/pru -I spec --reduce 'require "a_test"; ATest.to_s'`.should == "ATest\n"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe '--require' do
|
83
|
-
it "requires these libs" do
|
84
|
-
`echo 1 | ./bin/pru --require rake --reduce 'Rake.to_s'`.should == "Rake\n"
|
85
|
-
end
|
86
|
-
|
87
|
-
it "requires these libs comma-separated" do
|
88
|
-
`echo 1 | ./bin/pru --require jeweler,rake --reduce 'Rake.to_s + Jeweler.to_s'`.should == "RakeJeweler\n"
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe '--inplace-edit FILE' do
|
93
|
-
after do
|
94
|
-
`rm -f xxx`
|
95
|
-
end
|
96
|
-
|
97
|
-
it "modifies the file" do
|
98
|
-
File.open('xxx','w'){|f| f.write "abc\nab\na" }
|
99
|
-
`./bin/pru --inplace-edit xxx size`.should == ''
|
100
|
-
File.read('xxx').should == "3\n2\n1"
|
101
|
-
end
|
102
|
-
|
103
|
-
it "fails with empty file" do
|
104
|
-
`./bin/pru --inplace-edit xxx size 2>&1`.should include('No such file or directory - xxx')
|
105
|
-
end
|
106
|
-
|
107
|
-
it "keeps line separators when modifying" do
|
108
|
-
File.open('xxx','w'){|f| f.write "abc\r\nab\r\na" }
|
109
|
-
`./bin/pru --inplace-edit xxx size`.should == ''
|
110
|
-
File.read('xxx').should == "3\r\n2\r\n1"
|
111
|
-
end
|
112
|
-
|
113
|
-
it "modifies the file with reduce" do
|
114
|
-
File.open('xxx','w'){|f| f.write "abc\nab\na" }
|
115
|
-
`./bin/pru --inplace-edit xxx size inspect`.should == ''
|
116
|
-
File.read('xxx').should == "[3, 2, 1]"
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
data/spec/spec_helper.rb
DELETED