size_units 0.0.1 → 0.0.2
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/Gemfile +0 -1
- data/README.md +2 -1
- data/lib/size_units/size.rb +24 -5
- data/lib/size_units/version.rb +1 -1
- data/size_units.gemspec +1 -0
- data/specs/unit/size_spec.rb +8 -2
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a765f51303a450e6e60a5bb1e5009c273aad584
|
4
|
+
data.tar.gz: 068d511c342285a8dae8b7d330ae2de76d418ac3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be58ef8b1ef279a0c61466b1a4e7aeb739e8445b22fc311a31db92883cd12eebd16640713e0c4a0afe5a34177d114b6156c687d5cd38f4c247ec742eb838feb7
|
7
|
+
data.tar.gz: ba832da95d1ea94fcf2f0e8e41d558090389534f9444e1c3160e30c69ed3b4bccdb0d9f70ff4698d1abb8326819a7b748e249f2b4da88f30c17813138f50fd08
|
data/Gemfile
CHANGED
data/README.md
CHANGED
data/lib/size_units/size.rb
CHANGED
@@ -20,22 +20,41 @@ module HumanSize
|
|
20
20
|
#
|
21
21
|
# @return [String] the formatted duration
|
22
22
|
#
|
23
|
-
def human_size(limit = nil)
|
23
|
+
def human_size(limit = nil, round = true)
|
24
24
|
ret = []
|
25
25
|
diff = Hash.new(0)
|
26
26
|
t = self.to_i
|
27
|
-
|
27
|
+
|
28
|
+
highest_used_unit = nil
|
29
|
+
|
28
30
|
while t > 0
|
29
|
-
HumanSize.units.each do |(
|
30
|
-
if t >=
|
31
|
+
HumanSize.units.each.with_index do |(max_size, field), n|
|
32
|
+
if t >= max_size
|
33
|
+
highest_used_unit ||= n
|
31
34
|
diff[field] += 1
|
32
|
-
t -=
|
35
|
+
t -= max_size
|
33
36
|
break
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
37
40
|
end
|
38
41
|
|
42
|
+
if limit && round
|
43
|
+
|
44
|
+
tmp = 0
|
45
|
+
(HumanSize.units.size - 1).downto(highest_used_unit + 1) do |n|
|
46
|
+
max_size, unit_name, _ = *HumanSize.units[n]
|
47
|
+
if diff[unit_name] > 0
|
48
|
+
tmp += 1
|
49
|
+
if diff[unit_name] > 512
|
50
|
+
# increment next field
|
51
|
+
_, next_unit_name, _ = *HumanSize.units[n - 1]
|
52
|
+
diff[next_unit_name] += 1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
39
58
|
ret = []
|
40
59
|
|
41
60
|
HumanSize.units.each.with_index do |unit|
|
data/lib/size_units/version.rb
CHANGED
data/size_units.gemspec
CHANGED
@@ -10,6 +10,7 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.files = `git ls-files`.split($\)
|
11
11
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
12
|
gem.name = "size_units"
|
13
|
+
gem.license = 'MIT'
|
13
14
|
gem.require_paths = ["lib"]
|
14
15
|
gem.version = SizeUnits::VERSION
|
15
16
|
end
|
data/specs/unit/size_spec.rb
CHANGED
@@ -19,8 +19,14 @@ describe 'TimeDuration' do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
|
22
|
-
should '
|
23
|
-
(2*@gb1 + 200*@mb1 + 780).human_size(1).should == "2GB"
|
22
|
+
should 'limit output' do
|
23
|
+
(2*@gb1 + 200*@mb1 + 780).human_size(1, false).should == "2GB"
|
24
|
+
end
|
25
|
+
|
26
|
+
should 'limit and round output' do
|
27
|
+
s = 8157.megabytes
|
28
|
+
s.human_size(1, true).should == "8GB"
|
29
|
+
s.human_size(1, false).should == "7GB"
|
24
30
|
end
|
25
31
|
|
26
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: size_units
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julien Ammous
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -17,7 +17,7 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- .gitignore
|
20
|
+
- ".gitignore"
|
21
21
|
- ChangeLog
|
22
22
|
- Gemfile
|
23
23
|
- Guardfile
|
@@ -33,7 +33,8 @@ files:
|
|
33
33
|
- specs/unit/size_spec.rb
|
34
34
|
- specs/unit/units_spec.rb
|
35
35
|
homepage: https://github.com/schmurfy/size_units
|
36
|
-
licenses:
|
36
|
+
licenses:
|
37
|
+
- MIT
|
37
38
|
metadata: {}
|
38
39
|
post_install_message:
|
39
40
|
rdoc_options: []
|
@@ -41,17 +42,17 @@ require_paths:
|
|
41
42
|
- lib
|
42
43
|
required_ruby_version: !ruby/object:Gem::Requirement
|
43
44
|
requirements:
|
44
|
-
- -
|
45
|
+
- - ">="
|
45
46
|
- !ruby/object:Gem::Version
|
46
47
|
version: '0'
|
47
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
49
|
requirements:
|
49
|
-
- -
|
50
|
+
- - ">="
|
50
51
|
- !ruby/object:Gem::Version
|
51
52
|
version: '0'
|
52
53
|
requirements: []
|
53
54
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.0
|
55
|
+
rubygems_version: 2.2.0
|
55
56
|
signing_key:
|
56
57
|
specification_version: 4
|
57
58
|
summary: Some size helpers
|