filesize 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -1
- data/README.markdown +1 -1
- data/filesize.gemspec +10 -27
- data/lib/filesize.rb +41 -7
- metadata +23 -38
- data.tar.gz.sig +0 -3
- data/Manifest +0 -5
- data/Rakefile +0 -17
- metadata.gz.sig +1 -3
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -5,7 +5,7 @@ That means:
|
|
5
5
|
* Parsing strings (e.g. "1 GiB") and saving it internally as bytes
|
6
6
|
* Handling both SI and binary prefixes
|
7
7
|
* Converting from any type and unit to any other (SI to SI, SI to Binary and so on)
|
8
|
-
* doing
|
8
|
+
* doing calculations with filesizes (in a smart way, see Usage for more)
|
9
9
|
* filesize.rb also provides some default sizes, like the ones of DVDs
|
10
10
|
|
11
11
|
## Usage
|
data/filesize.gemspec
CHANGED
@@ -1,34 +1,17 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
2
|
Gem::Specification.new do |s|
|
4
|
-
s.name =
|
5
|
-
s.version = "0.0.
|
3
|
+
s.name = "filesize"
|
4
|
+
s.version = "0.0.2"
|
6
5
|
|
7
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
6
|
s.authors = ["Dominik Honnef"]
|
9
|
-
s.cert_chain = ["/home/dominikh/.rubyforge/gem-public_cert.pem"]
|
10
7
|
s.date = %q{2009-07-26}
|
11
|
-
s.description =
|
12
|
-
s.
|
13
|
-
s.extra_rdoc_files = ["README.markdown", "CHANGELOG", "lib/filesize.rb"]
|
14
|
-
s.files = ["Rakefile", "README.markdown", "CHANGELOG", "lib/filesize.rb", "Manifest", "filesize.gemspec"]
|
15
|
-
s.has_rdoc = true
|
16
|
-
s.homepage = %q{http://filesize.rubyforge.org/}
|
17
|
-
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Filesize", "--main", "README.markdown"]
|
18
|
-
s.require_paths = ["lib"]
|
19
|
-
s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
|
20
|
-
s.rubyforge_project = %q{filesize}
|
21
|
-
s.rubygems_version = %q{1.3.1}
|
22
|
-
s.signing_key = %q{/home/dominikh/.rubyforge/gem-private_key.pem}
|
23
|
-
s.summary = %q{filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.}
|
8
|
+
s.description = "filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size."
|
9
|
+
s.summary = s.description
|
24
10
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
else
|
33
|
-
end
|
11
|
+
s.email = "dominikh@fork-bomb.org"
|
12
|
+
s.extra_rdoc_files = ["README.markdown", "CHANGELOG", "lib/filesize.rb"]
|
13
|
+
s.files = ["README.markdown", "CHANGELOG", "lib/filesize.rb", "filesize.gemspec"]
|
14
|
+
s.homepage = "http://filesize.rubyforge.org/"
|
15
|
+
s.required_ruby_version = ">= 1.8.6"
|
16
|
+
s.rubyforge_project = "filesize"
|
34
17
|
end
|
data/lib/filesize.rb
CHANGED
@@ -1,20 +1,26 @@
|
|
1
1
|
class Filesize
|
2
|
+
# Set of rules describing file sizes according to SI units.
|
2
3
|
SI = {:regexp => /^([\d,.]+)?\s?([kmgtpezy]?)b$/i, :multiplier => 1000, :presuffix => ''}
|
4
|
+
# Set of rules describing file sizes according to binary units.
|
3
5
|
BINARY = {:regexp => /^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier => 1024, :presuffix => 'i'}
|
6
|
+
# Unit prefixes used for file sizes.
|
4
7
|
PREFIXES = %w{k M G T P E Z Y}
|
5
8
|
|
9
|
+
# @param [Number] size A file size, in bytes.
|
10
|
+
# @param [SI, BINARY] type Which type to use for conversions.
|
6
11
|
def initialize(size, type = BINARY)
|
7
12
|
@bytes = size.to_i
|
8
13
|
@type = type
|
9
14
|
end
|
10
15
|
|
11
|
-
# Returns the size in bytes
|
16
|
+
# @return [Number] Returns the size in bytes.
|
12
17
|
def to_i
|
13
18
|
@bytes
|
14
19
|
end
|
15
20
|
alias_method :to_int, :to_i
|
16
21
|
|
17
|
-
#
|
22
|
+
# @param [String] unit Which unit to convert to.
|
23
|
+
# @return [Float] Returns the size in a given unit.
|
18
24
|
def to(unit = 'B')
|
19
25
|
to_parts = self.class.parse(unit)
|
20
26
|
prefix = to_parts[:prefix]
|
@@ -32,13 +38,18 @@ class Filesize
|
|
32
38
|
end
|
33
39
|
alias_method :to_f, :to
|
34
40
|
|
35
|
-
#
|
41
|
+
# @param (see #to_f)
|
42
|
+
# @return [String] Same as {#to_f}, but as a string, with the unit appended.
|
43
|
+
# @see #to_f
|
36
44
|
def to_s(unit = 'B')
|
37
45
|
"%.2f %s" % [to(unit).to_f.to_s, unit]
|
38
46
|
end
|
39
47
|
|
40
|
-
# Same as to_s but with an automatic
|
41
|
-
# sensible unit
|
48
|
+
# Same as {#to_s} but with an automatic determination of the most
|
49
|
+
# sensible unit.
|
50
|
+
#
|
51
|
+
# @return [String]
|
52
|
+
# @see #to_s
|
42
53
|
def pretty
|
43
54
|
size = @bytes
|
44
55
|
if size < @type[:multiplier]
|
@@ -53,18 +64,22 @@ class Filesize
|
|
53
64
|
to_s(unit)
|
54
65
|
end
|
55
66
|
|
67
|
+
# @return [Filesize]
|
56
68
|
def +(other)
|
57
69
|
self.class.new(@bytes + other.to_i, @type)
|
58
70
|
end
|
59
71
|
|
72
|
+
# @return [Filesize]
|
60
73
|
def -(other)
|
61
74
|
self.class.new(@bytes - other.to_i, @type)
|
62
75
|
end
|
63
76
|
|
77
|
+
# @return [Filesize]
|
64
78
|
def *(other)
|
65
79
|
self.class.new(@bytes * other.to_i, @type)
|
66
80
|
end
|
67
81
|
|
82
|
+
# @return [Filesize]
|
68
83
|
def /(other)
|
69
84
|
result = @bytes / other.to_f
|
70
85
|
if other.is_a? Filesize
|
@@ -74,11 +89,19 @@ class Filesize
|
|
74
89
|
end
|
75
90
|
end
|
76
91
|
|
92
|
+
# @return [Array<self, other>]
|
93
|
+
# @api private
|
77
94
|
def coerce(other)
|
78
95
|
return self, other
|
79
96
|
end
|
80
97
|
|
81
98
|
class << self
|
99
|
+
# Parses a string, which describes a file size, and returns a
|
100
|
+
# Filesize object.
|
101
|
+
#
|
102
|
+
# @param [String] arg A file size to parse.
|
103
|
+
# @raise [ArgumentError] Raised if the file size cannot be parsed properly.
|
104
|
+
# @return [Filesize]
|
82
105
|
def from(arg)
|
83
106
|
parts = parse(arg)
|
84
107
|
prefix = parts[:prefix]
|
@@ -92,6 +115,8 @@ class Filesize
|
|
92
115
|
new(size * (type[:multiplier] ** (offset)), type)
|
93
116
|
end
|
94
117
|
|
118
|
+
# @return [Hash<:prefix, :size, :type>]
|
119
|
+
# @api private
|
95
120
|
def parse(string)
|
96
121
|
type = nil
|
97
122
|
# in this order, so we prefer binary :)
|
@@ -103,19 +128,28 @@ class Filesize
|
|
103
128
|
}
|
104
129
|
|
105
130
|
prefix = $2 || ''
|
106
|
-
size = ($1 ||
|
131
|
+
size = ($1 || 0).to_f
|
107
132
|
|
108
133
|
return { :prefix => prefix, :size => size, :type => type}
|
109
134
|
end
|
110
135
|
end
|
111
136
|
|
137
|
+
# The size of a floppy disk
|
112
138
|
Floppy = Filesize.from("1474 KiB")
|
139
|
+
# The size of a CD
|
113
140
|
CD = Filesize.from("700 MB")
|
141
|
+
# The size of a common DVD
|
114
142
|
DVD_5 = Filesize.from("4.38 GiB")
|
143
|
+
# The same as a DVD 5
|
115
144
|
DVD = DVD_5
|
145
|
+
# The size of a single-sided dual-layer DVD
|
116
146
|
DVD_9 = Filesize.from("7.92 GiB")
|
147
|
+
# The size of a double-sided single-layer DVD
|
117
148
|
DVD_10 = DVD_5 * 2
|
118
|
-
|
149
|
+
# The size of a double-sided DVD, combining a DVD-9 and a DVD-5
|
150
|
+
DVD_14 = DVD_9 + DVD_5
|
151
|
+
# The size of a double-sided dual-layer DVD
|
119
152
|
DVD_18 = DVD_14 * 2
|
153
|
+
# The size of a Zip disk
|
120
154
|
ZIP = Filesize.from("100 MB")
|
121
155
|
end
|
metadata
CHANGED
@@ -1,41 +1,25 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filesize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Dominik Honnef
|
8
13
|
autorequire:
|
9
14
|
bindir: bin
|
10
|
-
cert_chain:
|
11
|
-
- |
|
12
|
-
-----BEGIN CERTIFICATE-----
|
13
|
-
MIIDMDCCAhigAwIBAgIBADANBgkqhkiG9w0BAQUFADA+MRIwEAYDVQQDDAlkb21p
|
14
|
-
bmlraG8xEzARBgoJkiaJk/IsZAEZFgNnbXgxEzARBgoJkiaJk/IsZAEZFgNuZXQw
|
15
|
-
HhcNMDkwNzI0MjE1NDQ3WhcNMTAwNzI0MjE1NDQ3WjA+MRIwEAYDVQQDDAlkb21p
|
16
|
-
bmlraG8xEzARBgoJkiaJk/IsZAEZFgNnbXgxEzARBgoJkiaJk/IsZAEZFgNuZXQw
|
17
|
-
ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCgfogFNoAcdrR5mK0t5NiN
|
18
|
-
MbWCH2QVLqOvpuaHN6uiaE30R9fT+1CcF97PSboMHMKaQFgiyAcBj8c2uGw5Sk7+
|
19
|
-
Kv9TE2TIIG8l8oslnf9PVqrHSz823IjzbXo4WET4khI2v02pE2wx/souJ4PRgnEr
|
20
|
-
gxQaOF7fqJRT2eYiZAMQye1y9tNSShRoKP3JIlp51KBXUMzGdzq4Mopp32zhEcio
|
21
|
-
yEzcp1PYqu6D1Pr00fS/Alurnp/My5o3oIk0vBfJ6Q8j/6OSk+ck5xev2Z2Y9AUK
|
22
|
-
iwipCxKrAsfOjpPc/Y66okMeTmKxx5N/8h/xNp/HiUApHQwjbYgFOAwmht5UzoPp
|
23
|
-
AgMBAAGjOTA3MAkGA1UdEwQCMAAwHQYDVR0OBBYEFLlluBMEZ3rFvxFcB+ULjUNd
|
24
|
-
V+7WMAsGA1UdDwQEAwIEsDANBgkqhkiG9w0BAQUFAAOCAQEAoDJGicQwBqMnhMkC
|
25
|
-
JY7bgsrIwxoTMQT6B1keUtt9VQ3deywQu0ogawNmee5wy5oFqnMZFcSCXcJE/zIE
|
26
|
-
iuSYU5t87iF09BIsSHWg2OlHAB4+GTUL0PkVsCVBDFukqEiXpNcqMUjTr8U+fe7V
|
27
|
-
oD/qvzyMG1ooAFuTDfPknIQxTcavdnmUmO9Sfw10yXkomX8jjwaGduF/JHDIufub
|
28
|
-
/f4b22rpLBhrOD6dqHCl7WuAfAhpobLqc7sa6Ro8caSUnpq9blOrT/PRtiTXwN6h
|
29
|
-
p3eI3JV0LC6/tMHoeLAbHqauVbWUqDB+AfFw24sgZBqFA+MtMkDpqZ+BKiwiHl+S
|
30
|
-
YEfDWg==
|
31
|
-
-----END CERTIFICATE-----
|
15
|
+
cert_chain: []
|
32
16
|
|
33
17
|
date: 2009-07-26 00:00:00 +02:00
|
34
18
|
default_executable:
|
35
19
|
dependencies: []
|
36
20
|
|
37
21
|
description: filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.
|
38
|
-
email:
|
22
|
+
email: dominikh@fork-bomb.org
|
39
23
|
executables: []
|
40
24
|
|
41
25
|
extensions: []
|
@@ -45,42 +29,43 @@ extra_rdoc_files:
|
|
45
29
|
- CHANGELOG
|
46
30
|
- lib/filesize.rb
|
47
31
|
files:
|
48
|
-
- Rakefile
|
49
32
|
- README.markdown
|
50
33
|
- CHANGELOG
|
51
34
|
- lib/filesize.rb
|
52
|
-
- Manifest
|
53
35
|
- filesize.gemspec
|
54
36
|
has_rdoc: true
|
55
37
|
homepage: http://filesize.rubyforge.org/
|
38
|
+
licenses: []
|
39
|
+
|
56
40
|
post_install_message:
|
57
|
-
rdoc_options:
|
58
|
-
|
59
|
-
- --inline-source
|
60
|
-
- --title
|
61
|
-
- Filesize
|
62
|
-
- --main
|
63
|
-
- README.markdown
|
41
|
+
rdoc_options: []
|
42
|
+
|
64
43
|
require_paths:
|
65
44
|
- lib
|
66
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
67
47
|
requirements:
|
68
48
|
- - ">="
|
69
49
|
- !ruby/object:Gem::Version
|
50
|
+
segments:
|
51
|
+
- 1
|
52
|
+
- 8
|
53
|
+
- 6
|
70
54
|
version: 1.8.6
|
71
|
-
version:
|
72
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
73
57
|
requirements:
|
74
58
|
- - ">="
|
75
59
|
- !ruby/object:Gem::Version
|
76
|
-
|
77
|
-
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
78
63
|
requirements: []
|
79
64
|
|
80
65
|
rubyforge_project: filesize
|
81
|
-
rubygems_version: 1.3.
|
66
|
+
rubygems_version: 1.3.7
|
82
67
|
signing_key:
|
83
|
-
specification_version:
|
68
|
+
specification_version: 3
|
84
69
|
summary: filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.
|
85
70
|
test_files: []
|
86
71
|
|
data.tar.gz.sig
DELETED
data/Manifest
DELETED
data/Rakefile
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'echoe'
|
2
|
-
Echoe.new('filesize') do |p|
|
3
|
-
p.author = "Dominik Honnef"
|
4
|
-
p.email = "dominikho@gmx.net"
|
5
|
-
|
6
|
-
p.description = %q{filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.}
|
7
|
-
p.summary = %q{filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.}
|
8
|
-
|
9
|
-
p.url = "http://filesize.rubyforge.org/"
|
10
|
-
p.docs_host = "rubyforge.org:/var/www/gforge-projects/"
|
11
|
-
|
12
|
-
p.ruby_version = '>= 1.8.6'
|
13
|
-
|
14
|
-
p.require_signed = true
|
15
|
-
|
16
|
-
p.project = 'filesize'
|
17
|
-
end
|
metadata.gz.sig
DELETED