filesize 0.0.3 → 0.0.4
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.markdown +27 -12
- data/filesize.gemspec +3 -1
- data/lib/filesize.rb +27 -8
- metadata +19 -2
data/README.markdown
CHANGED
@@ -10,28 +10,43 @@ That means:
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
### Parsing a string
|
13
|
-
|
13
|
+
```ruby
|
14
|
+
Filesize.from("1 GiB")
|
15
|
+
# => #<Filesize:0x93c06c8 @bytes=1073741824, @type={:regexp=>/^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier=>1024, :presuffix=>"i"}>
|
16
|
+
```
|
14
17
|
|
15
18
|
### Converting filesizes
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
```ruby
|
20
|
+
Filesize.from("1 GiB").to_f('KiB') # => 1048576.0
|
21
|
+
Filesize.from("1 GiB").to_f('KB') # => 1073741.824
|
22
|
+
Filesize.from("1 GB").to_i # => 1000000000
|
23
|
+
```
|
19
24
|
|
20
25
|
### Outputting filesizes
|
21
|
-
|
22
|
-
|
26
|
+
```ruby
|
27
|
+
Filesize.from("12502343 B").to_s('GiB') # => "0.01 GiB"
|
28
|
+
Filesize.from("12502343 B").pretty # => "11.92 MiB"
|
29
|
+
```
|
23
30
|
|
24
31
|
### Calculating with filesizes
|
25
32
|
#### The file size on the left side sets the type
|
26
|
-
|
27
|
-
|
33
|
+
```ruby
|
34
|
+
(Filesize.from("1400 MB") + Filesize.from("1400 MiB")).pretty # => "2.87 GB"
|
35
|
+
(Filesize.from("1400 MiB") + Filesize.from("1400 MB")).pretty # => "2.67 GiB"
|
36
|
+
```
|
28
37
|
|
29
38
|
#### Filesizes can also be coerced
|
30
|
-
|
31
|
-
|
39
|
+
```ruby
|
40
|
+
(Filesize.from("1400 MiB") + 1024).pretty # => "1.37 GiB"
|
41
|
+
(1024 + Filesize.from("1400 MB")).pretty # => "1.40 GB"
|
42
|
+
```
|
32
43
|
|
33
44
|
#### filesize.rb is smart about the return value
|
34
|
-
|
45
|
+
```ruby
|
46
|
+
Filesize.from("1400 MiB") / Filesize.from("700 MiB") # => 2.0
|
47
|
+
```
|
35
48
|
|
36
49
|
#### One can also use predefined sizes
|
37
|
-
|
50
|
+
```ruby
|
51
|
+
Filesize::DVD / Filesize::CD # => 6.13566756571429
|
52
|
+
```
|
data/filesize.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = "filesize"
|
4
|
-
s.version = "0.0.
|
4
|
+
s.version = "0.0.4"
|
5
5
|
|
6
6
|
s.authors = ["Dominik Honnef"]
|
7
7
|
s.date = %q{2009-07-26}
|
@@ -14,4 +14,6 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.homepage = "http://filesize.rubyforge.org/"
|
15
15
|
s.required_ruby_version = ">= 1.8.6"
|
16
16
|
s.rubyforge_project = "filesize"
|
17
|
+
|
18
|
+
s.add_development_dependency "rspec", "~> 3.0"
|
17
19
|
end
|
data/lib/filesize.rb
CHANGED
@@ -1,10 +1,28 @@
|
|
1
1
|
class Filesize
|
2
|
+
TYPE_PREFIXES = {
|
3
|
+
# Unit prefixes used for SI file sizes.
|
4
|
+
SI: %w{k M G T P E Z Y},
|
5
|
+
# Unit prefixes used for binary file sizes.
|
6
|
+
BINARY: %w{Ki Mi Gi Ti Pi Ei Zi Yi}
|
7
|
+
}
|
8
|
+
|
9
|
+
# @deprecated Please use TYPE_PREFIXES[:SI] instead
|
10
|
+
PREFIXES = TYPE_PREFIXES[:SI]
|
11
|
+
|
2
12
|
# Set of rules describing file sizes according to SI units.
|
3
|
-
SI
|
13
|
+
SI = {
|
14
|
+
:regexp => /^([\d,.]+)?\s?([kmgtpezy]?)b$/i,
|
15
|
+
:multiplier => 1000,
|
16
|
+
:prefixes => TYPE_PREFIXES[:SI],
|
17
|
+
:presuffix => '' # deprecated
|
18
|
+
}
|
4
19
|
# Set of rules describing file sizes according to binary units.
|
5
|
-
BINARY = {
|
6
|
-
|
7
|
-
|
20
|
+
BINARY = {
|
21
|
+
:regexp => /^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i,
|
22
|
+
:multiplier => 1024,
|
23
|
+
:prefixes => TYPE_PREFIXES[:BINARY],
|
24
|
+
:presuffix => 'i' # deprecated
|
25
|
+
}
|
8
26
|
|
9
27
|
# @param [Number] size A file size, in bytes.
|
10
28
|
# @param [SI, BINARY] type Which type to use for conversions.
|
@@ -32,7 +50,7 @@ class Filesize
|
|
32
50
|
to_type = to_parts[:type]
|
33
51
|
size = @bytes
|
34
52
|
|
35
|
-
pos = (
|
53
|
+
pos = (@type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1
|
36
54
|
|
37
55
|
size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1
|
38
56
|
end
|
@@ -56,9 +74,9 @@ class Filesize
|
|
56
74
|
unit = "B"
|
57
75
|
else
|
58
76
|
pos = (Math.log(size) / Math.log(@type[:multiplier])).floor
|
59
|
-
pos =
|
77
|
+
pos = @type[:prefixes].size-1 if pos > @type[:prefixes].size - 1
|
60
78
|
|
61
|
-
unit =
|
79
|
+
unit = @type[:prefixes][pos-1] + "B"
|
62
80
|
end
|
63
81
|
|
64
82
|
to_s(unit)
|
@@ -89,6 +107,7 @@ class Filesize
|
|
89
107
|
end
|
90
108
|
end
|
91
109
|
|
110
|
+
# @return [Boolean]
|
92
111
|
def ==(other)
|
93
112
|
other.is_a?(self.class) && other.to_i == self.to_i
|
94
113
|
end
|
@@ -114,7 +133,7 @@ class Filesize
|
|
114
133
|
|
115
134
|
raise ArgumentError, "Unparseable filesize" unless type
|
116
135
|
|
117
|
-
offset = (
|
136
|
+
offset = (type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1
|
118
137
|
|
119
138
|
new(size * (type[:multiplier] ** (offset)), type)
|
120
139
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filesize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,23 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2009-07-26 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
14
30
|
description: filesize is a small class for handling filesizes with both the SI and
|
15
31
|
binary prefixes, allowing conversion from any size to any other size.
|
16
32
|
email: dominikh@fork-bomb.org
|
@@ -51,3 +67,4 @@ specification_version: 3
|
|
51
67
|
summary: filesize is a small class for handling filesizes with both the SI and binary
|
52
68
|
prefixes, allowing conversion from any size to any other size.
|
53
69
|
test_files: []
|
70
|
+
has_rdoc:
|