rubyc 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,21 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in rubyc.gemspec
4
4
  gemspec
5
+
6
+ # This part is taken from the thor gem Gemfile file
7
+ platforms :mri_18 do
8
+ gem 'ruby-debug', '>= 0.10.3'
9
+ end
10
+
11
+ platforms :mri_19 do
12
+ gem 'ruby-debug19'
13
+ end
14
+
15
+ group :development do
16
+ gem 'pry'
17
+ gem 'guard'
18
+ gem 'guard-minitest'
19
+ gem 'rb-fsevent', '~> 0.9.1'
20
+ # gem 'growl', '1.0.3'
21
+ # gem 'ruby_gntp', '0.3.4'
22
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest' do
5
+ watch(%r|^spec/(.*)_spec\.rb|)
6
+ watch(%r|^lib/rubyc(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch(%r|^lib/(.*)\.rb|) { |m| "spec/test_spec.rb" }
8
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
9
+ end
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Rubyc [![Build Status](https://secure.travis-ci.org/martinos/rubyc.png?branch=master)](http://travis-ci.org/martinos/rubyc)
2
+ ## Description
3
+ Adds Ruby's power to the command line.
4
+ ## Introduction
5
+ Sometimes we need to process files or streams at the bash prompt for filtering, parsing, calculating etc. Unix offers many tools for doing those actions: grep, sed, awk etc. However, their usage are not easy to remember besause of their cryptic syntax. They also use Unix regexes which are more limited than ruby's ones.
6
+
7
+ The Ruby interpreter offers us many command line options for processing files or pipes. The -p, -n options allows us to process lines one at a time. But their syntaxes are not really easy to remember, since it uses non Rubyish syntax, using $_, gets and print kernel methods.
8
+
9
+ For this reason, I have created Rubyc, which stands for Ruby Command line. Rubyc supports many enumerator methods applied to STDIN. The current line is represented by the "line" variable name or it's shorter alias 'l'.
10
+ ## Installation
11
+ ```
12
+ gem install rubyc
13
+ ```
14
+ ## Examples
15
+ ### Use Case: Upcasing
16
+ Upcase what is comming from stdin.
17
+
18
+ The awk way:
19
+ ``` bash
20
+ $ ls | awk '{print toupper($0)}'
21
+ ```
22
+ The Ruby interpreter with options way:
23
+ ``` bash
24
+ $ ls | ruby -pe '$_ = $_.upcase'
25
+ ```
26
+ The Rubyc way:
27
+ ``` bash
28
+ ls | rubyc map 'line.upcase'
29
+ ```
30
+ ### Use Case: CSV File Processing
31
+ Extract columns 2 and 3 of a csv file that has columns containing commas. Note that, in that case the "cut" shell command does not work.
32
+
33
+ The shell way
34
+ ``` bash
35
+ $ ???
36
+ ```
37
+ The Ruby interpreter with options way:
38
+ ``` bash
39
+ $ cat file1.csv | ruby -pe 'require "csv";csv = CSV.parse_line($_); $_ = [csv[2], csv[4]].to_s + "\n"'
40
+ ```
41
+ The Rubyc way:
42
+ ``` bash
43
+ $ cat file1.csv | rubyc map -r csv 'csv = CSV.parse_line(l); [csv[2], csv[3]]'
44
+ ```
45
+ NOTE: -r is an alias for the --require= option indicates the gems needed for the exectution of the script. Note that for multiple require, gems must me separated with a :.
46
+
47
+ ### Use Case: Colorize Stderr
48
+ The shell way:
49
+ ``` bash
50
+ $ rake 2> >(while read line;do echo -e "\033[31m$line\033[0m";done)
51
+ ```
52
+ The Rubyc way:
53
+ ``` bash
54
+ $ rake 2> >(rubyc map -r colorize 'l.red')
55
+ ```
56
+ ### Use Case: Counting insertion in database tables
57
+ Extract the number of insertions per table in a rails log file.
58
+
59
+ The shell way:
60
+ ``` bash
61
+ $ ???
62
+ ```
63
+ The Rubyc way:
64
+ ``` bash
65
+ $ cat development.log | rubyc count_by 'l =~ /INSERT INTO \"(\w+)\"/; $1'
66
+ ---
67
+ trunk_groups: 14
68
+ ld_call_records: 18
69
+ ld_provider_rates: 102191
70
+ cdrs: 47
71
+ tf_call_records: 18
72
+ ```
73
+
74
+
75
+ Here are the currently supported methods:
76
+ ```
77
+ compact # Remove empty lines
78
+ count_by # Count the number of lines that have the same property. The property is defined by the return value of the given the block.
79
+ grep # Enumerable#grep the first argument is the pattern matcher and the second is the block executed on each line.
80
+ map # Apply Enumerable#map on each line and outputs the results of the block by calling to_s on the returned object.
81
+ merge # Merge consecutive lines
82
+ scan # String#scan
83
+ select # Enumerable#select
84
+ sort_by # Emumerable#sort_by
85
+ sum # Rails Enumerable#sum
86
+ uniq # uniq
87
+ ```
data/lib/rubyc/cli.rb CHANGED
@@ -1,37 +1,8 @@
1
1
  require 'yaml'
2
2
  require 'thor'
3
+ require 'rubyc/core_extensions'
3
4
 
4
5
  module Rubyc
5
- module ::Enumerable
6
- def count_by
7
- self.inject({}) do |memo, elem|
8
- key = yield elem
9
- memo[key] ||= 0
10
- memo[key] += 1
11
- memo
12
- end
13
- end
14
-
15
- # This method was borrowed from ActiveSupport code
16
- def group_by
17
- self.inject({}) do |memo, elem|
18
- key = yield elem
19
- memo[key] ||= []
20
- memo[key] << elem
21
- memo
22
- end
23
- end
24
-
25
- # File activesupport/lib/active_support/core_ext/enumerable.rb, line 57
26
- def sum(identity = 0, &block)
27
- if block_given?
28
- map(&block).sum(identity)
29
- else
30
- inject { |sum, element| sum + element } || identity
31
- end
32
- end
33
- end
34
-
35
6
  class CLI < Thor
36
7
  class_option :require, :aliases => '-r'
37
8
 
@@ -45,9 +16,9 @@ module Rubyc
45
16
  $stdout.sync = true
46
17
  desc :map, "Apply Enumerable#map on each line"
47
18
  def map(code)
48
- proc = eval( "Proc.new{|line| l = line; #{code}}" )
49
- $stdin.each do |line|
50
- puts proc.call(line.chomp).to_s
19
+ proc = eval( "Proc.new{|line,index| l = line; lnum = index + 1;#{code}}" )
20
+ $stdin.each_line.each_with_index do |line, index|
21
+ puts proc.call(line.chomp, index).to_s
51
22
  end
52
23
  end
53
24
 
@@ -0,0 +1,29 @@
1
+ module Enumerable
2
+ def count_by
3
+ self.inject({}) do |memo, elem|
4
+ key = yield elem
5
+ memo[key] ||= 0
6
+ memo[key] += 1
7
+ memo
8
+ end
9
+ end
10
+
11
+ # This method was borrowed from ActiveSupport code
12
+ def group_by
13
+ self.inject({}) do |memo, elem|
14
+ key = yield elem
15
+ memo[key] ||= []
16
+ memo[key] << elem
17
+ memo
18
+ end
19
+ end
20
+
21
+ # File activesupport/lib/active_support/core_ext/enumerable.rb, line 57
22
+ def sum(identity = 0, &block)
23
+ if block_given?
24
+ map(&block).sum(identity)
25
+ else
26
+ inject { |sum, element| sum + element } || identity
27
+ end
28
+ end
29
+ end
data/lib/rubyc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rubyc
2
- VERSION = "0.0.16"
2
+ VERSION = "0.0.17"
3
3
  end
data/lib/rubyc.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  module Rubyc
2
- # Your code goes here...
2
+ autoload :EnumerableExt, 'rubyc/core_extensions'
3
+ autoload :CLI, 'rubyc/core_extensions'
3
4
  end
data/rubyc.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.add_development_dependency "bundler", "~> 1.0"
19
19
  s.add_development_dependency 'minitest'
20
20
  s.add_development_dependency 'aruba'
21
-
21
+ s.add_development_dependency 'colorize'
22
22
  s.files = `git ls-files`.split("\n")
23
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
24
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,78 @@
1
+ ROOT_PATH = File.expand_path('../..', __FILE__)
2
+ require File.expand_path('../spec_helper', __FILE__)
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+ require 'rubyc/cli'
5
+
6
+ include SpecHelper
7
+
8
+ describe "A rubyc cli" do
9
+ before do
10
+ @cli = Rubyc::CLI.new
11
+ end
12
+
13
+ it "should map stdin to stdout" do
14
+ out_str = local_io("first\nsecond") do
15
+ @cli.map('lnum.to_s + " " + l.upcase')
16
+ end
17
+ out_str.must_equal "1 FIRST\n2 SECOND\n"
18
+ end
19
+
20
+ it "should select line from stdin and send it to stdout" do
21
+ out_str = local_io("first\nsecond\nthird") do
22
+ @cli.select('l =~ /third/')
23
+ end
24
+ out_str.must_equal "third\n"
25
+ end
26
+
27
+ it "should sum line from stdin and send it to stdout" do
28
+ out_str = local_io("1\n2\nthird\n4") do
29
+ @cli.sum('l.to_i * 2')
30
+ end
31
+ out_str.must_equal "14.0\n"
32
+ end
33
+
34
+ it "should sort by stdin and send the result to stdout" do
35
+ out_str = local_io("a\nbbb\ncc\ndddd") do
36
+ @cli.sort_by('l.length')
37
+ end
38
+ out_str.must_equal "a\ncc\nbbb\ndddd\n"
39
+ end
40
+
41
+ it "should grep stdin and send the result to stdout" do
42
+ out_str = local_io("bbbb\nbbb\ncc\ndddd") do
43
+ @cli.grep('/^b/', 'l.upcase')
44
+ end
45
+ out_str.must_equal "BBBB\nBBB\n"
46
+ end
47
+
48
+ it "should count_by an algorithm and output to stdout" do
49
+ out_str = local_io("bbbb\nbbb\ncc\ndddd") do
50
+ @cli.count_by('l =~ /^(..)/;$1')
51
+ end
52
+ expected = {"bb" => 2, "cc" => 1, "dd" => 1}
53
+
54
+ YAML.load(out_str).must_equal expected
55
+ end
56
+
57
+ it "should remove empty lines from stdin and output to stdout" do
58
+ out_str = local_io("bbbb\n\ncc\n") do
59
+ @cli.compact
60
+ end
61
+ out_str.must_equal "bbbb\ncc\n"
62
+ end
63
+
64
+ it "should keep unique lines from stdin and output them to stdout" do
65
+ out_str = local_io("1\n2\n2\n3") do
66
+ @cli.uniq
67
+ end
68
+ out_str.must_equal "1\n2\n3\n"
69
+ end
70
+
71
+ it "should merge lines in group of n output them to stdout" do
72
+ out_str = local_io("1\n2\n3\n4\n5\n6\n7\n8") do
73
+ @cli.merge(3, ",")
74
+ end
75
+ out_str.must_equal "1,2,3\n4,5,6\n7,8\n"
76
+ end
77
+ end
78
+
data/spec/rubyc_spec.rb CHANGED
@@ -1,111 +0,0 @@
1
- ROOT_PATH = File.expand_path('../..', __FILE__)
2
- require File.expand_path('../spec_helper', __FILE__)
3
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
- require 'rubyc/cli'
5
-
6
- include SpecHelper
7
- # require 'colorize'
8
- #
9
- # module ColorizeIO
10
- # def puts(str = "")
11
- # red = "\\e[31m"
12
- # blank = "\\e[0m"
13
- # green = "\\e[32m"
14
- # blue = "\\e[34m"
15
- # magenta = "\\e[35m"
16
- # #
17
- # url_regex = %r{(\\S*.rb):(\\d+)(.*)}
18
- # str.each_line do |line|
19
- # if line =~ url_regex
20
- # file_name = $1
21
- # line_number = $2
22
- # complement = $3
23
- # if File.exist? file_name
24
- # full_path = File.expand_path(file_name)
25
- # app_trace = full_path.match(ROOT_PATH) && full_path !~ /vendor/
26
- # new_line = "#{blue}txmt://open?url=file://#{File.dirname(full_path)}/#{red if app_trace }#{File.basename(full_path)}#{blank}&line=#{line_number
27
- # }#{complement}"
28
- # else
29
- # new_line = line
30
- # end
31
- # else
32
- # new_line = line
33
- # end
34
- # super(new_line + "\\n")
35
- # end
36
- # end
37
- # end
38
- #
39
- # MiniTest::Unit.output.extend ColorizeIO
40
-
41
- describe "A rubyc cli" do
42
- before do
43
- @cli = Rubyc::CLI.new
44
- end
45
-
46
- it "should map stdin to stdout" do
47
- out_str = local_io("first\nsecond") do
48
- @cli.map('l.upcase')
49
- end
50
- out_str.must_equal "FIRST\nSECOND\n"
51
- end
52
-
53
- it "should select line from stdin and send it to stdout" do
54
- out_str = local_io("first\nsecond\nthird") do
55
- @cli.select('l =~ /third/')
56
- end
57
- out_str.must_equal "third\n"
58
- end
59
-
60
- it "should sum line from stdin and send it to stdout" do
61
- out_str = local_io("1\n2\nthird\n4") do
62
- @cli.sum('l.to_i * 2')
63
- end
64
- out_str.must_equal "14.0\n"
65
- end
66
-
67
- it "should sort by stdin and send the result to stdout" do
68
- out_str = local_io("a\nbbb\ncc\ndddd") do
69
- @cli.sort_by('l.length')
70
- end
71
- out_str.must_equal "a\ncc\nbbb\ndddd\n"
72
- end
73
-
74
- it "should grep stdin and send the result to stdout" do
75
- out_str = local_io("bbbb\nbbb\ncc\ndddd") do
76
- @cli.grep('/^b/', 'l.upcase')
77
- end
78
- out_str.must_equal "BBBB\nBBB\n"
79
- end
80
-
81
- it "should count_by an algorithm and output to stdout" do
82
- out_str = local_io("bbbb\nbbb\ncc\ndddd") do
83
- @cli.count_by('l =~ /^(..)/;$1')
84
- end
85
- expected = {"bb" => 2, "cc" => 1, "dd" => 1}
86
-
87
- YAML.load(out_str).must_equal expected
88
- end
89
-
90
- it "should remove empty lines from stdin and output to stdout" do
91
- out_str = local_io("bbbb\n\ncc\n") do
92
- @cli.compact
93
- end
94
- out_str.must_equal "bbbb\ncc\n"
95
- end
96
-
97
- it "should keep unique lines from stdin and output them to stdout" do
98
- out_str = local_io("1\n2\n2\n3") do
99
- @cli.uniq
100
- end
101
- out_str.must_equal "1\n2\n3\n"
102
- end
103
-
104
- it "should merge lines in group of n output them to stdout" do
105
- out_str = local_io("1\n2\n3\n4\n5\n6\n7\n8") do
106
- @cli.merge(3, ",")
107
- end
108
- out_str.must_equal "1,2,3\n4,5,6\n7,8\n"
109
- end
110
- end
111
-
data/spec/spec_helper.rb CHANGED
@@ -14,4 +14,39 @@ module SpecHelper
14
14
  $stdin, $stdout = old_stdin, old_stdout
15
15
  out_str
16
16
  end
17
- end
17
+ end
18
+
19
+
20
+ # require 'colorize'
21
+ #
22
+ # module ColorizeIO
23
+ # def puts(str = "")
24
+ # red = "\\e[31m"
25
+ # blank = "\\e[0m"
26
+ # green = "\\e[32m"
27
+ # blue = "\\e[34m"
28
+ # magenta = "\\e[35m"
29
+ # #
30
+ # url_regex = %r{(\\S*.rb):(\\d+)(.*)}
31
+ # str.each_line do |line|
32
+ # if line =~ url_regex
33
+ # file_name = $1
34
+ # line_number = $2
35
+ # complement = $3
36
+ # if File.exist? file_name
37
+ # full_path = File.expand_path(file_name)
38
+ # app_trace = full_path.match(ROOT_PATH) && full_path !~ /vendor/
39
+ # new_line = "#{blue}txmt://open?url=file://#{File.dirname(full_path)}/#{red if app_trace }#{File.basename(full_path)}#{blank}&line=#{line_number
40
+ # }#{complement}"
41
+ # else
42
+ # new_line = line
43
+ # end
44
+ # else
45
+ # new_line = line
46
+ # end
47
+ # super(new_line + "\\n")
48
+ # end
49
+ # end
50
+ # end
51
+ #
52
+ # MiniTest::Unit.output.extend ColorizeIO
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 63
4
+ hash: 61
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 16
10
- version: 0.0.16
9
+ - 17
10
+ version: 0.0.17
11
11
  platform: ruby
12
12
  authors:
13
13
  - Martin Chabot
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-09-04 00:00:00 Z
18
+ date: 2012-09-07 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  prerelease: false
@@ -88,6 +88,20 @@ dependencies:
88
88
  requirement: *id005
89
89
  name: aruba
90
90
  type: :development
91
+ - !ruby/object:Gem::Dependency
92
+ prerelease: false
93
+ version_requirements: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirement: *id006
103
+ name: colorize
104
+ type: :development
91
105
  description: Adds Ruby's powers to the command line
92
106
  email:
93
107
  - chabotm@gmail.com
@@ -100,7 +114,8 @@ extra_rdoc_files: []
100
114
  files:
101
115
  - .gitignore
102
116
  - Gemfile
103
- - README.rdoc
117
+ - Guardfile
118
+ - README.md
104
119
  - Rakefile
105
120
  - bin/rubyc
106
121
  - features/rubyc.feature
@@ -109,8 +124,10 @@ files:
109
124
  - features/support/env.rb
110
125
  - lib/rubyc.rb
111
126
  - lib/rubyc/cli.rb
127
+ - lib/rubyc/core_extensions.rb
112
128
  - lib/rubyc/version.rb
113
129
  - rubyc.gemspec
130
+ - spec/cli_spec.rb
114
131
  - spec/rubyc_spec.rb
115
132
  - spec/spec_helper.rb
116
133
  homepage: https://github.com/martinos/rubyc
@@ -151,5 +168,6 @@ test_files:
151
168
  - features/step_definitions/.rubyc_steps.rb.swp
152
169
  - features/step_definitions/rubyc_steps.rb
153
170
  - features/support/env.rb
171
+ - spec/cli_spec.rb
154
172
  - spec/rubyc_spec.rb
155
173
  - spec/spec_helper.rb
data/README.rdoc DELETED
@@ -1,29 +0,0 @@
1
- = Description {<img src="https://secure.travis-ci.org/martinos/rubyc.png" />}[http://travis-ci.org/martinos/rubyc]
2
- Adds Ruby's power to the command line.
3
- Supports many enumerator methods applied to STDIN. The current line is represented by the "line" variable name or it's shorter alias 'l'.
4
-
5
- To get help:
6
- rubyc help
7
-
8
- == Examples
9
- $ ls | rubyc map 'line.upcase'
10
- GEMFILE
11
- RAILS_VERSION
12
- README.RDOC
13
- RAKEFILE
14
- ACTIONMAILER
15
- ACTIONPACK
16
- ...
17
-
18
- Here are the currently supported methods:
19
- compact # Remove empty lines
20
- count_by # Count the number of lines that have the same property. The property is defined by the return value of the given the block.
21
- grep # Enumerable#grep the first argument is the pattern matcher and the second is the block executed on each line.
22
- map # Apply Enumerable#map on each line and outputs the results of the block by calling to_s on the returned object.
23
- merge # Merge consecutive lines
24
- scan # String#scan
25
- select # Enumerable#select
26
- sort_by # Emumerable#sort_by
27
- sum # Rails Enumerable#sum
28
- uniq # uniq
29
-