human_size_to_number 0.0.1
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/.gitignore +7 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/human_size_to_number.gemspec +25 -0
- data/lib/human_size_to_number/helper.rb +40 -0
- data/lib/human_size_to_number/version.rb +3 -0
- data/lib/human_size_to_number.rb +1 -0
- data/lib/tasks/rspec.rake +9 -0
- data/rails/init.rb +1 -0
- data/spec/human_size_to_number/helper_spec.rb +59 -0
- data/spec/spec_helper.rb +10 -0
- metadata +136 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use ree@human_size_to_number > /dev/null
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
Overview
|
2
|
+
========
|
3
|
+
|
4
|
+
About human\_size\_to\_number
|
5
|
+
-----------------------------
|
6
|
+
|
7
|
+
This gem implement the missing reverse of the [ActionView::Helpers::NumberHelper#number\_to\_human\_size](http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_human_size).
|
8
|
+
|
9
|
+
Usage
|
10
|
+
-----
|
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
|
18
|
+
|
19
|
+
Project Tracking
|
20
|
+
----------------
|
21
|
+
|
22
|
+
* [human\_size\_to\_number ticket](https://github.com/zedtux/human_size_to_number/issues)
|
23
|
+
* [human\_size\_to\_number Website and Documentation](https://github.com/zedtux/human_size_to_number/wiki)
|
24
|
+
|
25
|
+
Compatibility
|
26
|
+
-------------
|
27
|
+
|
28
|
+
human\_size\_to\_number is tested against REE.
|
29
|
+
|
30
|
+
License
|
31
|
+
-------
|
32
|
+
|
33
|
+
Copyright (c) 2011 Guillaume Hain
|
34
|
+
|
35
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
36
|
+
a copy of this software and associated documentation files (the
|
37
|
+
"Software"), to deal in the Software without restriction, including
|
38
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
39
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
40
|
+
permit persons to whom the Software is furnished to do so, subject to
|
41
|
+
the following conditions:
|
42
|
+
|
43
|
+
The above copyright notice and this permission notice shall be
|
44
|
+
included in all copies or substantial portions of the Software.
|
45
|
+
|
46
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
47
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
48
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
49
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
50
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
51
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
52
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
53
|
+
|
54
|
+
Credits
|
55
|
+
-------
|
56
|
+
|
57
|
+
Guillaume Hain: zedtux at zedroot dot com
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "human_size_to_number/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "human_size_to_number"
|
7
|
+
s.version = HumanSizeToNumber::VERSION
|
8
|
+
s.authors = ["zedtux"]
|
9
|
+
s.email = ["zedtux@zedroot.org"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Implement missing reverse of ActionView::Helpers::NumberHelper#number_to_human_size}
|
12
|
+
s.description = %q{Convert a size string like 1.12KB to the number value in Bytes.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "human_size_to_number"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "actionpack", "~> 2.0.0"
|
21
|
+
s.add_dependency "rake"
|
22
|
+
|
23
|
+
s.add_development_dependency "rspec"
|
24
|
+
s.add_development_dependency "rcov"
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
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
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "human_size_to_number/helper"
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "human_size_to_number"
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActionView::Helpers::TextHelper do
|
4
|
+
|
5
|
+
describe "#human_size_to_number" do
|
6
|
+
context "when passing the string 123" do
|
7
|
+
context "wihout raise option" do
|
8
|
+
it "should not raise an exception and return nil" do
|
9
|
+
subject.human_size_to_number("123").should be_nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
context "wih raise option" do
|
13
|
+
it "should raise an InvalidSizeStringError exception" do
|
14
|
+
lambda {
|
15
|
+
subject.human_size_to_number("123", :raise => true)
|
16
|
+
}.should raise_error(
|
17
|
+
ActionView::Helpers::TextHelper::InvalidSizeStringError
|
18
|
+
)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
context "when passing the string 123 Bytes" do
|
23
|
+
it "should return the number 123" do
|
24
|
+
subject.human_size_to_number("123 Bytes").should == 123
|
25
|
+
end
|
26
|
+
end
|
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
|
30
|
+
end
|
31
|
+
end
|
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
|
35
|
+
end
|
36
|
+
end
|
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
|
40
|
+
end
|
41
|
+
end
|
42
|
+
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
|
45
|
+
end
|
46
|
+
end
|
47
|
+
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
|
50
|
+
end
|
51
|
+
end
|
52
|
+
context "when passing the string lorem Bytes" do
|
53
|
+
it "should " do
|
54
|
+
subject.human_size_to_number("lorem Bytes").should be_nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: human_size_to_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- zedtux
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-03 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: actionpack
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 15
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 0
|
32
|
+
- 0
|
33
|
+
version: 2.0.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
type: :development
|
63
|
+
version_requirements: *id003
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: rcov
|
66
|
+
prerelease: false
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
type: :development
|
77
|
+
version_requirements: *id004
|
78
|
+
description: Convert a size string like 1.12KB to the number value in Bytes.
|
79
|
+
email:
|
80
|
+
- zedtux@zedroot.org
|
81
|
+
executables: []
|
82
|
+
|
83
|
+
extensions: []
|
84
|
+
|
85
|
+
extra_rdoc_files: []
|
86
|
+
|
87
|
+
files:
|
88
|
+
- .gitignore
|
89
|
+
- .rspec
|
90
|
+
- .rvmrc
|
91
|
+
- Gemfile
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- human_size_to_number.gemspec
|
95
|
+
- lib/human_size_to_number.rb
|
96
|
+
- lib/human_size_to_number/helper.rb
|
97
|
+
- lib/human_size_to_number/version.rb
|
98
|
+
- lib/tasks/rspec.rake
|
99
|
+
- rails/init.rb
|
100
|
+
- spec/human_size_to_number/helper_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: ""
|
103
|
+
licenses: []
|
104
|
+
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project: human_size_to_number
|
131
|
+
rubygems_version: 1.7.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Implement missing reverse of ActionView::Helpers::NumberHelper#number_to_human_size
|
135
|
+
test_files: []
|
136
|
+
|