pru 0.1.4 → 0.1.5

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.
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' ['reduce']
19
- something | pru -r 'reduce'
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 -r 'size'
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.4
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' ['reduce']
20
- something | pru -r 'reduce'
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 S","reduce via") {|s| options[:reduce] = s }
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 = nil if map and map.empty?
43
+ map = 'true' if not map or map.empty?
32
44
 
33
- if map and not reduce
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
- elsif reduce
40
- puts Pru.reduce($stdin.read.split("\n"), reduce)
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
@@ -0,0 +1,5 @@
1
+ class Symbol
2
+ def to_proc
3
+ proc { |obj, *args| obj.send(self, *args) }
4
+ end unless method_defined?(:to_proc)
5
+ 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
- class Pru
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
- result = line[0..-2]._pru(i)
17
- if result == true
18
- yield line
19
- elsif result.is_a?(Regexp)
20
- yield line if line =~ result
21
- elsif result
22
- yield result
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.4"
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-21}
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
@@ -0,0 +1,2 @@
1
+ class ATest
2
+ end
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 "can open preserves whitespaces" do
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: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
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-21 00:00:00 +02:00
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