human_size_to_number 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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
- > human_size_to_number("123 Bytes") # => 123
13
- > human_size_to_number("1.12 KB") # => 1239.04
14
- > human_size_to_number("12.1 KB") # => 12390.4
15
- > human_size_to_number("1.18 MB") # => 1237319.68
16
- > human_size_to_number("1.15 GB") # => 1234803097.6
17
- > human_size_to_number("1.12 TB") # => 1231453023109.12
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
- module ActionView
2
- module Helpers
3
- class TextHelper
4
-
5
- # Raised when argument +size+ param given to the helpers is invalid and
6
- # the option :raise is set to +true+.
7
- class InvalidSizeStringError < StandardError
8
- attr_accessor :size_string
9
- def initialize(size_string)
10
- @size_string = size_string
11
- end
12
- end
13
-
14
- def human_size_to_number(size, options={})
15
- size, unit = size.scan(/(\d*\.?\d+)\s?(Bytes|KB|MB|GB|TB)/).first
16
- number = begin
17
- Float(size)
18
- rescue ArgumentError, TypeError
19
- if options[:raise]
20
- raise InvalidSizeStringError, size
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,3 +1,3 @@
1
1
  module HumanSizeToNumber
2
- VERSION = "0.0.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,59 +1,64 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ActionView::Helpers::TextHelper do
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
- subject.human_size_to_number("123").should be_nil
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
- subject.human_size_to_number("123", :raise => true)
15
+ "123".human_size_to_number(:raise => true)
16
16
  }.should raise_error(
17
- ActionView::Helpers::TextHelper::InvalidSizeStringError
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
- subject.human_size_to_number("123 Bytes").should == 123
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.04" do
29
- subject.human_size_to_number("1.21 KB").should == 1239.04
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.4" do
34
- subject.human_size_to_number("12.1 KB").should == 12390.4
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 1237319.68" do
39
- subject.human_size_to_number("1.18 MB").should == 1237319.68
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 1234803097.6" do
44
- subject.human_size_to_number("1.15 GB").should == 1234803097.6
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.12" do
49
- subject.human_size_to_number("1.12 TB").should == 1231453023109.12
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
- subject.human_size_to_number("lorem Bytes").should be_nil
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: 29
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 2
8
9
  - 0
9
- - 1
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-03 00:00:00 Z
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: