humanize-bytes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3-p125@humanize_bytes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in humanize-bytes.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Paulo Henrique Lopes Ribeiro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Humanize::Bytes
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'humanize-bytes'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install humanize-bytes
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/humanize-bytes/version', __FILE__)
3
+
4
+ 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 = ""
10
+
11
+ 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"
15
+ gem.require_paths = %w(lib)
16
+ gem.version = Humanize::Bytes::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec", ">= 2.0.0"
20
+ end
@@ -0,0 +1,19 @@
1
+ require "humanize-bytes/version"
2
+
3
+ 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
12
+ when 'k' then Humanize::Bytes::Kilo.new(value)
13
+ when 'm' then Humanize::Bytes::Mega.new(value)
14
+ when 'g' then Humanize::Bytes::Giga.new(value)
15
+ else Humanize::Bytes::Byte.new(value)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,35 @@
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_k
13
+ @value / 1024.0
14
+ end
15
+
16
+ def to_m
17
+ @value / 1024.0 / 1024
18
+ end
19
+
20
+ def to_g
21
+ @value / 1024.0 / 1024 / 1024
22
+ end
23
+
24
+ def to_s
25
+ @value.instance_of?(Float) ? formatted_float + ' bytes' : @value.to_s + ' bytes'
26
+ end
27
+
28
+ protected
29
+
30
+ def formatted_float
31
+ @value.to_s[0 .. @value.to_s.index('.') + 2]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
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
+ @value * 1024 * 1024 * 1024
14
+ end
15
+
16
+ def to_k
17
+ @value * 1024 * 1024
18
+ end
19
+
20
+ def to_m
21
+ @value * 1024
22
+ end
23
+
24
+ def to_s
25
+ @value.instance_of?(Float) ? formatted_float + ' giga bytes' : @value.to_s + ' giga bytes'
26
+ end
27
+
28
+ protected
29
+
30
+ def formatted_float
31
+ @value.to_s[0 .. @value.to_s.index('.') + 2]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
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
+ @value * 1024
14
+ end
15
+
16
+ def to_m
17
+ @value / 1024.0
18
+ end
19
+
20
+ def to_g
21
+ @value / 1024.0 / 1024
22
+ end
23
+
24
+ def to_s
25
+ @value.instance_of?(Float) ? formatted_float + ' kilo bytes' : @value.to_s + ' kilo bytes'
26
+ end
27
+
28
+ protected
29
+
30
+ def formatted_float
31
+ @value.to_s[0 .. @value.to_s.index('.') + 2]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
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
+ @value * 1024 * 1024
14
+ end
15
+
16
+ def to_k
17
+ @value * 1024
18
+ end
19
+
20
+ def to_g
21
+ @value / 1024.0
22
+ end
23
+
24
+ def to_s
25
+ @value.instance_of?(Float) ? formatted_float + ' mega bytes' : @value.to_s + ' mega bytes'
26
+ end
27
+
28
+ protected
29
+
30
+ def formatted_float
31
+ @value.to_s[0 .. @value.to_s.index('.') + 2]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Humanize
2
+ module Bytes
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,43 @@
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_k" do
13
+ it "should convert the value to kilo bytes" do
14
+ b.to_k.should == 2097503.5625
15
+ end
16
+ end
17
+
18
+ context "#to_m" do
19
+ it "should convert the value to mega bytes" do
20
+ b.to_m.should == 2048.3433227539062
21
+ end
22
+ end
23
+
24
+ context "#to_g" do
25
+ it "should convert the value to giga bytes" do
26
+ b.to_g.should == 2.0003352761268616
27
+ end
28
+ end
29
+
30
+ context "#to_s" do
31
+ context "when value is an integer" do
32
+ it "should print a humanized version of the value" do
33
+ b.to_s.should == '2147843648 bytes'
34
+ end
35
+ end
36
+
37
+ context "when value is an integer" do
38
+ it "should print a humanized version of the value" do
39
+ Humanize::Bytes::Byte.new(4.997).to_s.should == '4.99 bytes'
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
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 the value to bytes" do
14
+ g.to_b.should == 3489660928
15
+ end
16
+ end
17
+
18
+ context "#to_k" do
19
+ it "should convert the value to kilo bytes" do
20
+ g.to_k.should == 3407872
21
+ end
22
+ end
23
+
24
+ context "#to_m" do
25
+ it "should convert the value to mega bytes" do
26
+ g.to_m.should == 3328
27
+ end
28
+ end
29
+
30
+ context "#to_s" do
31
+ context "when value is a decimal" do
32
+ it "should print a humanized version of the value" do
33
+ g.to_s.should == '3.25 giga bytes'
34
+ end
35
+ end
36
+
37
+ context "when value is an integer" do
38
+ it "should print a humanized version of the value" do
39
+ Humanize::Bytes::Giga.new(4).to_s.should == '4 giga bytes'
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
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 the value to bytes" do
14
+ k.to_b.should == 5120000
15
+ end
16
+ end
17
+
18
+ context "#to_m" do
19
+ it "should convert the value to mega bytes" do
20
+ k.to_m.should == 4.8828125
21
+ end
22
+ end
23
+
24
+ context "#to_g" do
25
+ it "should convert the value to giga bytes" do
26
+ Humanize::Bytes::Kilo.new(5120000).to_g.should == 4.8828125
27
+ end
28
+ end
29
+
30
+ context "#to_s" do
31
+ context "when value is an integer" do
32
+ it "should print a humanized version of the value" do
33
+ k.to_s.should == '5000 kilo bytes'
34
+ end
35
+ end
36
+
37
+ context "when value is an integer" do
38
+ it "should print a humanized version of the value" do
39
+ Humanize::Bytes::Kilo.new(4.997).to_s.should == '4.99 kilo bytes'
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
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 the value to bytes" do
14
+ m.to_b.should == 5120000
15
+ end
16
+ end
17
+
18
+ context "#to_k" do
19
+ it "should convert the value to kilo bytes" do
20
+ m.to_k.should == 5000
21
+ end
22
+ end
23
+
24
+ context "#to_g" do
25
+ it "should convert the value to giga bytes" do
26
+ Humanize::Bytes::Mega.new(1000).to_g.should == 0.9765625
27
+ end
28
+ end
29
+
30
+ context "#to_s" do
31
+ context "when value is a decimal" do
32
+ it "should print a humanized version of the value" do
33
+ m.to_s.should == '4.88 mega bytes'
34
+ end
35
+ end
36
+
37
+ context "when value is an integer" do
38
+ it "should print a humanized version of the value" do
39
+ Humanize::Bytes::Mega.new(4).to_s.should == '4 mega bytes'
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Humanize::Bytes do
4
+ describe "#initialize" do
5
+ context "when no unit is specified" 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 kilo is specfied as unit" do
12
+ it "should return a new Kilo instance" do
13
+ Humanize::Bytes.initialize('1024', 'k').should be_instance_of(Humanize::Bytes::Kilo)
14
+ end
15
+ end
16
+
17
+ context "when mega is specfied as unit" do
18
+ it "should return a new Mega instance" do
19
+ Humanize::Bytes.initialize('1024', 'm').should be_instance_of(Humanize::Bytes::Mega)
20
+ end
21
+ end
22
+
23
+ context "when giga is specfied as unit" do
24
+ it "should return a new Giga instance" do
25
+ Humanize::Bytes.initialize('1024', 'g').should be_instance_of(Humanize::Bytes::Giga)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ require "rubygems"
2
+ require "rspec"
3
+
4
+ require File.dirname(__FILE__) + "/../lib/humanize-bytes"
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: humanize-bytes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paulo Henrique Lopes Ribeiro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &16545640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *16545640
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &16561300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *16561300
36
+ description: Convert Byte, Kbyte, MByte, Gbyte into each other
37
+ email: plribeiro3000@gmail.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - .gitignore
43
+ - .rspec
44
+ - .rvmrc
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - humanize-bytes.gemspec
50
+ - lib/humanize-bytes.rb
51
+ - lib/humanize-bytes/byte.rb
52
+ - lib/humanize-bytes/gbyte.rb
53
+ - lib/humanize-bytes/kbyte.rb
54
+ - lib/humanize-bytes/mbyte.rb
55
+ - lib/humanize-bytes/version.rb
56
+ - spec/humanize_bytes/byte_spec.rb
57
+ - spec/humanize_bytes/gbyte_spec.rb
58
+ - spec/humanize_bytes/kbyte_spec.rb
59
+ - spec/humanize_bytes/mbyte_spec.rb
60
+ - spec/humanize_bytes_spec.rb
61
+ - spec/spec_helper.rb
62
+ homepage: ''
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.17
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Humanize bytes Gem
86
+ test_files:
87
+ - spec/humanize_bytes/byte_spec.rb
88
+ - spec/humanize_bytes/gbyte_spec.rb
89
+ - spec/humanize_bytes/kbyte_spec.rb
90
+ - spec/humanize_bytes/mbyte_spec.rb
91
+ - spec/humanize_bytes_spec.rb
92
+ - spec/spec_helper.rb