able 0.1.3 → 0.1.4
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 +4 -4
- data/README.md +9 -1
- data/lib/able.rb +4 -0
- data/lib/able/version.rb +1 -1
- data/lib/ext/integer.rb +13 -0
- data/lib/ext/kernel.rb +53 -0
- data/lib/ext/string.rb +12 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 375a99d24d70203258f5d235ade3088182097302
|
4
|
+
data.tar.gz: afecad7c5c6e6b015825221d5a0a3ac56625d28f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca3a53d44651d77710fd9bb45b43c172009d64bf47787c5952c089a156385cdb337fb9eff6dd8bae2fb62d4b858a891e0e2612beb7e00f409db583d52050afdc
|
7
|
+
data.tar.gz: 006295e17471daa7be685f12d5f404e012a30964a9b12313b01a06807e5cf59376b184b6c735aae69448d6cff6e371c57df0614ea252bec2eefa1b0ece0eaa59
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Able
|
2
2
|
|
3
|
-
This gem
|
3
|
+
This gem contains pieces of code I copy-past from one project to another one.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,6 +20,8 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
###Concerns
|
24
|
+
|
23
25
|
1. [Emailable](https://github.com/entity1991/able/tree/master/lib/able/emailable.rb)
|
24
26
|
2. [Fullnameable](https://github.com/entity1991/able/tree/master/lib/able/fullnameable.rb)
|
25
27
|
`full_name` method
|
@@ -33,6 +35,12 @@ Or install it yourself as:
|
|
33
35
|
end
|
34
36
|
```
|
35
37
|
|
38
|
+
### Ruby class extensions
|
39
|
+
|
40
|
+
1. [Integer](https://github.com/entity1991/able/tree/master/lib/ext/integer.rb)
|
41
|
+
1. [Kernel](https://github.com/entity1991/able/tree/master/lib/ext/kernel.rb)
|
42
|
+
1. [String](https://github.com/entity1991/able/tree/master/lib/ext/string.rb)
|
43
|
+
|
36
44
|
## Contributing
|
37
45
|
|
38
46
|
Bug reports and pull requests are welcome on GitHub at [https://github.com/entity1991/able](https://github.com/entity1991/able).
|
data/lib/able.rb
CHANGED
data/lib/able/version.rb
CHANGED
data/lib/ext/integer.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
class Integer
|
2
|
+
def to_filesize
|
3
|
+
{
|
4
|
+
'B' => 1024,
|
5
|
+
'KB' => 1024 * 1024,
|
6
|
+
'MB' => 1024 * 1024 * 1024,
|
7
|
+
'GB' => 1024 * 1024 * 1024 * 1024,
|
8
|
+
'TB' => 1024 * 1024 * 1024 * 1024 * 1024
|
9
|
+
}.each_pair { |e, s| return "#{(self.to_f / (s / 1024)).round(2)} #{e}" if self < s }
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :to_file_size, :to_filesize
|
13
|
+
end
|
data/lib/ext/kernel.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module Kernel
|
2
|
+
|
3
|
+
# retrying code 'attempt' times with delay 'attempt' ** 2. If last attempt failed - returns nil.
|
4
|
+
def try_to(attempt = 1, &block)
|
5
|
+
begin
|
6
|
+
yield
|
7
|
+
rescue => exception
|
8
|
+
if attempt < 5
|
9
|
+
sleep(attempt ** 2)
|
10
|
+
try_to(attempt + 1, &block)
|
11
|
+
else
|
12
|
+
Airbrake.notify(exception, error_message: "Exception from try_to block: #{exception.message}")
|
13
|
+
return nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# returns spent time in milliseconds
|
19
|
+
def track_time(&block)
|
20
|
+
start = Time.now
|
21
|
+
yield block
|
22
|
+
(Time.now - start).round(3) * 1000
|
23
|
+
end
|
24
|
+
|
25
|
+
# Is good for rake tasks, migrations etc.
|
26
|
+
def each_with_progress(collection)
|
27
|
+
progress = 0
|
28
|
+
|
29
|
+
all_count = collection.count
|
30
|
+
puts_with_time "Found before: #{all_count} items."
|
31
|
+
i = 0
|
32
|
+
|
33
|
+
collection.find_each(batch_size: 1000) do |item|
|
34
|
+
yield item
|
35
|
+
|
36
|
+
i = i + 1
|
37
|
+
|
38
|
+
current_progress = ((i * 100) / all_count.to_f).floor
|
39
|
+
if current_progress > progress
|
40
|
+
progress = current_progress
|
41
|
+
puts_with_time "Done #{progress}%. #{i} of #{all_count} records."
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
puts_with_time "Left after: #{collection.count} items."
|
47
|
+
end
|
48
|
+
|
49
|
+
def puts_with_time(string)
|
50
|
+
puts "#{Time.now.utc.strftime('%F %T(%Z)')}. #{string}"
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/lib/ext/string.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: able
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- entity
|
@@ -75,6 +75,9 @@ files:
|
|
75
75
|
- lib/able/fullnameable.rb
|
76
76
|
- lib/able/tokenizer.rb
|
77
77
|
- lib/able/version.rb
|
78
|
+
- lib/ext/integer.rb
|
79
|
+
- lib/ext/kernel.rb
|
80
|
+
- lib/ext/string.rb
|
78
81
|
homepage: https://github.com/entity1991/able
|
79
82
|
licenses:
|
80
83
|
- MIT
|