human_size_to_number 0.0.1 → 0.2.0
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.
- data/README.md +11 -6
- data/Rakefile +3 -1
- data/human_size_to_number.gemspec +3 -1
- data/lib/human_size_to_number/helper.rb +28 -36
- data/lib/human_size_to_number/version.rb +1 -1
- data/spec/human_size_to_number/helper_spec.rb +22 -17
- metadata +5 -5
data/README.md
CHANGED
@@ -9,12 +9,12 @@ This gem implement the missing reverse of the [ActionView::Helpers::NumberHelper
|
|
9
9
|
Usage
|
10
10
|
-----
|
11
11
|
|
12
|
-
>
|
13
|
-
>
|
14
|
-
>
|
15
|
-
>
|
16
|
-
>
|
17
|
-
>
|
12
|
+
> "123 Bytes".human_size_to_number # => 123
|
13
|
+
> "1.12 KB".human_size_to_number # => 1239
|
14
|
+
> "12.1 KB".human_size_to_number # => 12390
|
15
|
+
> "1.18 MB".human_size_to_number # => 1237320
|
16
|
+
> "1.15 GB".human_size_to_number # => 1234803098
|
17
|
+
> "1.12 TB".human_size_to_number # => 1231453023109
|
18
18
|
|
19
19
|
Project Tracking
|
20
20
|
----------------
|
@@ -27,6 +27,11 @@ Compatibility
|
|
27
27
|
|
28
28
|
human\_size\_to\_number is tested against REE.
|
29
29
|
|
30
|
+
<img src="https://travis-ci.org/zedtux/human_size_to_number.png?branch=master&.png"/>
|
31
|
+
|
32
|
+
[Build History](http://travis-ci.org/zedtux/human_size_to_number)
|
33
|
+
|
34
|
+
|
30
35
|
License
|
31
36
|
-------
|
32
37
|
|
data/Rakefile
CHANGED
@@ -3,4 +3,6 @@ require "rake"
|
|
3
3
|
require "bundler"
|
4
4
|
Bundler::GemHelper.install_tasks
|
5
5
|
|
6
|
-
Dir["#{Gem.searcher.find("human_size_to_number").full_gem_path}/lib/tasks/**/*.rake"].each { |ext| load ext }
|
6
|
+
Dir["#{Gem.searcher.find("human_size_to_number").full_gem_path}/lib/tasks/**/*.rake"].each { |ext| load ext }
|
7
|
+
|
8
|
+
task :default => ["spec:coverage"]
|
@@ -5,9 +5,11 @@ require "human_size_to_number/version"
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "human_size_to_number"
|
7
7
|
s.version = HumanSizeToNumber::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.date = File.new("lib/human_size_to_number/version.rb").ctime
|
8
10
|
s.authors = ["zedtux"]
|
9
11
|
s.email = ["zedtux@zedroot.org"]
|
10
|
-
s.homepage = ""
|
12
|
+
s.homepage = "https://github.com/zedtux/human_size_to_number"
|
11
13
|
s.summary = %q{Implement missing reverse of ActionView::Helpers::NumberHelper#number_to_human_size}
|
12
14
|
s.description = %q{Convert a size string like 1.12KB to the number value in Bytes.}
|
13
15
|
|
@@ -1,40 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
else
|
22
|
-
return size
|
23
|
-
end
|
24
|
-
end
|
25
|
-
case unit
|
26
|
-
when "Bytes"
|
27
|
-
number.to_i
|
28
|
-
when "KB"
|
29
|
-
number * 1024
|
30
|
-
when "MB"
|
31
|
-
number * 1024 * 1024
|
32
|
-
when "GB"
|
33
|
-
number * 1024 * 1024 * 1024
|
34
|
-
when "TB"
|
35
|
-
number * 1024 * 1024 * 1024 * 1024
|
36
|
-
end
|
1
|
+
class String
|
2
|
+
|
3
|
+
# Raised when argument +size+ param given to the helpers is invalid and
|
4
|
+
# the option :raise is set to +true+.
|
5
|
+
class InvalidSizeStringError < StandardError
|
6
|
+
attr_accessor :size_string
|
7
|
+
def initialize(size_string)
|
8
|
+
@size_string = size_string
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def human_size_to_number(options={})
|
13
|
+
size, unit = self.scan(/(\d*\.?\d+)\s?(Bytes|KB|MB|GB|TB)/).first
|
14
|
+
number = begin
|
15
|
+
Float(size)
|
16
|
+
rescue ArgumentError, TypeError
|
17
|
+
if options[:raise]
|
18
|
+
raise InvalidSizeStringError, size
|
19
|
+
else
|
20
|
+
return size
|
37
21
|
end
|
38
22
|
end
|
23
|
+
number = case unit
|
24
|
+
when "Bytes": number
|
25
|
+
when "KB": number * 1024
|
26
|
+
when "MB": number * 1024 * 1024
|
27
|
+
when "GB": number * 1024 * 1024 * 1024
|
28
|
+
when "TB": number * 1024 * 1024 * 1024 * 1024
|
29
|
+
end
|
30
|
+
number.round
|
39
31
|
end
|
40
32
|
end
|
@@ -1,59 +1,64 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe String do
|
4
4
|
|
5
5
|
describe "#human_size_to_number" do
|
6
6
|
context "when passing the string 123" do
|
7
7
|
context "wihout raise option" do
|
8
8
|
it "should not raise an exception and return nil" do
|
9
|
-
|
9
|
+
"123".human_size_to_number.should be_nil
|
10
10
|
end
|
11
11
|
end
|
12
12
|
context "wih raise option" do
|
13
13
|
it "should raise an InvalidSizeStringError exception" do
|
14
14
|
lambda {
|
15
|
-
|
15
|
+
"123".human_size_to_number(:raise => true)
|
16
16
|
}.should raise_error(
|
17
|
-
|
17
|
+
String::InvalidSizeStringError
|
18
18
|
)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
22
|
context "when passing the string 123 Bytes" do
|
23
23
|
it "should return the number 123" do
|
24
|
-
|
24
|
+
"123 Bytes".human_size_to_number.should == 123
|
25
25
|
end
|
26
26
|
end
|
27
27
|
context "when passing the string 1.21 KB" do
|
28
|
-
it "should return the number 1239
|
29
|
-
|
28
|
+
it "should return the number 1239" do
|
29
|
+
"1.21 KB".human_size_to_number.should == 1239
|
30
30
|
end
|
31
31
|
end
|
32
32
|
context "when passing the string 12.1 KB" do
|
33
|
-
it "should return the number 12390
|
34
|
-
|
33
|
+
it "should return the number 12390" do
|
34
|
+
"12.1 KB".human_size_to_number.should == 12390
|
35
35
|
end
|
36
36
|
end
|
37
37
|
context "when passing the string 1.18 MB" do
|
38
|
-
it "should return the number
|
39
|
-
|
38
|
+
it "should return the number 1237320" do
|
39
|
+
"1.18 MB".human_size_to_number.should == 1237320
|
40
|
+
end
|
41
|
+
end
|
42
|
+
context "when passing the string 1 MB" do
|
43
|
+
it "should return the number 1048576" do
|
44
|
+
"1 MB".human_size_to_number.should == 1048576
|
40
45
|
end
|
41
46
|
end
|
42
47
|
context "when passing the string 1.15 GB" do
|
43
|
-
it "should return the number
|
44
|
-
|
48
|
+
it "should return the number 1234803098" do
|
49
|
+
"1.15 GB".human_size_to_number.should == 1234803098
|
45
50
|
end
|
46
51
|
end
|
47
52
|
context "when passing the string 1.12 TB" do
|
48
|
-
it "should return the number 1231453023109
|
49
|
-
|
53
|
+
it "should return the number 1231453023109" do
|
54
|
+
"1.12 TB".human_size_to_number.should == 1231453023109
|
50
55
|
end
|
51
56
|
end
|
52
57
|
context "when passing the string lorem Bytes" do
|
53
58
|
it "should " do
|
54
|
-
|
59
|
+
"lorem Bytes".human_size_to_number.should be_nil
|
55
60
|
end
|
56
61
|
end
|
57
62
|
end
|
58
63
|
|
59
|
-
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: human_size_to_number
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 2
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.1
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- zedtux
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-04 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: actionpack
|
@@ -99,7 +99,7 @@ files:
|
|
99
99
|
- rails/init.rb
|
100
100
|
- spec/human_size_to_number/helper_spec.rb
|
101
101
|
- spec/spec_helper.rb
|
102
|
-
homepage:
|
102
|
+
homepage: https://github.com/zedtux/human_size_to_number
|
103
103
|
licenses: []
|
104
104
|
|
105
105
|
post_install_message:
|