filesize 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +19 -0
- data/{README.markdown → README.md} +0 -0
- data/lib/filesize.rb +4 -4
- data/spec/lib/filesize_spec.rb +162 -0
- data/spec/spec_helper.rb +3 -0
- metadata +19 -24
- data/CHANGELOG +0 -3
- data/filesize.gemspec +0 -19
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a40982cb8ea531ae79dfce2cd06391e4f00f4cd
|
4
|
+
data.tar.gz: 1ec01b8c640c1788a4668f1de8ffb1427cbf113e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e039c7cfce3ea31ddf7b013e4250e942d835fa975ee1a8fbda57eda779fea9c4bcec72c6a0763afac893bcbbc02245d74ae2ca98fccd743688c1cdcb8d90373f
|
7
|
+
data.tar.gz: 2d208660a67c829b4e3fa2c5a81685a9e65311dfe3f1ba3e12733263bac9b1b54d9a9ba2c0f1c81c3430c1788aea4da3b4c8559294721f582e5c18d57406cbda
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2009 Dominik Honnef
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
File without changes
|
data/lib/filesize.rb
CHANGED
@@ -3,9 +3,9 @@ class Filesize
|
|
3
3
|
|
4
4
|
TYPE_PREFIXES = {
|
5
5
|
# Unit prefixes used for SI file sizes.
|
6
|
-
SI
|
6
|
+
:SI => %w{k M G T P E Z Y},
|
7
7
|
# Unit prefixes used for binary file sizes.
|
8
|
-
BINARY
|
8
|
+
:BINARY => %w{Ki Mi Gi Ti Pi Ei Zi Yi}
|
9
9
|
}
|
10
10
|
|
11
11
|
# @deprecated Please use TYPE_PREFIXES[:SI] instead
|
@@ -52,7 +52,7 @@ class Filesize
|
|
52
52
|
to_type = to_parts[:type]
|
53
53
|
size = @bytes
|
54
54
|
|
55
|
-
pos = (@type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1
|
55
|
+
pos = (@type[:prefixes].map { |s| s[0].chr.downcase }.index(prefix.downcase) || -1) + 1
|
56
56
|
|
57
57
|
size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1
|
58
58
|
end
|
@@ -134,7 +134,7 @@ class Filesize
|
|
134
134
|
|
135
135
|
raise ArgumentError, "Unparseable filesize" unless type
|
136
136
|
|
137
|
-
offset = (type[:prefixes].map { |s| s[0].downcase }.index(prefix.downcase) || -1) + 1
|
137
|
+
offset = (type[:prefixes].map { |s| s[0].chr.downcase }.index(prefix.downcase) || -1) + 1
|
138
138
|
|
139
139
|
new(size * (type[:multiplier] ** (offset)), type)
|
140
140
|
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'filesize'
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
5
|
+
describe Filesize do
|
6
|
+
describe '::PREFIXES' do
|
7
|
+
it 'are the SI prefixes' do
|
8
|
+
expect(Filesize::PREFIXES).to eq Filesize::TYPE_PREFIXES[:SI]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '::SI' do
|
13
|
+
it 'has the right multiplier' do
|
14
|
+
expect(Filesize::SI[:multiplier]).to eq 1000
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'has the right prefixes' do
|
18
|
+
expect(Filesize::SI[:prefixes]).to eq Filesize::TYPE_PREFIXES[:SI]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '::BINARY' do
|
23
|
+
it 'has the right multiplier' do
|
24
|
+
expect(Filesize::BINARY[:multiplier]).to eq 1024
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has the right prefixes' do
|
28
|
+
expect(Filesize::BINARY[:prefixes]).to eq Filesize::TYPE_PREFIXES[:BINARY]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '.from' do
|
33
|
+
context 'SI units' do
|
34
|
+
it 'parses kilobytes' do
|
35
|
+
expect(Filesize.from('1 kB').to).to eq 1000
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'parses megabytes' do
|
39
|
+
expect(Filesize.from('1 MB').to).to eq 1000 * 1000
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'BINARY units' do
|
44
|
+
it 'parses kilobytes' do
|
45
|
+
expect(Filesize.from('1 KiB').to).to eq 1024
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'parses megabytes' do
|
49
|
+
expect(Filesize.from('1 MiB').to).to eq 1024 * 1024
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#to_i' do
|
55
|
+
it 'returns the bytes' do
|
56
|
+
expect(Filesize.new(555).to_i).to eq 555
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#to' do
|
61
|
+
it 'returns the number of bytes (default)' do
|
62
|
+
expect(Filesize.new(555).to).to eq 555
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'returns the number for the specified unit (BINARY)' do
|
66
|
+
expect(Filesize.new(1024).to('KiB')).to eq 1.00
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'returns the number for the specified unit (SI)' do
|
70
|
+
expect(Filesize.new(1000).to('kB')).to eq 1.00
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#<=>" do
|
75
|
+
it 'compares correctly' do
|
76
|
+
[["1024 B", "1025 B", -1],
|
77
|
+
["1025 B", "1024 B", 1],
|
78
|
+
["1024 B", "1024 B", 0],
|
79
|
+
["1024 B", "1 KiB", 0]].each do |left, right, expected|
|
80
|
+
expect(Filesize.from(left) <=> Filesize.from(right)).to eq expected
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#to_s' do
|
86
|
+
it 'returns the number of bytes (default) with its unit' do
|
87
|
+
expect(Filesize.new(555).to_s).to eq '555.00 B'
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'returns the number of for the specified unit with its unit (BINARY)' do
|
91
|
+
expect(Filesize.new(1024).to_s('KiB')).to eq '1.00 KiB'
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'returns the number of for the specified unit with its unit (SI)' do
|
95
|
+
expect(Filesize.new(1000).to_s('kB')).to eq '1.00 kB'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#pretty' do
|
100
|
+
it 'returns the number of the most matching prefix with its unit (BINARY default)' do
|
101
|
+
expect(Filesize.new(1024).pretty).to eq '1.00 KiB'
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'returns the number of the most matching prefix with its unit (SI)' do
|
105
|
+
expect(Filesize.new(1000, Filesize::SI).pretty).to eq '1.00 kB'
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '::Floppy' do
|
110
|
+
it 'has the right size in bytes' do
|
111
|
+
expect(Filesize::Floppy.to_i).to eq 1_509_376
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe '::CD' do
|
116
|
+
it 'has the right size in bytes' do
|
117
|
+
expect(Filesize::CD.to_i).to eq 700_000_000
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '::DVD_5' do
|
122
|
+
it 'has the right size in bytes' do
|
123
|
+
expect(Filesize::DVD_5.to_i).to eq 4_702_989_189
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe '::DVD' do
|
128
|
+
it 'has the right size in bytes' do
|
129
|
+
expect(Filesize::DVD.to_i).to eq 4_702_989_189
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe '::DVD_9' do
|
134
|
+
it 'has the right size in bytes' do
|
135
|
+
expect(Filesize::DVD_9.to_i).to eq 8_504_035_246
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe '::DVD_10' do
|
140
|
+
it 'has the right size in bytes' do
|
141
|
+
expect(Filesize::DVD_10.to_i).to eq 9_405_978_378
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe '::DVD_14' do
|
146
|
+
it 'has the right size in bytes' do
|
147
|
+
expect(Filesize::DVD_14.to_i).to eq 13_207_024_435
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe '::DVD_18' do
|
152
|
+
it 'has the right size in bytes' do
|
153
|
+
expect(Filesize::DVD_18.to_i).to eq 26_414_048_870
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe '::ZIP' do
|
158
|
+
it 'has the right size in bytes' do
|
159
|
+
expect(Filesize::ZIP.to_i).to eq 100_000_000
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filesize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Dominik Honnef
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-09-05 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '3.0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.0'
|
30
27
|
description: filesize is a small class for handling filesizes with both the SI and
|
@@ -32,39 +29,37 @@ description: filesize is a small class for handling filesizes with both the SI a
|
|
32
29
|
email: dominikh@fork-bomb.org
|
33
30
|
executables: []
|
34
31
|
extensions: []
|
35
|
-
extra_rdoc_files:
|
36
|
-
- README.markdown
|
37
|
-
- CHANGELOG
|
38
|
-
- lib/filesize.rb
|
32
|
+
extra_rdoc_files: []
|
39
33
|
files:
|
40
|
-
-
|
41
|
-
-
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
42
36
|
- lib/filesize.rb
|
43
|
-
-
|
44
|
-
|
45
|
-
|
37
|
+
- spec/lib/filesize_spec.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
homepage: https://github.com/dominikh/filesize
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
46
43
|
post_install_message:
|
47
44
|
rdoc_options: []
|
48
45
|
require_paths:
|
49
46
|
- lib
|
50
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
48
|
requirements:
|
53
|
-
- -
|
49
|
+
- - ">="
|
54
50
|
- !ruby/object:Gem::Version
|
55
51
|
version: 1.8.6
|
56
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
53
|
requirements:
|
59
|
-
- -
|
54
|
+
- - ">="
|
60
55
|
- !ruby/object:Gem::Version
|
61
56
|
version: '0'
|
62
57
|
requirements: []
|
63
|
-
rubyforge_project:
|
64
|
-
rubygems_version:
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.4.5.1
|
65
60
|
signing_key:
|
66
|
-
specification_version:
|
61
|
+
specification_version: 4
|
67
62
|
summary: filesize is a small class for handling filesizes with both the SI and binary
|
68
63
|
prefixes, allowing conversion from any size to any other size.
|
69
64
|
test_files: []
|
70
|
-
has_rdoc:
|
65
|
+
has_rdoc: yard
|
data/CHANGELOG
DELETED
data/filesize.gemspec
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
Gem::Specification.new do |s|
|
3
|
-
s.name = "filesize"
|
4
|
-
s.version = "0.1.0"
|
5
|
-
|
6
|
-
s.authors = ["Dominik Honnef"]
|
7
|
-
s.date = %q{2009-07-26}
|
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
|
10
|
-
|
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"
|
17
|
-
|
18
|
-
s.add_development_dependency "rspec", "~> 3.0"
|
19
|
-
end
|