pru 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 819c391f98a1c8d622a34b5e95e7eb6d1770da78
4
- data.tar.gz: 65e0fa9c0f8a8651d903b01ecb35a87173fa6115
3
+ metadata.gz: d7ab83319d415eed24a2650913be76d27c0652a1
4
+ data.tar.gz: 69d39153bc5388450fb4125e6d66513740e81273
5
5
  SHA512:
6
- metadata.gz: 283aea062a69167b5f611bd68d936c4ba174a21fea05a5103e72096f4cd5726d075a18175ba9b260aa4afa0c759f5a7cab64c7e08b67dd87abf797f46ac2ff80
7
- data.tar.gz: eb42663ead626b3108ec357a8d0fe26f49acd22b0cc07f0994f3cb47ce72b14c9db4cbe23fa6ece2a81ca0e7c0126dfc8643101a2ab57c37c2f73b53b1a5d97f
6
+ metadata.gz: f21fbb6b7a9925e65176555fe73b732f9708c32dbc202d83555542b710f14ef92bbc6543edb1f0d0b1f21fc38b4f2dbea05c1189a85353e3a49fc0bd74b70d00
7
+ data.tar.gz: 8c10b8fdd4a47e024c603729b6d5b6b260f0402c4c71e1e0c7fa2f0182c68ca50f4adfee06f389d4bdcfc4b3da2f9d937897b00bf67831ec83bc0b581c528133
data/bin/pru CHANGED
@@ -1,39 +1,36 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'optparse'
3
3
 
4
- $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
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 = <<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
- something | pru 'map'
21
- something | pru 'map' 'reduce'
22
- something | pru '' 'reduce'
23
- something | pru --reduce 'reduce'
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 }
@@ -41,7 +38,7 @@ BANNER
41
38
  usage = opts
42
39
  end.parse!
43
40
 
44
- if ARGV.empty? and options.empty? # no arguments -> show usage
41
+ if ARGV.empty? && options.empty? # no arguments -> show usage
45
42
  puts usage
46
43
  exit
47
44
  end
@@ -50,28 +47,31 @@ 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 not map or map.empty?
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{|line|
61
+ collector = lambda do |line|
64
62
  output_lines ? output_lines << line : puts(line)
65
- }
63
+ end
66
64
 
67
65
  if reduce
68
66
  results = []
69
- Pru.map(input, map){|out| results << out }
67
+ Pru.map(input, map) { |out| results << out }
70
68
  collector.call Pru.reduce(results, reduce)
71
69
  else
72
- Pru.map(input, map){|out| collector.call out }
70
+ Pru.map(input, map) { |out| collector.call out }
73
71
  end
74
72
 
75
73
  if options[:file]
76
- File.open(options[:file], 'w'){|f| f.write output_lines.join(newline) }
74
+ content = output_lines.join(newline)
75
+ content << newline if trailing_newline
76
+ File.write options[:file], content
77
77
  end
data/lib/pru.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'pru/core_ext/array'
2
- require 'pru/core_ext/symbol'
3
2
 
4
3
  module Pru
5
4
  class << self
@@ -22,10 +22,4 @@ class Array
22
22
  def counted
23
23
  grouped.sort_by{|d, f| -1 * f.size }.map{|d, f| "#{d} : #{f.size}" }
24
24
  end unless method_defined?(:counted)
25
-
26
- def group_by
27
- hash = {}
28
- each { |x| hash[yield(x)] = x }
29
- hash
30
- end unless method_defined?(:group_by)
31
25
  end
@@ -1,3 +1,3 @@
1
1
  module Pru
2
- VERSION = "0.1.8"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-18 00:00:00.000000000 Z
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: michael@grosser.it
@@ -20,7 +20,6 @@ files:
20
20
  - bin/pru
21
21
  - lib/pru.rb
22
22
  - lib/pru/core_ext/array.rb
23
- - lib/pru/core_ext/symbol.rb
24
23
  - lib/pru/version.rb
25
24
  homepage: https://github.com/grosser/pru
26
25
  licenses:
@@ -34,7 +33,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
34
33
  requirements:
35
34
  - - ">="
36
35
  - !ruby/object:Gem::Version
37
- version: '0'
36
+ version: 2.0.0
38
37
  required_rubygems_version: !ruby/object:Gem::Requirement
39
38
  requirements:
40
39
  - - ">="
@@ -42,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
41
  version: '0'
43
42
  requirements: []
44
43
  rubyforge_project:
45
- rubygems_version: 2.2.2
44
+ rubygems_version: 2.5.1
46
45
  signing_key:
47
46
  specification_version: 4
48
47
  summary: Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable
@@ -1,5 +0,0 @@
1
- class Symbol
2
- def to_proc
3
- proc { |obj, *args| obj.send(self, *args) }
4
- end unless method_defined?(:to_proc)
5
- end