pru 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f1a9fa4b87d124b1a9db81613d4d5766dec96ee8
4
+ data.tar.gz: cd470ff4274abd0142c12181b345aeac6e86341b
5
+ SHA512:
6
+ metadata.gz: 943f2461781ca582827333ee7a03f613b61f3eaf7fed078a91a3301fe420b9ecc429000313eabd1c00b4aef4e354b6e3311dd1df9efdd14e9629bdb0f9469aad
7
+ data.tar.gz: ec749e0976312f4ff1723c529e79c7df46863c27ae4629a52c2456c313beacae9ad10b0c8c51ea7f48daa610bdee6645bbf88ec20bb9988734f37b29207cf4e1
data/lib/pru.rb CHANGED
@@ -2,35 +2,35 @@ require 'pru/core_ext/array'
2
2
  require 'pru/core_ext/symbol'
3
3
 
4
4
  module Pru
5
- VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
5
+ class << self
6
+ def map(io, code)
7
+ String.class_eval <<-RUBY, __FILE__, __LINE__ + 1
8
+ def _pru(i)
9
+ #{code}
10
+ end
11
+ RUBY
6
12
 
7
- def self.map(io, code)
8
- String.class_eval <<-RUBY, __FILE__, __LINE__ + 1
9
- def _pru(i)
10
- #{code}
11
- end
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
13
+ i = 0
14
+ io.each_line do |line|
15
+ i += 1
16
+ line.chomp!
17
+ result = line._pru(i) or next
19
18
 
20
- case result
21
- when true then yield line
22
- when Regexp then yield line if line =~ result
23
- else yield result
19
+ case result
20
+ when true then yield line
21
+ when Regexp then yield line if line =~ result
22
+ else yield result
23
+ end
24
24
  end
25
25
  end
26
- end
27
26
 
28
- def self.reduce(array, code)
29
- Array.class_eval <<-RUBY, __FILE__, __LINE__ + 1
30
- def _pru
31
- #{code}
32
- end
33
- RUBY
34
- array._pru
27
+ def reduce(array, code)
28
+ Array.class_eval <<-RUBY, __FILE__, __LINE__ + 1
29
+ def _pru
30
+ #{code}
31
+ end
32
+ RUBY
33
+ array._pru
34
+ end
35
35
  end
36
36
  end
@@ -19,6 +19,10 @@ class Array
19
19
  group_by { |x| x }
20
20
  end unless method_defined?(:grouped)
21
21
 
22
+ def counted
23
+ grouped.sort_by{|d, f| -1 * f.size }.map{|d, f| "#{d} : #{f.size}" }
24
+ end unless method_defined?(:counted)
25
+
22
26
  def group_by
23
27
  hash = {}
24
28
  each { |x| hash[yield(x)] = x }
@@ -0,0 +1,3 @@
1
+ module Pru
2
+ VERSION = "0.1.7"
3
+ end
metadata CHANGED
@@ -1,82 +1,50 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: pru
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 6
10
- version: 0.1.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
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: 2014-06-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
23
  - lib/pru/core_ext/symbol.rb
40
- - pru.gemspec
41
- - spec/a_test.rb
42
- - spec/pru_spec.rb
43
- - spec/spec_helper.rb
44
- - spec/test.txt
45
- has_rdoc: true
46
- homepage: http://github.com/grosser/pru
47
- licenses: []
48
-
24
+ - lib/pru/version.rb
25
+ homepage: https://github.com/grosser/pru
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
49
29
  post_install_message:
50
30
  rdoc_options: []
51
-
52
- require_paths:
31
+ require_paths:
53
32
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
55
- none: false
56
- requirements:
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
57
35
  - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
63
- required_rubygems_version: !ruby/object:Gem::Requirement
64
- none: false
65
- requirements:
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
66
40
  - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
- version: "0"
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
72
43
  requirements: []
73
-
74
44
  rubyforge_project:
75
- rubygems_version: 1.6.2
45
+ rubygems_version: 2.2.2
76
46
  signing_key:
77
- specification_version: 3
78
- summary: Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!
79
- test_files:
80
- - spec/a_test.rb
81
- - spec/pru_spec.rb
82
- - spec/spec_helper.rb
47
+ specification_version: 4
48
+ summary: Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable
49
+ Ruby!
50
+ test_files: []
data/Gemfile DELETED
@@ -1,7 +0,0 @@
1
- source :rubygems
2
-
3
- group :dev do # not development <-> would add unneeded development dependencies in gemspec
4
- gem 'rake'
5
- gem 'rspec', '~>2'
6
- gem 'jeweler'
7
- end
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/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
@@ -1,2 +0,0 @@
1
- class ATest
2
- end
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
@@ -1,2 +0,0 @@
1
- $LOAD_PATH.unshift 'lib'
2
- require 'pru'
data/spec/test.txt DELETED
@@ -1,5 +0,0 @@
1
- abc
2
- abc
3
- abcdef
4
- 12
5
- 1200