pbatcher 0.0.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/.document +5 -0
- data/CONTRIBUTING.md +29 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +100 -0
- data/LICENSE.md +25 -0
- data/README.md +82 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/pbatcher/generator.rb +54 -0
- data/lib/pbatcher/handler.rb +9 -0
- data/lib/pbatcher/iterator.rb +29 -0
- data/lib/pbatcher/namer.rb +7 -0
- data/lib/pbatcher/printer.rb +45 -0
- data/lib/pbatcher/proc_handler.rb +16 -0
- data/lib/pbatcher/processor.rb +33 -0
- data/lib/pbatcher/validator.rb +9 -0
- data/lib/pbatcher.rb +16 -0
- data/test/helper.rb +51 -0
- data/test/test_generator.rb +27 -0
- data/test/test_iterator.rb +33 -0
- data/test/test_logger.rb +12 -0
- data/test/test_namer.rb +17 -0
- data/test/test_pbatcher.rb +11 -0
- data/test/test_printer.rb +41 -0
- data/test/test_processor.rb +21 -0
- data/test/test_validator.rb +19 -0
- metadata +156 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 27467cd06d8e1ccda1be1cd65130b45ed3551ea2
|
|
4
|
+
data.tar.gz: 79ed28ce7f0459590094e0f305110ee41f9b5885
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 871de621c34980137fc8db0a9720445bd2f75bf6250686a81771f5ed8c9c4211942d6188fc99b36e33bbe1e9ac4e3e4333b136916c00565fe6a0a84cd29a6255
|
|
7
|
+
data.tar.gz: ec065bb5d3077b15752a225c8c9097c1d72a93a5405d7a9e8abd623ca5c1cb11c54b1973fca566a7b1f80130ec17cc9937e36a6d59e95836fc8a04ba3da438cc
|
data/.document
ADDED
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
1. Fork this project
|
|
4
|
+
2. Create your feature branch `git checkout -b my-branch`
|
|
5
|
+
3. Add yourself as a developer to `README.md`!
|
|
6
|
+
4. Commit your changes `git commit -am 'Add some feature'`
|
|
7
|
+
5. Push to the branch `git push origin my-new-feature`
|
|
8
|
+
6. Create a pull request with the following format:
|
|
9
|
+
|
|
10
|
+
```markdown
|
|
11
|
+
## Description
|
|
12
|
+
|
|
13
|
+
This pull request will be used to [fix|implement|...] the following [bugs|features|...].
|
|
14
|
+
|
|
15
|
+
## Tasks
|
|
16
|
+
|
|
17
|
+
- [ ] Uncompleted task nº1.
|
|
18
|
+
- [x] Completed task nº2.
|
|
19
|
+
- [x] Buy eggs.
|
|
20
|
+
- [ ] Fix that strange bug.
|
|
21
|
+
- [ ] Etc.
|
|
22
|
+
|
|
23
|
+
## References
|
|
24
|
+
|
|
25
|
+
- [Example reference](http://www.example.com)
|
|
26
|
+
- [Wabit season!](https://www.youtube.com/watch?v=17ocaZb-bGg)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Remember that your help is always appreciated! :smile:
|
data/Gemfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
|
+
|
|
3
|
+
gem 'colorize'
|
|
4
|
+
|
|
5
|
+
group :development do
|
|
6
|
+
gem 'rdoc', '~> 3.12'
|
|
7
|
+
gem 'bundler', '~> 1.0'
|
|
8
|
+
gem 'juwelier', '~> 2.1.0'
|
|
9
|
+
gem 'pry', '~> 0.10.0'
|
|
10
|
+
gem 'pry-nav', '~> 0.2.0'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
group :test do
|
|
14
|
+
gem 'test-unit', '>= 3.2'
|
|
15
|
+
gem 'shoulda', '>= 0'
|
|
16
|
+
gem 'simplecov', '>= 0'
|
|
17
|
+
end
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
GEM
|
|
2
|
+
remote: https://rubygems.org/
|
|
3
|
+
specs:
|
|
4
|
+
activesupport (5.0.0.1)
|
|
5
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
6
|
+
i18n (~> 0.7)
|
|
7
|
+
minitest (~> 5.1)
|
|
8
|
+
tzinfo (~> 1.1)
|
|
9
|
+
addressable (2.4.0)
|
|
10
|
+
builder (3.2.2)
|
|
11
|
+
coderay (1.1.1)
|
|
12
|
+
colorize (0.8.1)
|
|
13
|
+
concurrent-ruby (1.0.2)
|
|
14
|
+
descendants_tracker (0.0.4)
|
|
15
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
|
16
|
+
docile (1.1.5)
|
|
17
|
+
faraday (0.9.2)
|
|
18
|
+
multipart-post (>= 1.2, < 3)
|
|
19
|
+
git (1.3.0)
|
|
20
|
+
github_api (0.14.5)
|
|
21
|
+
addressable (~> 2.4.0)
|
|
22
|
+
descendants_tracker (~> 0.0.4)
|
|
23
|
+
faraday (~> 0.8, < 0.10)
|
|
24
|
+
hashie (>= 3.4)
|
|
25
|
+
oauth2 (~> 1.0)
|
|
26
|
+
hashie (3.4.6)
|
|
27
|
+
highline (1.7.8)
|
|
28
|
+
i18n (0.7.0)
|
|
29
|
+
json (1.8.3)
|
|
30
|
+
juwelier (2.1.2)
|
|
31
|
+
builder
|
|
32
|
+
bundler (>= 1.0)
|
|
33
|
+
git (>= 1.2.5)
|
|
34
|
+
github_api
|
|
35
|
+
highline (>= 1.6.15)
|
|
36
|
+
nokogiri (>= 1.5.10)
|
|
37
|
+
rake
|
|
38
|
+
rdoc
|
|
39
|
+
semver
|
|
40
|
+
jwt (1.5.6)
|
|
41
|
+
method_source (0.8.2)
|
|
42
|
+
mini_portile2 (2.1.0)
|
|
43
|
+
minitest (5.9.1)
|
|
44
|
+
multi_json (1.12.1)
|
|
45
|
+
multi_xml (0.5.5)
|
|
46
|
+
multipart-post (2.0.0)
|
|
47
|
+
nokogiri (1.6.8.1)
|
|
48
|
+
mini_portile2 (~> 2.1.0)
|
|
49
|
+
oauth2 (1.2.0)
|
|
50
|
+
faraday (>= 0.8, < 0.10)
|
|
51
|
+
jwt (~> 1.0)
|
|
52
|
+
multi_json (~> 1.3)
|
|
53
|
+
multi_xml (~> 0.5)
|
|
54
|
+
rack (>= 1.2, < 3)
|
|
55
|
+
power_assert (0.3.0)
|
|
56
|
+
pry (0.10.4)
|
|
57
|
+
coderay (~> 1.1.0)
|
|
58
|
+
method_source (~> 0.8.1)
|
|
59
|
+
slop (~> 3.4)
|
|
60
|
+
pry-nav (0.2.4)
|
|
61
|
+
pry (>= 0.9.10, < 0.11.0)
|
|
62
|
+
rack (2.0.1)
|
|
63
|
+
rake (11.3.0)
|
|
64
|
+
rdoc (3.12.2)
|
|
65
|
+
json (~> 1.4)
|
|
66
|
+
semver (1.0.1)
|
|
67
|
+
shoulda (3.5.0)
|
|
68
|
+
shoulda-context (~> 1.0, >= 1.0.1)
|
|
69
|
+
shoulda-matchers (>= 1.4.1, < 3.0)
|
|
70
|
+
shoulda-context (1.2.1)
|
|
71
|
+
shoulda-matchers (2.8.0)
|
|
72
|
+
activesupport (>= 3.0.0)
|
|
73
|
+
simplecov (0.12.0)
|
|
74
|
+
docile (~> 1.1.0)
|
|
75
|
+
json (>= 1.8, < 3)
|
|
76
|
+
simplecov-html (~> 0.10.0)
|
|
77
|
+
simplecov-html (0.10.0)
|
|
78
|
+
slop (3.6.0)
|
|
79
|
+
test-unit (3.2.1)
|
|
80
|
+
power_assert
|
|
81
|
+
thread_safe (0.3.5)
|
|
82
|
+
tzinfo (1.2.2)
|
|
83
|
+
thread_safe (~> 0.1)
|
|
84
|
+
|
|
85
|
+
PLATFORMS
|
|
86
|
+
ruby
|
|
87
|
+
|
|
88
|
+
DEPENDENCIES
|
|
89
|
+
bundler (~> 1.0)
|
|
90
|
+
colorize
|
|
91
|
+
juwelier (~> 2.1.0)
|
|
92
|
+
pry (~> 0.10.0)
|
|
93
|
+
pry-nav (~> 0.2.0)
|
|
94
|
+
rdoc (~> 3.12)
|
|
95
|
+
shoulda
|
|
96
|
+
simplecov
|
|
97
|
+
test-unit (>= 3.2)
|
|
98
|
+
|
|
99
|
+
BUNDLED WITH
|
|
100
|
+
1.12.5
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
=====================
|
|
3
|
+
|
|
4
|
+
Copyright © `2016` `Daniel Herzog`
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person
|
|
7
|
+
obtaining a copy of this software and associated documentation
|
|
8
|
+
files (the “Software”), to deal in the Software without
|
|
9
|
+
restriction, including without limitation the rights to use,
|
|
10
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the
|
|
12
|
+
Software is furnished to do so, subject to the following
|
|
13
|
+
conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be
|
|
16
|
+
included in all copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
20
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
23
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
24
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
25
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
[](https://badge.fury.io/rb/pbatcher) [](https://gemnasium.com/github.com/wikiti/pbatcher) [](https://circleci.com/gh/wikiti/pbatcher)
|
|
2
|
+
|
|
3
|
+
# Process Batcher
|
|
4
|
+
|
|
5
|
+
Process Batcher (or `pbatcher`) is a ruby gem used to build quick CLI apps to process batches of tasks.
|
|
6
|
+
|
|
7
|
+
This project uses [`juwelier`](https://github.com/flajann2/juwelier) for managing and releasing this gem.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'pbatcher'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
$ bundle install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or install it yourself as:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
$ gem install pbatcher
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
`pbatcher` uses a simple DSL syntax to construct applications.
|
|
32
|
+
|
|
33
|
+
For example, to create an application that takes an array of numbers, and calculate their double, and take only those
|
|
34
|
+
which are smaller than `30`:
|
|
35
|
+
|
|
36
|
+
```rb
|
|
37
|
+
require 'pbatcher'
|
|
38
|
+
|
|
39
|
+
pbatcher do
|
|
40
|
+
# Define the iterator (data) to process
|
|
41
|
+
iteration [1, 5, 10, 20]
|
|
42
|
+
|
|
43
|
+
# Define the callback to process each item.
|
|
44
|
+
processing { |x| x * 2 }
|
|
45
|
+
|
|
46
|
+
# Validate the processed content
|
|
47
|
+
# (double of x must be smaller than 30)
|
|
48
|
+
validation { |x| x < 30 }
|
|
49
|
+
|
|
50
|
+
# Name each item for output purposes
|
|
51
|
+
naming { |x| "nº #{x}" }
|
|
52
|
+
end
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This program will output the following:
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
Processing nº 1 [OK]
|
|
59
|
+
Processing nº 5 [OK]
|
|
60
|
+
Processing nº 10 [OK]
|
|
61
|
+
Processing nº 20 [FAIL]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Options
|
|
65
|
+
|
|
66
|
+
The DSL includes the following methods within the `pbatcher` method:
|
|
67
|
+
|
|
68
|
+
| Method | Description | Argument | Default value |
|
|
69
|
+
| ------ | ----------- | -------- | ------------- |
|
|
70
|
+
| iteration | Data to iterate. Each item will be processed as an unique task. | `Enumerator`, `Proc`, or anything that respond to `:each`. | `[]` |
|
|
71
|
+
| processing | Items handler. A method which is run for each item. Any unhandled exception will result into a `FAIL` | A block, `proc` or a `lambda`. | `->(x) { x }` |
|
|
72
|
+
| validation | Validates the output of `processing` to mark the item as `OK` or `FAIL`. | A block, `proc` or a `lambda`. | `->(x) { true }` |
|
|
73
|
+
| naming | A simple method to given a human-readable name to each item. | A block, `proc` or a `lambda`. | `->(x) { x.to_s }` |
|
|
74
|
+
| logger | A logger item to inform about errors. Can be accessed via `logger` within the `pbatcher` context. | Any object that respond to any method supported by Ruby's `Logger` class. | `Logger.new('pbatcher.log')` |
|
|
75
|
+
|
|
76
|
+
## Authors
|
|
77
|
+
|
|
78
|
+
This project has been developed by:
|
|
79
|
+
|
|
80
|
+
| Avatar | Name | Nickname | Email |
|
|
81
|
+
| ------- | ------------- | --------- | ------------------ |
|
|
82
|
+
|  | Daniel Herzog | Wikiti | [info@danielherzog.es](mailto:info@danielherzog.es) |
|
data/Rakefile
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'bundler'
|
|
5
|
+
begin
|
|
6
|
+
Bundler.setup(:default, :development)
|
|
7
|
+
rescue Bundler::BundlerError => e
|
|
8
|
+
$stderr.puts e.message
|
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
|
10
|
+
exit e.status_code
|
|
11
|
+
end
|
|
12
|
+
require 'rake'
|
|
13
|
+
|
|
14
|
+
require 'juwelier'
|
|
15
|
+
Juwelier::Tasks.new do |gem|
|
|
16
|
+
gem.name = "pbatcher"
|
|
17
|
+
gem.homepage = "http://github.com/wikiti/pbatcher"
|
|
18
|
+
gem.license = "MIT"
|
|
19
|
+
gem.summary = %Q{A simple library to create CLI batch applications.}
|
|
20
|
+
gem.description = %Q{Process Batcher (or `pbatcher`) is a ruby gem used to build quick CLI apps to process batches of tasks.}
|
|
21
|
+
gem.email = "info@danielherzog.es"
|
|
22
|
+
gem.authors = ["Daniel Herzog"]
|
|
23
|
+
end
|
|
24
|
+
Juwelier::RubygemsDotOrgTasks.new
|
|
25
|
+
|
|
26
|
+
require 'rake/testtask'
|
|
27
|
+
Rake::TestTask.new(:test) do |test|
|
|
28
|
+
test.libs << 'lib' << 'test'
|
|
29
|
+
test.pattern = 'test/**/test_*.rb'
|
|
30
|
+
test.verbose = true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
desc "Code coverage detail"
|
|
34
|
+
task :simplecov do
|
|
35
|
+
ENV['COVERAGE'] = "true"
|
|
36
|
+
Rake::Task['test'].execute
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
task :default => :test
|
|
40
|
+
|
|
41
|
+
require 'rdoc/task'
|
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
|
44
|
+
|
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
46
|
+
rdoc.title = "pbatcher #{version}"
|
|
47
|
+
rdoc.rdoc_files.include('README*')
|
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
49
|
+
end
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.1
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module PBatcher
|
|
2
|
+
class Generator
|
|
3
|
+
attr_reader :iterator, :processor, :validator, :namer, :logger
|
|
4
|
+
|
|
5
|
+
def self.build(&block)
|
|
6
|
+
obj = new
|
|
7
|
+
obj.instance_eval(&block) if block_given?
|
|
8
|
+
obj.run
|
|
9
|
+
obj
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def run
|
|
17
|
+
setup_default_handlers
|
|
18
|
+
logger.info '------------------------------------'
|
|
19
|
+
iterator.iterate { |item| processor.process item }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def naming(l = nil, &block)
|
|
23
|
+
@namer = Namer.new self, l || block
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def validation(l = nil, &block)
|
|
27
|
+
@validator = Validator.new self, l || block
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def iteration(data)
|
|
31
|
+
@iterator = Iterator.new self, data
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def processing(l = nil, &block)
|
|
35
|
+
@processor = Processor.new self, l || block
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def logging(logger)
|
|
39
|
+
@logger = logger
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def setup_default_handlers
|
|
45
|
+
@iterator ||= Iterator.new self, []
|
|
46
|
+
@validator ||= Validator.new self, ->(item) { true }
|
|
47
|
+
@namer ||= Namer.new self, ->(item) { item.to_s }
|
|
48
|
+
@processor ||= Processor.new self, ->(item) { item }
|
|
49
|
+
@logger ||= ::Logger.new('pbatcher.log')
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# ...
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module PBatcher
|
|
2
|
+
class Iterator < Handler
|
|
3
|
+
attr_reader :data
|
|
4
|
+
|
|
5
|
+
def initialize(generator, data)
|
|
6
|
+
super(generator)
|
|
7
|
+
@data = data
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def iterate(&block)
|
|
11
|
+
iterator.each(&block)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def iterator
|
|
17
|
+
case data
|
|
18
|
+
when Enumerator
|
|
19
|
+
data
|
|
20
|
+
when Proc
|
|
21
|
+
data.call()
|
|
22
|
+
when ->(data) { data.respond_to? :each }
|
|
23
|
+
data
|
|
24
|
+
else
|
|
25
|
+
raise NotImplementedError, "Data type cannot be handled because it doesn't respond to :each."
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module PBatcher
|
|
2
|
+
module Printer
|
|
3
|
+
extend self
|
|
4
|
+
|
|
5
|
+
SUCCESS_MESSAGE = "OK".green.freeze
|
|
6
|
+
FAILURE_MESSAGE = "FAIL".red.freeze
|
|
7
|
+
|
|
8
|
+
OVERFLOW = '...'
|
|
9
|
+
|
|
10
|
+
def message(str, options = { max_size: 90 })
|
|
11
|
+
print message_str(str, options)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def success
|
|
15
|
+
print "[#{SUCCESS_MESSAGE}]"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def failure
|
|
19
|
+
print "[#{FAILURE_MESSAGE}]"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def newline
|
|
23
|
+
puts ''
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def message_str(str, options)
|
|
29
|
+
return message_str_overflow(str, options) if message_str_overflow?(str, options)
|
|
30
|
+
message_str_normal(str, options)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def message_str_overflow(str, options)
|
|
34
|
+
str[0...(options[:max_size] - OVERFLOW.size)] + OVERFLOW
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def message_str_normal(str, options)
|
|
38
|
+
str.ljust options[:max_size]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def message_str_overflow?(str, options)
|
|
42
|
+
str.size > options[:max_size] - OVERFLOW.size
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module PBatcher
|
|
2
|
+
class ProcHandler < Handler
|
|
3
|
+
attr_reader :method
|
|
4
|
+
|
|
5
|
+
def initialize(generator, method = nil, &block)
|
|
6
|
+
super(generator)
|
|
7
|
+
method = method ? method : block
|
|
8
|
+
raise ArgumentError, 'Namer is not a proc' unless method.is_a? Proc
|
|
9
|
+
@method = method || block
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call(*args)
|
|
13
|
+
method.call(*args)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module PBatcher
|
|
2
|
+
class Processor < ProcHandler
|
|
3
|
+
def process(item)
|
|
4
|
+
pre(item)
|
|
5
|
+
result = call(item)
|
|
6
|
+
raise "Item #{item} is not a valid item." unless generator.validator.valid?(result)
|
|
7
|
+
success(item)
|
|
8
|
+
result
|
|
9
|
+
rescue => e
|
|
10
|
+
failure(item, e)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def pre(item)
|
|
16
|
+
Printer.message "Processing #{generator.namer.name item}"
|
|
17
|
+
generator.logger.info "Processing #{generator.namer.name item}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def success(item)
|
|
21
|
+
Printer.success
|
|
22
|
+
Printer.newline
|
|
23
|
+
generator.logger.info "Processed #{generator.namer.name item}"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def failure(item, exception)
|
|
27
|
+
Printer.failure
|
|
28
|
+
Printer.newline
|
|
29
|
+
generator.logger.error "Error while processing #{generator.namer.name item}: #{exception}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
33
|
+
end
|
data/lib/pbatcher.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'logger'
|
|
2
|
+
|
|
3
|
+
require 'colorize'
|
|
4
|
+
|
|
5
|
+
require 'pbatcher/printer'
|
|
6
|
+
require 'pbatcher/handler'
|
|
7
|
+
require 'pbatcher/proc_handler'
|
|
8
|
+
require 'pbatcher/processor'
|
|
9
|
+
require 'pbatcher/validator'
|
|
10
|
+
require 'pbatcher/namer'
|
|
11
|
+
require 'pbatcher/iterator'
|
|
12
|
+
require 'pbatcher/generator'
|
|
13
|
+
|
|
14
|
+
def pbatcher(&block)
|
|
15
|
+
PBatcher::Generator.build(&block)
|
|
16
|
+
end
|
data/test/helper.rb
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'bundler'
|
|
3
|
+
begin
|
|
4
|
+
Bundler.setup(:default, :development)
|
|
5
|
+
rescue Bundler::BundlerError => e
|
|
6
|
+
$stderr.puts e.message
|
|
7
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
|
8
|
+
exit e.status_code
|
|
9
|
+
end
|
|
10
|
+
require 'test/unit'
|
|
11
|
+
require 'shoulda'
|
|
12
|
+
|
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
15
|
+
require 'pbatcher'
|
|
16
|
+
|
|
17
|
+
class Test::Unit::TestCase
|
|
18
|
+
def capture_stdout
|
|
19
|
+
original_stdout = $stdout
|
|
20
|
+
$stdout = fake = StringIO.new
|
|
21
|
+
yield
|
|
22
|
+
fake.string
|
|
23
|
+
ensure
|
|
24
|
+
$stdout = original_stdout
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def nil_pbatcher
|
|
28
|
+
pbatcher
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def pbatcher_example
|
|
32
|
+
output = nil
|
|
33
|
+
capture_stdout do
|
|
34
|
+
output = pbatcher do
|
|
35
|
+
# Define the iterator (data) to process
|
|
36
|
+
iteration [1, 5, 10, 20]
|
|
37
|
+
|
|
38
|
+
# Define the callback to process each item.
|
|
39
|
+
processing { |x| x * 2 }
|
|
40
|
+
|
|
41
|
+
# Validate the processed content
|
|
42
|
+
# (double of x must be smaller than 30)
|
|
43
|
+
validation { |x| x < 30 }
|
|
44
|
+
|
|
45
|
+
# Name each item for output purposes
|
|
46
|
+
naming { |x| "nº #{x}" }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
output
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestGenerator < Test::Unit::TestCase
|
|
4
|
+
context 'creating an empty generator' do
|
|
5
|
+
should 'has all handlers' do
|
|
6
|
+
@generator = PBatcher::Generator.build
|
|
7
|
+
|
|
8
|
+
assert !@generator.logger.nil?
|
|
9
|
+
assert !@generator.processor.nil?
|
|
10
|
+
assert !@generator.namer.nil?
|
|
11
|
+
assert !@generator.validator.nil?
|
|
12
|
+
assert !@generator.iterator.nil?
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
context 'creating a complete generator' do
|
|
17
|
+
should 'be iterable' do
|
|
18
|
+
@generator = pbatcher_example
|
|
19
|
+
|
|
20
|
+
assert !@generator.logger.nil?
|
|
21
|
+
assert !@generator.processor.nil?
|
|
22
|
+
assert !@generator.namer.nil?
|
|
23
|
+
assert !@generator.validator.nil?
|
|
24
|
+
assert !@generator.iterator.nil?
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestIterator < Test::Unit::TestCase
|
|
4
|
+
context 'creating an iterator with an array' do
|
|
5
|
+
should 'be iterable' do
|
|
6
|
+
@iterator = PBatcher::Iterator.new nil, [1, 2, 3]
|
|
7
|
+
assert @iterator.iterate.is_a?(Enumerator)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context 'creating an iterator with an enumerable' do
|
|
12
|
+
should 'be iterable' do
|
|
13
|
+
@iterator = PBatcher::Iterator.new nil, (1..3)
|
|
14
|
+
assert @iterator.iterate.is_a?(Enumerator)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
context 'creating an iterator with a block' do
|
|
19
|
+
should 'be iterable' do
|
|
20
|
+
@iterator = PBatcher::Iterator.new nil, -> { (1..3) }
|
|
21
|
+
assert @iterator.iterate.is_a?(Enumerator)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context 'creating an iterator with a block' do
|
|
26
|
+
should 'not be iterable' do
|
|
27
|
+
@iterator = PBatcher::Iterator.new nil, -> { nil }
|
|
28
|
+
assert_raise do
|
|
29
|
+
@iterator.iterate
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
data/test/test_logger.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestValidator < Test::Unit::TestCase
|
|
4
|
+
context 'creating a cli application' do
|
|
5
|
+
should 'be a valid logger' do
|
|
6
|
+
@pbatcher = nil_pbatcher
|
|
7
|
+
%i(debug error fatal log warn).each do |method|
|
|
8
|
+
assert @pbatcher.logger.respond_to?(method)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
data/test/test_namer.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestNamer < Test::Unit::TestCase
|
|
4
|
+
context 'creating a namer with a block' do
|
|
5
|
+
should 'return the representation of the item' do
|
|
6
|
+
@namer = PBatcher::Namer.new(nil_pbatcher) { |x| "-#{x}-" }
|
|
7
|
+
assert_equal @namer.name(3), "-3-"
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context 'creating a namer with a proc' do
|
|
12
|
+
should 'return the representation of the item' do
|
|
13
|
+
@namer = PBatcher::Namer.new nil_pbatcher, ->(x) { "-#{x}-" }
|
|
14
|
+
assert_equal @namer.name(3), "-3-"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestPrinter < Test::Unit::TestCase
|
|
4
|
+
context 'priting success' do
|
|
5
|
+
should 'print to stdout' do
|
|
6
|
+
stdout = capture_stdout { PBatcher::Printer.success }
|
|
7
|
+
assert_equal stdout, "[#{PBatcher::Printer::SUCCESS_MESSAGE}]"
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context 'priting failure' do
|
|
12
|
+
should 'print to stdout' do
|
|
13
|
+
stdout = capture_stdout { PBatcher::Printer.failure }
|
|
14
|
+
assert_equal stdout, "[#{PBatcher::Printer::FAILURE_MESSAGE}]"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
context 'priting message without overflow' do
|
|
19
|
+
should 'print to stdout' do
|
|
20
|
+
stdout = capture_stdout { PBatcher::Printer.message 'Message' }
|
|
21
|
+
assert_match /^Message\s+$/, stdout
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context 'priting message with overflow' do
|
|
26
|
+
should 'print to stdout' do
|
|
27
|
+
max_size = 100
|
|
28
|
+
str = 'a' * max_size
|
|
29
|
+
stdout = capture_stdout { PBatcher::Printer.message str, max_size: max_size }
|
|
30
|
+
assert_not_equal stdout, str
|
|
31
|
+
assert_match /\.\.\.$/, stdout
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
context 'priting newline' do
|
|
36
|
+
should 'print to stdout' do
|
|
37
|
+
stdout = capture_stdout { PBatcher::Printer.newline }
|
|
38
|
+
assert_match /^\r?\n$/, stdout
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestProcessor < Test::Unit::TestCase
|
|
4
|
+
context 'creating a processor with a block' do
|
|
5
|
+
should 'return the processed value' do
|
|
6
|
+
@processor = PBatcher::Processor.new(nil_pbatcher) { |x| x * 2 }
|
|
7
|
+
value = nil
|
|
8
|
+
capture_stdout { value = @processor.process(3) }
|
|
9
|
+
assert_equal value, 3 * 2
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context 'creating a processor with a proc' do
|
|
14
|
+
should 'return the processed value' do
|
|
15
|
+
@processor = PBatcher::Processor.new nil_pbatcher, ->(x) { x * 2 }
|
|
16
|
+
value = nil
|
|
17
|
+
capture_stdout { value = @processor.process(3) }
|
|
18
|
+
assert_equal value, 3 * 2
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestValidator < Test::Unit::TestCase
|
|
4
|
+
context 'creating a validator with a block' do
|
|
5
|
+
should 'validate properly' do
|
|
6
|
+
@validator = PBatcher::Validator.new(nil_pbatcher) { |x| x.odd? }
|
|
7
|
+
assert @validator.valid?(3)
|
|
8
|
+
assert !@validator.valid?(2)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
context 'creating a validator with a proc' do
|
|
13
|
+
should 'validate properly' do
|
|
14
|
+
@validator = PBatcher::Validator.new nil_pbatcher, ->(x) { x.odd? }
|
|
15
|
+
assert @validator.valid?(3)
|
|
16
|
+
assert !@validator.valid?(2)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pbatcher
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daniel Herzog
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-10-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: colorize
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rdoc
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.12'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.12'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: juwelier
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 2.1.0
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 2.1.0
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: pry
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 0.10.0
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 0.10.0
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: pry-nav
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 0.2.0
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 0.2.0
|
|
97
|
+
description: Process Batcher (or `pbatcher`) is a ruby gem used to build quick CLI
|
|
98
|
+
apps to process batches of tasks.
|
|
99
|
+
email: info@danielherzog.es
|
|
100
|
+
executables: []
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files:
|
|
103
|
+
- LICENSE.md
|
|
104
|
+
- README.md
|
|
105
|
+
files:
|
|
106
|
+
- ".document"
|
|
107
|
+
- CONTRIBUTING.md
|
|
108
|
+
- Gemfile
|
|
109
|
+
- Gemfile.lock
|
|
110
|
+
- LICENSE.md
|
|
111
|
+
- README.md
|
|
112
|
+
- Rakefile
|
|
113
|
+
- VERSION
|
|
114
|
+
- lib/pbatcher.rb
|
|
115
|
+
- lib/pbatcher/generator.rb
|
|
116
|
+
- lib/pbatcher/handler.rb
|
|
117
|
+
- lib/pbatcher/iterator.rb
|
|
118
|
+
- lib/pbatcher/namer.rb
|
|
119
|
+
- lib/pbatcher/printer.rb
|
|
120
|
+
- lib/pbatcher/proc_handler.rb
|
|
121
|
+
- lib/pbatcher/processor.rb
|
|
122
|
+
- lib/pbatcher/validator.rb
|
|
123
|
+
- test/helper.rb
|
|
124
|
+
- test/test_generator.rb
|
|
125
|
+
- test/test_iterator.rb
|
|
126
|
+
- test/test_logger.rb
|
|
127
|
+
- test/test_namer.rb
|
|
128
|
+
- test/test_pbatcher.rb
|
|
129
|
+
- test/test_printer.rb
|
|
130
|
+
- test/test_processor.rb
|
|
131
|
+
- test/test_validator.rb
|
|
132
|
+
homepage: http://github.com/wikiti/pbatcher
|
|
133
|
+
licenses:
|
|
134
|
+
- MIT
|
|
135
|
+
metadata: {}
|
|
136
|
+
post_install_message:
|
|
137
|
+
rdoc_options: []
|
|
138
|
+
require_paths:
|
|
139
|
+
- lib
|
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
141
|
+
requirements:
|
|
142
|
+
- - ">="
|
|
143
|
+
- !ruby/object:Gem::Version
|
|
144
|
+
version: '0'
|
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
|
+
requirements:
|
|
147
|
+
- - ">="
|
|
148
|
+
- !ruby/object:Gem::Version
|
|
149
|
+
version: '0'
|
|
150
|
+
requirements: []
|
|
151
|
+
rubyforge_project:
|
|
152
|
+
rubygems_version: 2.4.5.1
|
|
153
|
+
signing_key:
|
|
154
|
+
specification_version: 4
|
|
155
|
+
summary: A simple library to create CLI batch applications.
|
|
156
|
+
test_files: []
|