array_with_progress 0.1.1

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: d9168135d123a7340ac52333d1856408e57a3427
4
+ data.tar.gz: b5c16ae2b04e94a07719532365b4494d2fa18658
5
+ SHA512:
6
+ metadata.gz: f94f10c7006041ad9038cf4b6e1f3f747aca73078e923da686d19b7c675df34ff284c40ba047ba6e067ac62d9216c458c45032bf1d51d90e6eaec0c573fef7a7
7
+ data.tar.gz: ad43981523803f91c6d5ef8c7f4117ca37c94c338e972f4111da05f3b1375ddcfc7079cad54b6e72a78c29f3552c19aa0c8b9d36ba176c476c8f927b2ab1cfdf
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.4
6
+ - 2.2.0
7
+ - jruby-19mode
8
+ script:
9
+ bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in array_with_progress.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tomasz Dąbrowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # ArrayWithProgress
2
+
3
+ [![Build Status](https://travis-ci.org/dabroz/array_with_progress.svg?branch=master)](https://travis-ci.org/dabroz/array_with_progress)
4
+
5
+ Easily visualize progress of any Ruby task. A drop in replacement for `Array.each`.
6
+
7
+ Output uses different colors for successes/failured, allowing to easily check status of every task. Every line is trimmed/padded to match the terminal width. There is also a possiblity to change item name during processing. Transactions for `ActiveRecord` are also handled if required.
8
+
9
+ ![array_with_progress](https://cloud.githubusercontent.com/assets/179706/7215359/5cb6d116-e5d4-11e4-9a77-165e75330cfe.png)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'array_with_progress'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install array_with_progress
26
+
27
+ ## Usage
28
+
29
+ Basic usage is as easy as replacing all `Array.each` to `Array.each_with_progress` in your rake tasks.
30
+
31
+ ```ruby
32
+ User.all.each_with_progress do |user|
33
+ user.backup_avatar!
34
+ end
35
+ ```
36
+
37
+ One of the main features is the ability to provide statuses for each task. To do that, just return one of the following in your block:
38
+
39
+ - `:ok` (or `true`) - Success
40
+ - `:warning` - Success, but with some warnings
41
+ - `:error` (or `false`) - Failure
42
+ - `:skip` - Task was skipped
43
+
44
+ ```ruby
45
+ User.all.each_with_progress do |user|
46
+ if user.has_avatar?
47
+ user.backup_avatar! # returns true/false
48
+ else
49
+ :skip
50
+ end
51
+ end
52
+ ```
53
+
54
+ You can provide a description for task block (useful if many different operations are to be performed on the same collection):
55
+
56
+ ```ruby
57
+ User.all.each_with_progress('Backing up the avatar for') do |user|
58
+ user.backup_avatar!
59
+ end
60
+ ```
61
+
62
+ To change item name during processing (for example when performing migration on some data, and the correct name is discovered after some processing) use an extra block argument and one of the following:
63
+
64
+ ```ruby
65
+ User.all.each_with_progress('Backing up the avatar for') do |user, operation|
66
+ operation.change_name(user.avatar_path) # this will completely replace username in output
67
+ operation.expand_name(user.avatar_path) # this will append extra data to the username
68
+ user.backup_avatar!
69
+ end
70
+ ```
71
+
72
+ For `ActiveRecord` objects, to enclose processing in transaction you can use additional option `transaction`. It can be one the following:
73
+
74
+ - `none` - no transaction (default)
75
+ - `collection` - wrap whole array in a single transaction
76
+ - `member` - wrap every item in a separate transaction
77
+
78
+ ```ruby
79
+ User.all.each_with_progress('Backing up the avatar for', transaction: :collection) do |user|
80
+ user.backup_avatar!
81
+ end
82
+ ```
83
+
84
+ More complicated example:
85
+
86
+ ```ruby
87
+ User.all.each_with_progress('Reindex data for', transaction: :collection) do |user, operation|
88
+ if user.details_available?
89
+ operation.expand_name("[#{user.details_url}]")
90
+ begin
91
+ if user.reindex_details!
92
+ :ok
93
+ else
94
+ operation.expand_name("[#{user.details_url} - version mismatch]")
95
+ :warning
96
+ end
97
+ rescue UserDetailsHTTPError => e
98
+ operation.expand_name("[#{user.details_url} - HTTP 404 Not Found!]")
99
+ :error
100
+ end
101
+ else
102
+ :skip
103
+ end
104
+ end
105
+ ```
106
+
107
+ ## Development
108
+
109
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
110
+
111
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
112
+
113
+ ## Contributing
114
+
115
+ 1. Fork it ( https://github.com/dabroz/array_with_progress/fork )
116
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
117
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
118
+ 4. Push to the branch (`git push origin my-new-feature`)
119
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Run example'
4
+ task :example do
5
+ require './example/example.rb'
6
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'array_with_progress/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "array_with_progress"
8
+ spec.version = ArrayWithProgress::VERSION
9
+ spec.authors = ["Tomasz Dąbrowski"]
10
+ spec.email = ["t.dabrowski@rock-hard.eu"]
11
+
12
+ spec.summary = 'Easily visualize progress of any Ruby task. A drop in replacement for Array.each.'
13
+ spec.description = "Easily visualize progress of any Ruby task. A drop in replacement for Array.each. Output uses different colors for successes/failured, allowing to easily check status of every task. Every line is trimmed/padded to match the terminal width. There is also a possiblity to change item name during processing. Transactions for ActiveRecord are also handled if required."
14
+ spec.homepage = "https://github.com/dabroz/array_with_progress"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|example)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'colorize'
22
+ spec.add_runtime_dependency 'highline'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.2"
27
+ spec.add_development_dependency 'activerecord'
28
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "array_with_progress"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require 'highline/system_extensions'
2
+
3
+ class ArrayProgressConfiguration
4
+ def self.terminal_width
5
+ HighLine::SystemExtensions.terminal_size[1]
6
+ end
7
+
8
+ def self.item_width
9
+ terminal_width - 22
10
+ end
11
+ end
@@ -0,0 +1,115 @@
1
+ require 'array_with_progress/array_progress_configuration'
2
+ require 'colorize'
3
+
4
+ class ArrayProgressItem
5
+ attr_accessor :parent
6
+ attr_accessor :item
7
+
8
+ attr_accessor :status, :extra_name, :progress, :name
9
+
10
+ def self.run!(parent_, item_, progress_, &block)
11
+ pi = ArrayProgressItem.new
12
+ pi.parent = parent_
13
+ pi.item = item_
14
+ pi.progress = sprintf('[%5.1f%%] ', progress_)
15
+ pi.run!(&block)
16
+ end
17
+
18
+ def change_name(new_name)
19
+ set_name(new_name)
20
+ reprint!
21
+ end
22
+
23
+ def expand_name(extra_name)
24
+ self.extra_name = extra_name
25
+ reprint!
26
+ end
27
+
28
+ def run!
29
+ self.status = nil
30
+ self.extra_name = ''
31
+
32
+ set_name(item)
33
+ reprint!
34
+
35
+ self.status = process_status_code(yield(item, self))
36
+ reprint!
37
+ print "\n"
38
+ end
39
+
40
+ private
41
+
42
+ def process_status_code(code)
43
+ if code.is_a? Symbol
44
+ code
45
+ elsif code.is_a? TrueClass
46
+ :ok
47
+ elsif code.is_a? FalseClass
48
+ :error
49
+ else
50
+ :unknown
51
+ end
52
+ end
53
+
54
+ def description
55
+ parent.description
56
+ end
57
+
58
+ def set_name(new_name)
59
+ if new_name.is_a? Array
60
+ self.name = new_name.map(&:to_s).join('|')
61
+ else
62
+ self.name = new_name.to_s
63
+ end
64
+ end
65
+
66
+ def display_name
67
+ width = ArrayProgressConfiguration.item_width
68
+ (description + ' ' + self.name + ' ' + self.extra_name).strip[0..width-1].ljust(width, ' ')
69
+ end
70
+
71
+ def reprint!
72
+ print "\r"
73
+ print progress_colored
74
+ print name_colored
75
+ print result_colored
76
+ end
77
+
78
+ def progress_colored
79
+ if status == :error
80
+ progress.light_white.on_light_red
81
+ elsif status == :warning
82
+ progress.black.on_light_yellow
83
+ else
84
+ progress.white
85
+ end
86
+ end
87
+
88
+ def name_colored
89
+ if status == :error
90
+ display_name.light_white.on_light_red
91
+ elsif status == :warning
92
+ display_name.black.on_light_yellow
93
+ elsif status == :skip
94
+ display_name.white
95
+ else
96
+ display_name
97
+ end
98
+ end
99
+
100
+ def result_colored
101
+ if status == nil
102
+ ''
103
+ elsif status == :ok
104
+ ' [ Success ]'.green.bold
105
+ elsif status == :error
106
+ ' [ Failure ]'.light_white.on_light_red
107
+ elsif status == :warning
108
+ ' [ Warning ]'.black.on_light_yellow
109
+ elsif status == :skip
110
+ ' [ Skipped ]'.white.bold
111
+ else
112
+ ' [ Unknown ]'.bold
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,37 @@
1
+ require 'array_with_progress/array_progress_item'
2
+
3
+ class ArrayProgressOperation
4
+ attr_accessor :items
5
+ attr_accessor :description
6
+ attr_accessor :transaction
7
+
8
+ def initialize(items_, description_, options_)
9
+ self.items = items_
10
+ self.description = description_
11
+ self.transaction = options_[:transaction] || :none
12
+ end
13
+
14
+ def run!(&block)
15
+ count = self.items.count.to_f
16
+ run_with_transaction(:collection) do
17
+ self.items.each_with_index do |item, index|
18
+ run_with_transaction(:member) do
19
+ progress = index.to_f / count * 100.0
20
+ ArrayProgressItem.run!(self, item, progress, &block)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def run_with_transaction(transaction_type)
29
+ if self.transaction == transaction_type
30
+ ActiveRecord::Base.transaction do
31
+ yield
32
+ end
33
+ else
34
+ yield
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module ArrayWithProgress
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'array_with_progress/version'
2
+ require 'array_with_progress/array_progress_operation'
3
+
4
+ class Array
5
+ def each_with_progress(description = '', options = {}, &block)
6
+ ArrayProgressOperation.new(self, description, options).run!(&block)
7
+ end
8
+ end
9
+
10
+ module ArrayWithProgress
11
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_with_progress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Dąbrowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-18 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: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
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.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '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'
97
+ description: Easily visualize progress of any Ruby task. A drop in replacement for
98
+ Array.each. Output uses different colors for successes/failured, allowing to easily
99
+ check status of every task. Every line is trimmed/padded to match the terminal width.
100
+ There is also a possiblity to change item name during processing. Transactions for
101
+ ActiveRecord are also handled if required.
102
+ email:
103
+ - t.dabrowski@rock-hard.eu
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".gitignore"
109
+ - ".rspec"
110
+ - ".travis.yml"
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - array_with_progress.gemspec
116
+ - bin/console
117
+ - bin/setup
118
+ - lib/array_with_progress.rb
119
+ - lib/array_with_progress/array_progress_configuration.rb
120
+ - lib/array_with_progress/array_progress_item.rb
121
+ - lib/array_with_progress/array_progress_operation.rb
122
+ - lib/array_with_progress/version.rb
123
+ homepage: https://github.com/dabroz/array_with_progress
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.4.5
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Easily visualize progress of any Ruby task. A drop in replacement for Array.each.
147
+ test_files: []