humanize-bytes 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -19,15 +19,12 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  Initialize the New Object with
22
- Humanize::Bytes.initialize(1024)
23
22
 
24
- The initialization process accept a second argument which is the unit. Available units are: b, k, m, g
25
-
26
- Then call on of the methods: to_b, to_k, to_m, to_g.
23
+ Humanize::Bytes.initialize(1024)
27
24
 
28
- ## Note
25
+ The initialization process accept a second argument which is the unit. Available units are: b, k, m, g
29
26
 
30
- The available methods are different depending on the initialization.
27
+ Then call one of the methods: to_b, to_k, to_m, to_g.
31
28
 
32
29
  ## Contributing
33
30
 
@@ -7,12 +7,17 @@ module Humanize
7
7
  autoload :Mega, 'humanize-bytes/mbyte'
8
8
  autoload :Giga, 'humanize-bytes/gbyte'
9
9
 
10
- def self.initialize(value, unit = 'b')
10
+ def self.initialize(value, unit = :b)
11
11
  case unit
12
+ when 'b' then Humanize::Bytes::Byte.new(value)
12
13
  when 'k' then Humanize::Bytes::Kilo.new(value)
13
14
  when 'm' then Humanize::Bytes::Mega.new(value)
14
15
  when 'g' then Humanize::Bytes::Giga.new(value)
15
- else Humanize::Bytes::Byte.new(value)
16
+ when :b then Humanize::Bytes::Byte.new(value)
17
+ when :k then Humanize::Bytes::Kilo.new(value)
18
+ when :m then Humanize::Bytes::Mega.new(value)
19
+ when :g then Humanize::Bytes::Giga.new(value)
20
+ else nil
16
21
  end
17
22
  end
18
23
  end
@@ -1,5 +1,5 @@
1
1
  module Humanize
2
2
  module Bytes
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -2,28 +2,64 @@ require 'spec_helper'
2
2
 
3
3
  describe Humanize::Bytes do
4
4
  describe "#initialize" do
5
- context "when no unit is specified" do
5
+ context "when the second parameter is nil" do
6
6
  it "should return a new Byte instance" do
7
7
  Humanize::Bytes.initialize(1024).should be_instance_of(Humanize::Bytes::Byte)
8
8
  end
9
9
  end
10
10
 
11
- context "when kilo is specfied as unit" do
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
12
18
  it "should return a new Kilo instance" do
13
19
  Humanize::Bytes.initialize(1024, 'k').should be_instance_of(Humanize::Bytes::Kilo)
14
20
  end
15
21
  end
16
22
 
17
- context "when mega is specfied as unit" do
23
+ context "when the second parameter is 'm'" do
18
24
  it "should return a new Mega instance" do
19
25
  Humanize::Bytes.initialize(1024, 'm').should be_instance_of(Humanize::Bytes::Mega)
20
26
  end
21
27
  end
22
28
 
23
- context "when giga is specfied as unit" do
29
+ context "when the second parameter is 'g'" do
24
30
  it "should return a new Giga instance" do
25
31
  Humanize::Bytes.initialize(1024, 'g').should be_instance_of(Humanize::Bytes::Giga)
26
32
  end
27
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
28
64
  end
29
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humanize-bytes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &15077160 !ruby/object:Gem::Requirement
16
+ requirement: &19265380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *15077160
24
+ version_requirements: *19265380
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &15092840 !ruby/object:Gem::Requirement
27
+ requirement: &19280960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *15092840
35
+ version_requirements: *19280960
36
36
  description: Convert Byte, Kbyte, MByte, Gbyte into each other
37
37
  email: plribeiro3000@gmail.com
38
38
  executables: []