Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit b727392

Browse files
author
Mikhail Georgievskiy
committed
Initial commit
0 parents  commit b727392

File tree

14 files changed

+521
-0
lines changed

14 files changed

+521
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
10+
# rspec failure tracking
11+
.rspec_status

.rubocop.yml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
AllCops:
2+
NewCops: disable
3+
4+
Gemspec/RequiredRubyVersion:
5+
Enabled: false
6+
7+
Style/StringLiterals:
8+
EnforcedStyle: single_quotes
9+
Enabled: true
10+
11+
# kind_of? is a good way to check a type
12+
Style/ClassCheck:
13+
EnforcedStyle: kind_of?
14+
15+
# It's better to be more explicit about the type
16+
Layout/AccessModifierIndentation:
17+
Enabled: false
18+
19+
# specs sometimes have useless assignments, which is fine
20+
Lint/UselessAssignment:
21+
Exclude:
22+
- '**/spec/**/*'
23+
24+
# We could potentially enable the 2 below:
25+
Layout/FirstHashElementIndentation:
26+
Enabled: false
27+
28+
Layout/HashAlignment:
29+
Enabled: false
30+
31+
# HoundCI doesn't like this rule
32+
Layout/DotPosition:
33+
Enabled: false
34+
35+
# We allow !! as it's an easy way to convert ot boolean
36+
Style/DoubleNegation:
37+
Enabled: false
38+
39+
# Cop supports --auto-correct.
40+
Lint/UnusedBlockArgument:
41+
Enabled: false
42+
43+
# We want to allow class Fastlane::Class
44+
Style/ClassAndModuleChildren:
45+
Enabled: false
46+
47+
Metrics/AbcSize:
48+
Max: 60
49+
50+
# The %w might be confusing for new users
51+
Style/WordArray:
52+
MinSize: 19
53+
54+
# raise and fail are both okay
55+
Style/SignalException:
56+
Enabled: false
57+
58+
# Better too much 'return' than one missing
59+
Style/RedundantReturn:
60+
Enabled: false
61+
62+
# Having if in the same line might not always be good
63+
Style/IfUnlessModifier:
64+
Enabled: false
65+
66+
# and and or is okay
67+
Style/AndOr:
68+
Enabled: false
69+
70+
# Configuration parameters: CountComments.
71+
Metrics/ClassLength:
72+
Max: 350
73+
74+
Metrics/CyclomaticComplexity:
75+
Max: 17
76+
77+
# Configuration parameters: AllowURI, URISchemes.
78+
Layout/LineLength:
79+
Max: 370
80+
81+
# Configuration parameters: CountKeywordArgs.
82+
Metrics/ParameterLists:
83+
Max: 10
84+
85+
Metrics/PerceivedComplexity:
86+
Max: 18
87+
88+
# Sometimes it's easier to read without guards
89+
Style/GuardClause:
90+
Enabled: false
91+
92+
# something = if something_else
93+
# that's confusing
94+
Style/ConditionalAssignment:
95+
Enabled: false
96+
97+
# Better to have too much self than missing a self
98+
Style/RedundantSelf:
99+
Enabled: false
100+
101+
Metrics/MethodLength:
102+
Max: 60
103+
104+
# We're not there yet
105+
Style/Documentation:
106+
Enabled: false
107+
108+
# Adds complexity
109+
Style/IfInsideElse:
110+
Enabled: false
111+
112+
# danger specific
113+
114+
Style/BlockComments:
115+
Enabled: false
116+
117+
Layout/MultilineMethodCallIndentation:
118+
EnforcedStyle: indented
119+
120+
# FIXME: 25
121+
Metrics/BlockLength:
122+
Max: 345
123+
Exclude:
124+
- "**/*_spec.rb"
125+
126+
Style/MixinGrouping:
127+
Enabled: false
128+
129+
Naming/FileName:
130+
Enabled: false
131+
132+
Layout/HeredocIndentation:
133+
Enabled: false
134+
135+
Style/SpecialGlobalVars:
136+
Enabled: false
137+
138+
Security/YAMLLoad:
139+
Enabled: false

Gemfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
# Specify your gem's dependencies in unity-utils.gemspec
6+
gemspec
7+
8+
gem 'pry', '~> 3.0'
9+
10+
gem 'rspec', '~> 3.0'
11+
12+
gem 'rubocop', '~> 1.21.0'
13+
14+
gem 'ruby-progressbar', '~> 1.11.0'

Gemfile.lock

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
PATH
2+
remote: .
3+
specs:
4+
unity-utils (0.1.0)
5+
ruby-progressbar
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
ast (2.4.2)
11+
coderay (1.1.3)
12+
diff-lcs (1.4.4)
13+
method_source (1.0.0)
14+
parallel (1.20.1)
15+
parser (3.0.2.0)
16+
ast (~> 2.4.1)
17+
pry (0.14.0)
18+
coderay (~> 1.1)
19+
method_source (~> 1.0)
20+
rainbow (3.0.0)
21+
regexp_parser (2.1.1)
22+
rexml (3.2.5)
23+
rspec (3.10.0)
24+
rspec-core (~> 3.10.0)
25+
rspec-expectations (~> 3.10.0)
26+
rspec-mocks (~> 3.10.0)
27+
rspec-core (3.10.1)
28+
rspec-support (~> 3.10.0)
29+
rspec-expectations (3.10.1)
30+
diff-lcs (>= 1.2.0, < 2.0)
31+
rspec-support (~> 3.10.0)
32+
rspec-mocks (3.10.2)
33+
diff-lcs (>= 1.2.0, < 2.0)
34+
rspec-support (~> 3.10.0)
35+
rspec-support (3.10.2)
36+
rubocop (1.20.0)
37+
parallel (~> 1.10)
38+
parser (>= 3.0.0.0)
39+
rainbow (>= 2.2.2, < 4.0)
40+
regexp_parser (>= 1.8, < 3.0)
41+
rexml
42+
rubocop-ast (>= 1.9.1, < 2.0)
43+
ruby-progressbar (~> 1.7)
44+
unicode-display_width (>= 1.4.0, < 3.0)
45+
rubocop-ast (1.11.0)
46+
parser (>= 3.0.1.1)
47+
ruby-progressbar (1.11.0)
48+
unicode-display_width (2.0.0)
49+
50+
PLATFORMS
51+
x86_64-darwin-19
52+
53+
DEPENDENCIES
54+
pry
55+
rspec
56+
rubocop
57+
ruby-progressbar
58+
unity-utils!
59+
60+
BUNDLED WITH
61+
2.2.12

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# UnityUtils
2+
3+
Gem with utilities used by Unity team
4+
5+
# Documentation
6+
7+
- ### Utils:
8+
- **Retrier** - Restarting passed block.
9+
```ruby
10+
Unity::Utils::Retrier.call { 5 / 0 }
11+
```
12+
13+
- **ThreadPool** - Working with multithreading.
14+
```ruby
15+
result = []
16+
pool = Unity::Utils::ThreadPool.new(50)
17+
18+
10.times { |n| pool.schedule { result << n * n } }
19+
20+
pool.run!
21+
```
22+
23+
- ### Modules:
24+
- **CliModeable** - Wrapper over the ruby-progressbar.
25+
```ruby
26+
elements = (1..10).to_a
27+
@cli_mode = true
28+
29+
init_progressbar(elements.count)
30+
31+
elements.each do |element|
32+
incr_progressbar
33+
34+
# do something and look on great progress bar :)
35+
end
36+
```
37+
38+
- **Loggable** - Wrapper over logger.
39+
```ruby
40+
@log_file = 'some_file.log'
41+
@logger = Logger.new(@log_file)
42+
43+
clean_logfile
44+
45+
begin
46+
5 / 0
47+
rescue StandardError => e
48+
pretty_e = exception_to_array(e)
49+
logger.error(pretty_e)
50+
end
51+
```
52+
53+
## Installation
54+
55+
Add this line to your application's Gemfile:
56+
57+
```ruby
58+
gem 'unity-utils'
59+
```
60+
61+
And then execute:
62+
63+
$ bundle install
64+
65+
Or install it yourself as:
66+
67+
$ gem install unity-utils
68+
69+
## License
70+
71+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

lib/unity-utils.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'unity/utils'

lib/unity/modules/cli_modeable.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'ruby-progressbar'
4+
5+
module Unity
6+
module Modules
7+
module CliModeable
8+
private
9+
10+
def cli_mode?
11+
@cli_mode == true
12+
end
13+
14+
def init_progressbar(amount, title = self.class.to_s)
15+
return unless cli_mode?
16+
17+
@progressbar = ProgressBar.create(title: title, format: '%t, %c/%C,%e: |%B|')
18+
@progressbar.total = amount
19+
end
20+
21+
def incr_progressbar
22+
@progressbar.increment if cli_mode?
23+
end
24+
end
25+
end
26+
end

lib/unity/modules/loggable.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require 'logger'
4+
5+
module Unity
6+
module Modules
7+
module Logable
8+
attr_reader :log_file, :logger
9+
10+
def clean_logfile
11+
return if log_file.kind_of?(IO)
12+
13+
File.open(log_file, 'w') {}
14+
end
15+
16+
def puts_log_path
17+
$stdout.puts("All logs in #{log_file}")
18+
end
19+
20+
private
21+
22+
def exception_to_array(exception)
23+
[
24+
"exception=#{exception.class}",
25+
"msg='#{exception.message}'",
26+
"backtrace='#{exception.backtrace.first(4).join('; ')}'"
27+
]
28+
end
29+
end
30+
end
31+
end

lib/unity/utils.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
Dir['./lib/unity/**/**.rb'].sort.each { |file_path| require(file_path) }
4+
5+
module Unity
6+
module Utils
7+
class Error < StandardError; end
8+
end
9+
end

0 commit comments

Comments
 (0)