size_units 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82727be11a4559dc9b12d1dc8707ff1f6dc10aca
4
- data.tar.gz: 5453daf4b4de78e89c59b1c9204718e986c43920
3
+ metadata.gz: 1a765f51303a450e6e60a5bb1e5009c273aad584
4
+ data.tar.gz: 068d511c342285a8dae8b7d330ae2de76d418ac3
5
5
  SHA512:
6
- metadata.gz: 75bc86fda4b54f59748f757a986f1bb90e902687591996a2919ef0de004bb77b2b4248c19336a90064f9c9a2199a7510af6d4d0ed2ef7f2077dcefd5087317e7
7
- data.tar.gz: fdc21bb0f89bb3fcd9d6638c720ddfd800ee5d51f5b461eada442e842619edcb5b4d59bfe0a140297cb0acc749c1429acb82589a2f176e6a8ab5306a0760bf2d
6
+ metadata.gz: be58ef8b1ef279a0c61466b1a4e7aeb739e8445b22fc311a31db92883cd12eebd16640713e0c4a0afe5a34177d114b6156c687d5cd38f4c247ec742eb838feb7
7
+ data.tar.gz: ba832da95d1ea94fcf2f0e8e41d558090389534f9444e1c3160e30c69ed3b4bccdb0d9f70ff4698d1abb8326819a7b748e249f2b4da88f30c17813138f50fd08
data/Gemfile CHANGED
@@ -7,7 +7,6 @@ gem 'rake'
7
7
  group(:test) do
8
8
  gem 'schmurfy-bacon'
9
9
  gem 'mocha', '~> 0.10.0'
10
- gem 'factory_girl'
11
10
 
12
11
  gem 'simplecov'
13
12
  gem 'guard'
data/README.md CHANGED
@@ -18,5 +18,6 @@ It works the same way:
18
18
  Another activesupport alternative
19
19
 
20
20
  ```ruby
21
- 1500.human_size => "2 bytes"
21
+ 1500.human_size => "1KB, 476B"
22
+ 1500.human_size(1) => "1KB"
22
23
  ```
@@ -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 |(limit, field)|
30
- if t >= limit
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 -= limit
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|
@@ -1,3 +1,3 @@
1
1
  module SizeUnits
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
@@ -19,8 +19,14 @@ describe 'TimeDuration' do
19
19
  end
20
20
 
21
21
 
22
- should 'limite output' do
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.1
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: 2013-07-14 00:00:00.000000000 Z
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.3
55
+ rubygems_version: 2.2.0
55
56
  signing_key:
56
57
  specification_version: 4
57
58
  summary: Some size helpers