humanize-bytes 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f822f2ca03aaef2328c13d7695900d9a8c56a410
4
+ data.tar.gz: ec6452119bbedd0ed6bf35d78904c0a35fb29081
5
+ SHA512:
6
+ metadata.gz: 2ccc4c753f4801f4fb244d3c5b758e52b44d27d93552e7772415222c0682ce34ab56f9297fa52202de8e070cf2a174263c9585bec206678a0fda646e1a353051
7
+ data.tar.gz: 8bc447ae715d88ca0e1c7ceb7b93f184108cb94c2d9af77c1ac1f23df046aa0672b6ed061c004867fed50a6e0d669a13af61125c5c9152866689ab89b87c44d5
data/.gitignore CHANGED
@@ -3,6 +3,8 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .coveralls.yml
7
+ *.swp
6
8
  Gemfile.lock
7
9
  InstalledFiles
8
10
  _yardoc
@@ -12,6 +14,6 @@ lib/bundler/man
12
14
  pkg
13
15
  rdoc
14
16
  spec/reports
15
- test/tmp
16
- test/version_tmp
17
17
  tmp
18
+ .*~
19
+ *~
data/.rspec CHANGED
@@ -1 +1 @@
1
- --colour --format documentation
1
+ --colour --format documentation --profile
@@ -0,0 +1 @@
1
+ humanize-bytes
@@ -0,0 +1 @@
1
+ 2.1.2
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0
6
+ - 2.1.2
7
+ - jruby
File without changes
data/README.md CHANGED
@@ -18,11 +18,11 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- Initialize the New Object with
21
+ Initialize a New Byte Object with
22
22
 
23
- Humanize::Bytes.initialize(1024)
24
-
25
- The initialization process accept a second argument which is the unit. Available units are: b, k, m, g
23
+ ```ruby
24
+ Humanize::Byte.new(1024)
25
+ ```
26
26
 
27
27
  Then call one of the methods: to_b, to_k, to_m, to_g.
28
28
 
@@ -1,20 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/humanize-bytes/version', __FILE__)
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'humanize/bytes'
3
5
 
4
6
  Gem::Specification.new do |gem|
5
- gem.authors = ["Paulo Henrique Lopes Ribeiro"]
6
- gem.email = %q{plribeiro3000@gmail.com}
7
- gem.description = %q{Convert Byte, Kbyte, MByte, Gbyte into each other}
8
- gem.summary = %q{Humanize bytes Gem}
9
- gem.homepage = ""
7
+ gem.name = 'humanize-bytes'
8
+ gem.version = Humanize::Bytes::VERSION
9
+ gem.authors = %q{Paulo Henrique Lopes Ribeiro}
10
+ gem.email = %q{plribeiro3000@gmail.com}
11
+ gem.summary = %q{Convert Byte, Kbyte, MByte, Gbyte into each other}
10
12
 
11
13
  gem.files = `git ls-files`.split($\)
12
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "humanize-bytes"
14
+ gem.test_files = `git ls-files -- {test,spec,features,examples,gemfiles}/*`.split("\n")
15
+ gem.executables = `git ls-files -- bin/*`.split('\n').map{ |f| File.basename(f) }
15
16
  gem.require_paths = %w(lib)
16
- gem.version = Humanize::Bytes::VERSION
17
17
 
18
- gem.add_development_dependency "rake"
19
- gem.add_development_dependency "rspec", ">= 2.0.0"
18
+ gem.license = 'MIT'
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
20
22
  end
@@ -1,20 +1,7 @@
1
- require "humanize-bytes/version"
2
-
3
1
  module Humanize
4
- module Bytes
5
- autoload :Byte, 'humanize-bytes/byte'
6
- autoload :Kilo, 'humanize-bytes/kbyte'
7
- autoload :Mega, 'humanize-bytes/mbyte'
8
- autoload :Giga, 'humanize-bytes/gbyte'
9
-
10
- def self.initialize(value, unit = 'b')
11
- case unit.to_s
12
- when 'b' then Humanize::Bytes::Byte.new(value)
13
- when 'k' then Humanize::Bytes::Kilo.new(value)
14
- when 'm' then Humanize::Bytes::Mega.new(value)
15
- when 'g' then Humanize::Bytes::Giga.new(value)
16
- else nil
17
- end
18
- end
19
- end
2
+ autoload :Bytes, 'humanize/bytes'
3
+ autoload :Byte, 'humanize/byte'
4
+ autoload :Kilo, 'humanize/kilo'
5
+ autoload :Mega, 'humanize/mega'
6
+ autoload :Giga, 'humanize/giga'
20
7
  end
@@ -0,0 +1,32 @@
1
+ module Humanize
2
+ class Byte
3
+ def initialize(value)
4
+ @value = value
5
+ end
6
+
7
+ def value
8
+ @value
9
+ end
10
+
11
+ def to_b
12
+ self
13
+ end
14
+
15
+ def to_k
16
+ Kilo.new @value / 1024.0
17
+ end
18
+
19
+ def to_m
20
+ Mega.new @value / 1024.0 / 1024
21
+ end
22
+
23
+ def to_g
24
+ Giga.new @value / 1024.0 / 1024 / 1024
25
+ end
26
+
27
+ def to_s(options = {})
28
+ size = options.fetch(:decimal_digits, value.to_s.size)
29
+ ("%.#{size}f" % value).to_f
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Humanize
2
+ class Bytes
3
+ VERSION = '2.0.0'
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ module Humanize
2
+ class Giga
3
+ def initialize(value)
4
+ @value = value
5
+ end
6
+
7
+ def value
8
+ @value
9
+ end
10
+
11
+ def to_b
12
+ Byte.new @value * 1024 * 1024 * 1024
13
+ end
14
+
15
+ def to_k
16
+ Kilo.new @value * 1024 * 1024
17
+ end
18
+
19
+ def to_m
20
+ Mega.new @value * 1024
21
+ end
22
+
23
+ def to_g
24
+ self
25
+ end
26
+
27
+ def to_s(options = {})
28
+ size = options.fetch(:decimal_digits, value.to_s.size)
29
+ ("%.#{size}f" % value).to_f
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module Humanize
2
+ class Kilo
3
+ def initialize(value)
4
+ @value = value
5
+ end
6
+
7
+ def value
8
+ @value
9
+ end
10
+
11
+ def to_b
12
+ Byte.new @value * 1024
13
+ end
14
+
15
+ def to_k
16
+ self
17
+ end
18
+
19
+ def to_m
20
+ Mega.new @value / 1024.0
21
+ end
22
+
23
+ def to_g
24
+ Giga.new @value / 1024.0 / 1024
25
+ end
26
+
27
+ def to_s(options = {})
28
+ size = options.fetch(:decimal_digits, value.to_s.size)
29
+ ("%.#{size}f" % value).to_f
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module Humanize
2
+ class Mega
3
+ def initialize(value)
4
+ @value = value
5
+ end
6
+
7
+ def value
8
+ @value
9
+ end
10
+
11
+ def to_b
12
+ Byte.new @value * 1024 * 1024
13
+ end
14
+
15
+ def to_k
16
+ Kilo.new @value * 1024
17
+ end
18
+
19
+ def to_m
20
+ self
21
+ end
22
+
23
+ def to_g
24
+ Giga.new @value / 1024.0
25
+ end
26
+
27
+ def to_s(options = {})
28
+ size = options.fetch(:decimal_digits, value.to_s.size)
29
+ ("%.#{size}f" % value).to_f
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Humanize::Byte do
4
+ let(:b) { Humanize::Byte.new(2147843648) }
5
+
6
+ context "#value" do
7
+ it "should return the value" do
8
+ expect(b.value).to eq(2147843648)
9
+ end
10
+ end
11
+
12
+ context "#to_b" do
13
+ it "should return return self" do
14
+ expect(b.to_b).to be b
15
+ end
16
+ end
17
+
18
+ context "#to_k" do
19
+ it "should convert to Kilo bytes" do
20
+ expect(b.to_k).to be_an_instance_of(Humanize::Kilo)
21
+ end
22
+ it "should convert the value to Kilo bytes" do
23
+ expect(b.to_k.value).to eq(2097503.5625)
24
+ end
25
+ end
26
+
27
+ context "#to_m" do
28
+ it "should convert to Mega bytes" do
29
+ expect(b.to_m).to be_an_instance_of(Humanize::Mega)
30
+ end
31
+ it "should convert the value to Mega bytes" do
32
+ expect(b.to_m.value).to eq(2048.3433227539062)
33
+ end
34
+ end
35
+
36
+ context "#to_g" do
37
+ it "should convert to Giga bytes" do
38
+ expect(b.to_g).to be_an_instance_of(Humanize::Giga)
39
+ end
40
+ it "should convert the value to Giga bytes" do
41
+ expect(b.to_g.value).to eq(2.0003352761268616)
42
+ end
43
+ end
44
+
45
+ context "#to_s" do
46
+ context 'without any specification' do
47
+ it "should return a float with all digits" do
48
+ expect(b.to_s).to eq(2147843648.0)
49
+ end
50
+ end
51
+
52
+ context 'with decimal_digits specified' do
53
+ before :each do
54
+ @b = Humanize::Byte.new(2147843648.2345)
55
+ end
56
+
57
+ it "should return a float with specified digits" do
58
+ expect(@b.to_s(:decimal_digits => 2)).to eq(2147843648.23)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Humanize::Giga do
4
+ let(:g) { Humanize::Giga.new(3.25) }
5
+
6
+ context "#value" do
7
+ it "should return the value" do
8
+ expect(g.value).to eq(3.25)
9
+ end
10
+ end
11
+
12
+ context "#to_b" do
13
+ it "should convert to Bytes" do
14
+ expect(g.to_b).to be_an_instance_of(Humanize::Byte)
15
+ end
16
+ it "should convert the value to Byte" do
17
+ expect(g.to_b.value).to eq(3489660928)
18
+ end
19
+ end
20
+
21
+ context "#to_k" do
22
+ it "should convert to Kilo bytes" do
23
+ expect(g.to_k).to be_an_instance_of(Humanize::Kilo)
24
+ end
25
+ it "should convert the value to Kilo bytes" do
26
+ expect(g.to_k.value).to eq(3407872)
27
+ end
28
+ end
29
+
30
+ context "#to_m" do
31
+ it "should convert to Mega bytes" do
32
+ expect(g.to_m).to be_an_instance_of(Humanize::Mega)
33
+ end
34
+ it "should convert the value to Mega bytes" do
35
+ expect(g.to_m.value).to eq(3328)
36
+ end
37
+ end
38
+
39
+ context "#to_g" do
40
+ it "should return self" do
41
+ expect(g.to_g).to be g
42
+ end
43
+ end
44
+
45
+ context "#to_s" do
46
+ context 'without any specification' do
47
+ it "should return a float with all digits" do
48
+ expect(g.to_s).to eq(3.25)
49
+ end
50
+ end
51
+
52
+ context 'with decimal_digits specified' do
53
+ before :each do
54
+ @g = Humanize::Giga.new 3.22
55
+ end
56
+
57
+ it "should return a float with specified digits" do
58
+ expect(@g.to_s(:decimal_digits => 1)).to eq(3.2)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Humanize::Kilo do
4
+ let(:k) { Humanize::Kilo.new(5000) }
5
+
6
+ context "#value" do
7
+ it "should return the value" do
8
+ expect(k.value).to eq(5000)
9
+ end
10
+ end
11
+
12
+ context "#to_b" do
13
+ it "should convert to Byte" do
14
+ expect(k.to_b).to be_an_instance_of(Humanize::Byte)
15
+ end
16
+ it "should convert the value to Bytes" do
17
+ expect(k.to_b.value).to eq(5120000)
18
+ end
19
+ end
20
+
21
+ context "#to_k" do
22
+ it "should return self" do
23
+ expect(k.to_k).to be k
24
+ end
25
+ end
26
+
27
+ context "#to_m" do
28
+ it "should convert to Mega bytes" do
29
+ expect(k.to_m).to be_an_instance_of(Humanize::Mega)
30
+ end
31
+ it "should convert the value to Mega bytes" do
32
+ expect(k.to_m.value).to eq(4.8828125)
33
+ end
34
+ end
35
+
36
+ context "#to_g" do
37
+ it "should convert to Giga bytes" do
38
+ expect(k.to_g).to be_an_instance_of(Humanize::Giga)
39
+ end
40
+ it "should convert the value to Giga bytes" do
41
+ expect(k.to_g.value).to eq(0.00476837158203125)
42
+ end
43
+ end
44
+
45
+ context "#to_s" do
46
+ context 'without any specification' do
47
+ it "should return a float with all digits" do
48
+ expect(k.to_s).to eq(5000.0)
49
+ end
50
+ end
51
+
52
+ context 'with decimal_digits specified' do
53
+ before :each do
54
+ @k = Humanize::Kilo.new(2147843648.2345)
55
+ end
56
+
57
+ it "should return a float with specified digits" do
58
+ expect(@k.to_s(:decimal_digits => 1)).to eq(2147843648.2)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Humanize::Mega do
4
+ let(:m) { Humanize::Mega.new(4.8828125) }
5
+
6
+ context "#value" do
7
+ it "should return the value" do
8
+ expect(m.value).to eq(4.8828125)
9
+ end
10
+ end
11
+
12
+ context "#to_b" do
13
+ it "should convert to Byte" do
14
+ expect(m.to_b).to be_an_instance_of(Humanize::Byte)
15
+ end
16
+ it "should convert the value to Bytes" do
17
+ expect(m.to_b.value).to eq(5120000)
18
+ end
19
+ end
20
+
21
+ context "#to_k" do
22
+ it "should convert to Kilo bytes" do
23
+ expect(m.to_k).to be_an_instance_of(Humanize::Kilo)
24
+ end
25
+ it "should convert the value to Kilo bytes" do
26
+ expect(m.to_k.value).to eq(5000)
27
+ end
28
+ end
29
+
30
+ context "#to_m" do
31
+ it "should return self" do
32
+ expect(m.to_m).to be m
33
+ end
34
+ end
35
+
36
+ context "#to_g" do
37
+ it "should convert to Giga bytes" do
38
+ expect(m.to_g).to be_an_instance_of(Humanize::Giga)
39
+ end
40
+ it "should convert the value to Giga bytes" do
41
+ expect(m.to_g.value).to eq(0.00476837158203125)
42
+ end
43
+ end
44
+
45
+ context "#to_s" do
46
+ context 'without any specification' do
47
+ it "should return a float with all digits" do
48
+ expect(m.to_s).to eq(4.8828125)
49
+ end
50
+ end
51
+
52
+ context 'with decimal_digits specified' do
53
+ it "should return a float with specified digits" do
54
+ expect(m.to_s(:decimal_digits => 1)).to eq(4.9)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,6 +1,4 @@
1
1
  require "rubygems"
2
2
  require "rspec"
3
3
 
4
- require File.dirname(__FILE__) + "/../lib/humanize-bytes"
5
-
6
- include Humanize::Bytes
4
+ require "#{File.dirname(__FILE__)}/../lib/humanize-bytes"
metadata CHANGED
@@ -1,108 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humanize-bytes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 2.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Paulo Henrique Lopes Ribeiro
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-09-03 00:00:00.000000000 Z
11
+ date: 2014-08-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
- version: 2.0.0
33
+ version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
- version: 2.0.0
46
- description: Convert Byte, Kbyte, MByte, Gbyte into each other
40
+ version: '0'
41
+ description:
47
42
  email: plribeiro3000@gmail.com
48
43
  executables: []
49
44
  extensions: []
50
45
  extra_rdoc_files: []
51
46
  files:
52
- - .gitignore
53
- - .rspec
54
- - .rvmrc
47
+ - ".gitignore"
48
+ - ".rspec"
49
+ - ".ruby-gemset"
50
+ - ".ruby-version"
51
+ - ".travis.yml"
55
52
  - Gemfile
56
- - LICENSE
53
+ - LICENSE.md
57
54
  - README.md
58
55
  - Rakefile
59
56
  - humanize-bytes.gemspec
60
57
  - lib/humanize-bytes.rb
61
- - lib/humanize-bytes/byte.rb
62
- - lib/humanize-bytes/gbyte.rb
63
- - lib/humanize-bytes/kbyte.rb
64
- - lib/humanize-bytes/mbyte.rb
65
- - lib/humanize-bytes/version.rb
66
- - spec/humanize_bytes/byte_spec.rb
67
- - spec/humanize_bytes/gbyte_spec.rb
68
- - spec/humanize_bytes/kbyte_spec.rb
69
- - spec/humanize_bytes/mbyte_spec.rb
70
- - spec/humanize_bytes_spec.rb
58
+ - lib/humanize/byte.rb
59
+ - lib/humanize/bytes.rb
60
+ - lib/humanize/giga.rb
61
+ - lib/humanize/kilo.rb
62
+ - lib/humanize/mega.rb
63
+ - spec/humanize/byte_spec.rb
64
+ - spec/humanize/giga_spec.rb
65
+ - spec/humanize/kilo_spec.rb
66
+ - spec/humanize/mega_spec.rb
71
67
  - spec/spec_helper.rb
72
- homepage: ''
73
- licenses: []
68
+ homepage:
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
74
72
  post_install_message:
75
73
  rdoc_options: []
76
74
  require_paths:
77
75
  - lib
78
76
  required_ruby_version: !ruby/object:Gem::Requirement
79
- none: false
80
77
  requirements:
81
- - - ! '>='
78
+ - - ">="
82
79
  - !ruby/object:Gem::Version
83
80
  version: '0'
84
- segments:
85
- - 0
86
- hash: 2054005163981464363
87
81
  required_rubygems_version: !ruby/object:Gem::Requirement
88
- none: false
89
82
  requirements:
90
- - - ! '>='
83
+ - - ">="
91
84
  - !ruby/object:Gem::Version
92
85
  version: '0'
93
- segments:
94
- - 0
95
- hash: 2054005163981464363
96
86
  requirements: []
97
87
  rubyforge_project:
98
- rubygems_version: 1.8.24
88
+ rubygems_version: 2.2.2
99
89
  signing_key:
100
- specification_version: 3
101
- summary: Humanize bytes Gem
90
+ specification_version: 4
91
+ summary: Convert Byte, Kbyte, MByte, Gbyte into each other
102
92
  test_files:
103
- - spec/humanize_bytes/byte_spec.rb
104
- - spec/humanize_bytes/gbyte_spec.rb
105
- - spec/humanize_bytes/kbyte_spec.rb
106
- - spec/humanize_bytes/mbyte_spec.rb
107
- - spec/humanize_bytes_spec.rb
93
+ - spec/humanize/byte_spec.rb
94
+ - spec/humanize/giga_spec.rb
95
+ - spec/humanize/kilo_spec.rb
96
+ - spec/humanize/mega_spec.rb
108
97
  - spec/spec_helper.rb
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use 1.9.3-p194@humanize_bytes --create
@@ -1,39 +0,0 @@
1
- module Humanize
2
- module Bytes
3
- class Byte
4
- def initialize(value)
5
- @value = value
6
- end
7
-
8
- def value
9
- @value
10
- end
11
-
12
- def to_b
13
- self
14
- end
15
-
16
- def to_k
17
- Kilo.new @value / 1024.0
18
- end
19
-
20
- def to_m
21
- Mega.new @value / 1024.0 / 1024
22
- end
23
-
24
- def to_g
25
- Giga.new @value / 1024.0 / 1024 / 1024
26
- end
27
-
28
- def to_s
29
- @value.instance_of?(Float) ? formatted_float + ' bytes' : @value.to_s + ' bytes'
30
- end
31
-
32
- protected
33
-
34
- def formatted_float
35
- @value.to_s[0 .. @value.to_s.index('.') + 2]
36
- end
37
- end
38
- end
39
- end
@@ -1,39 +0,0 @@
1
- module Humanize
2
- module Bytes
3
- class Giga
4
- def initialize(value)
5
- @value = value
6
- end
7
-
8
- def value
9
- @value
10
- end
11
-
12
- def to_b
13
- Byte.new @value * 1024 * 1024 * 1024
14
- end
15
-
16
- def to_k
17
- Kilo.new @value * 1024 * 1024
18
- end
19
-
20
- def to_m
21
- Mega.new @value * 1024
22
- end
23
-
24
- def to_g
25
- self
26
- end
27
-
28
- def to_s
29
- @value.instance_of?(Float) ? formatted_float + ' giga bytes' : @value.to_s + ' giga bytes'
30
- end
31
-
32
- protected
33
-
34
- def formatted_float
35
- @value.to_s[0 .. @value.to_s.index('.') + 2]
36
- end
37
- end
38
- end
39
- end
@@ -1,39 +0,0 @@
1
- module Humanize
2
- module Bytes
3
- class Kilo
4
- def initialize(value)
5
- @value = value
6
- end
7
-
8
- def value
9
- @value
10
- end
11
-
12
- def to_b
13
- Byte.new @value * 1024
14
- end
15
-
16
- def to_k
17
- self
18
- end
19
-
20
- def to_m
21
- Mega.new @value / 1024.0
22
- end
23
-
24
- def to_g
25
- Giga.new @value / 1024.0 / 1024
26
- end
27
-
28
- def to_s
29
- @value.instance_of?(Float) ? formatted_float + ' kilo bytes' : @value.to_s + ' kilo bytes'
30
- end
31
-
32
- protected
33
-
34
- def formatted_float
35
- @value.to_s[0 .. @value.to_s.index('.') + 2]
36
- end
37
- end
38
- end
39
- end
@@ -1,39 +0,0 @@
1
- module Humanize
2
- module Bytes
3
- class Mega
4
- def initialize(value)
5
- @value = value
6
- end
7
-
8
- def value
9
- @value
10
- end
11
-
12
- def to_b
13
- Byte.new @value * 1024 * 1024
14
- end
15
-
16
- def to_k
17
- Kilo.new @value * 1024
18
- end
19
-
20
- def to_m
21
- self
22
- end
23
-
24
- def to_g
25
- Giga.new @value / 1024.0
26
- end
27
-
28
- def to_s
29
- @value.instance_of?(Float) ? formatted_float + ' mega bytes' : @value.to_s + ' mega bytes'
30
- end
31
-
32
- protected
33
-
34
- def formatted_float
35
- @value.to_s[0 .. @value.to_s.index('.') + 2]
36
- end
37
- end
38
- end
39
- end
@@ -1,5 +0,0 @@
1
- module Humanize
2
- module Bytes
3
- VERSION = "1.0.0"
4
- end
5
- end
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Humanize::Bytes::Byte do
4
- let(:b) { Humanize::Bytes::Byte.new(2147843648) }
5
-
6
- context "#value" do
7
- it "should return the value" do
8
- b.value.should == 2147843648
9
- end
10
- end
11
-
12
- context "#to_b" do
13
- it "should return return self" do
14
- b.to_b.should be b
15
- end
16
- end
17
-
18
- context "#to_k" do
19
- it "should convert to Kilo bytes" do
20
- b.to_k.class.should be Kilo
21
- end
22
- it "should convert the value to Kilo bytes" do
23
- b.to_k.value.should == 2097503.5625
24
- end
25
- end
26
-
27
- context "#to_m" do
28
- it "should convert to Mega bytes" do
29
- b.to_m.class.should be Mega
30
- end
31
- it "should convert the value to Mega bytes" do
32
- b.to_m.value.should == 2048.3433227539062
33
- end
34
- end
35
-
36
- context "#to_g" do
37
- it "should convert to Giga bytes" do
38
- b.to_g.class.should be Giga
39
- end
40
- it "should convert the value to Giga bytes" do
41
- b.to_g.value.should == 2.0003352761268616
42
- end
43
- end
44
-
45
- context "#to_s" do
46
- context "when value is an integer" do
47
- it "should print a humanized version of the value" do
48
- b.to_s.should == '2147843648 bytes'
49
- end
50
- end
51
-
52
- context "when value is an integer" do
53
- it "should print a humanized version of the value" do
54
- Humanize::Bytes::Byte.new(4.997).to_s.should == '4.99 bytes'
55
- end
56
- end
57
- end
58
- end
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Humanize::Bytes::Giga do
4
- let(:g) { Humanize::Bytes::Giga.new(3.25) }
5
-
6
- context "#value" do
7
- it "should return the value" do
8
- g.value.should == 3.25
9
- end
10
- end
11
-
12
- context "#to_b" do
13
- it "should convert to Bytes" do
14
- g.to_b.class.should be Byte
15
- end
16
- it "should convert the value to Byte" do
17
- g.to_b.value.should == 3489660928
18
- end
19
- end
20
-
21
- context "#to_k" do
22
- it "should convert to Kilo bytes" do
23
- g.to_k.class.should be Kilo
24
- end
25
- it "should convert the value to Kilo bytes" do
26
- g.to_k.value.should == 3407872
27
- end
28
- end
29
-
30
- context "#to_m" do
31
- it "should convert to Mega bytes" do
32
- g.to_m.class.should be Mega
33
- end
34
- it "should convert the value to Mega bytes" do
35
- g.to_m.value.should == 3328
36
- end
37
- end
38
-
39
- context "#to_g" do
40
- it "should return self" do
41
- g.to_g.should be g
42
- end
43
- end
44
-
45
- context "#to_s" do
46
- context "when value is a decimal" do
47
- it "should print a humanized version of the value" do
48
- g.to_s.should == '3.25 giga bytes'
49
- end
50
- end
51
-
52
- context "when value is an integer" do
53
- it "should print a humanized version of the value" do
54
- Humanize::Bytes::Giga.new(4).to_s.should == '4 giga bytes'
55
- end
56
- end
57
- end
58
- end
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Humanize::Bytes::Kilo do
4
- let(:k) { Humanize::Bytes::Kilo.new(5000) }
5
-
6
- context "#value" do
7
- it "should return the value" do
8
- k.value.should == 5000
9
- end
10
- end
11
-
12
- context "#to_b" do
13
- it "should convert to Byte" do
14
- k.to_b.class.should be Byte
15
- end
16
- it "should convert the value to Bytes" do
17
- k.to_b.value.should == 5120000
18
- end
19
- end
20
-
21
- context "#to_k" do
22
- it "should return self" do
23
- k.to_k.should be k
24
- end
25
- end
26
-
27
- context "#to_m" do
28
- it "should convert to Mega bytes" do
29
- k.to_m.class.should be Mega
30
- end
31
- it "should convert the value to Mega bytes" do
32
- k.to_m.value.should == 4.8828125
33
- end
34
- end
35
-
36
- context "#to_g" do
37
- it "should convert to Giga bytes" do
38
- k.to_g.class.should be Giga
39
- end
40
- it "should convert the value to Giga bytes" do
41
- k.to_g.value.should == 0.00476837158203125
42
- end
43
- end
44
-
45
- context "#to_s" do
46
- context "when value is an integer" do
47
- it "should print a humanized version of the value" do
48
- k.to_s.should == '5000 kilo bytes'
49
- end
50
- end
51
-
52
- context "when value is an integer" do
53
- it "should print a humanized version of the value" do
54
- Humanize::Bytes::Kilo.new(4.997).to_s.should == '4.99 kilo bytes'
55
- end
56
- end
57
- end
58
- end
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Humanize::Bytes::Mega do
4
- let(:m) { Humanize::Bytes::Mega.new(4.8828125) }
5
-
6
- context "#value" do
7
- it "should return the value" do
8
- m.value.should == 4.8828125
9
- end
10
- end
11
-
12
- context "#to_b" do
13
- it "should convert to Byte" do
14
- m.to_b.class.should be Byte
15
- end
16
- it "should convert the value to Bytes" do
17
- m.to_b.value.should == 5120000
18
- end
19
- end
20
-
21
- context "#to_k" do
22
- it "should convert to Kilo bytes" do
23
- m.to_k.class.should be Kilo
24
- end
25
- it "should convert the value to Kilo bytes" do
26
- m.to_k.value.should == 5000
27
- end
28
- end
29
-
30
- context "#to_m" do
31
- it "should return self" do
32
- m.to_m.should be m
33
- end
34
- end
35
-
36
- context "#to_g" do
37
- it "should convert to Giga bytes" do
38
- m.to_g.class.should be Giga
39
- end
40
- it "should convert the value to Giga bytes" do
41
- m.to_g.value.should == 0.00476837158203125
42
- end
43
- end
44
-
45
- context "#to_s" do
46
- context "when value is a decimal" do
47
- it "should print a humanized version of the value" do
48
- m.to_s.should == '4.88 mega bytes'
49
- end
50
- end
51
-
52
- context "when value is an integer" do
53
- it "should print a humanized version of the value" do
54
- Humanize::Bytes::Mega.new(4).to_s.should == '4 mega bytes'
55
- end
56
- end
57
- end
58
- end
@@ -1,65 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Humanize::Bytes do
4
- describe "#initialize" do
5
- context "when the second parameter is nil" do
6
- it "should return a new Byte instance" do
7
- Humanize::Bytes.initialize(1024).should be_instance_of(Humanize::Bytes::Byte)
8
- end
9
- end
10
-
11
- context "when the second parameter is 'b'" do
12
- it "should return a new Kilo instance" do
13
- Humanize::Bytes.initialize(1024, 'b').should be_instance_of(Humanize::Bytes::Byte)
14
- end
15
- end
16
-
17
- context "when the second parameter is 'k'" do
18
- it "should return a new Kilo instance" do
19
- Humanize::Bytes.initialize(1024, 'k').should be_instance_of(Humanize::Bytes::Kilo)
20
- end
21
- end
22
-
23
- context "when the second parameter is 'm'" do
24
- it "should return a new Mega instance" do
25
- Humanize::Bytes.initialize(1024, 'm').should be_instance_of(Humanize::Bytes::Mega)
26
- end
27
- end
28
-
29
- context "when the second parameter is 'g'" do
30
- it "should return a new Giga instance" do
31
- Humanize::Bytes.initialize(1024, 'g').should be_instance_of(Humanize::Bytes::Giga)
32
- end
33
- end
34
-
35
- context "when the second parameter is :b" do
36
- it "should return a new Kilo instance" do
37
- Humanize::Bytes.initialize(1024, :b).should be_instance_of(Humanize::Bytes::Byte)
38
- end
39
- end
40
-
41
- context "when the second parameter is :k" do
42
- it "should return a new Kilo instance" do
43
- Humanize::Bytes.initialize(1024, :k).should be_instance_of(Humanize::Bytes::Kilo)
44
- end
45
- end
46
-
47
- context "when the second parameter is :m" do
48
- it "should return a new Mega instance" do
49
- Humanize::Bytes.initialize(1024, :m).should be_instance_of(Humanize::Bytes::Mega)
50
- end
51
- end
52
-
53
- context "when the second parameter is :g" do
54
- it "should return a new Giga instance" do
55
- Humanize::Bytes.initialize(1024, :g).should be_instance_of(Humanize::Bytes::Giga)
56
- end
57
- end
58
-
59
- context "when the second parameter is any other thing" do
60
- it "should return nil" do
61
- Humanize::Bytes.initialize(1024, :thing).should be_nil
62
- end
63
- end
64
- end
65
- end