filesize 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4a40982cb8ea531ae79dfce2cd06391e4f00f4cd
4
- data.tar.gz: 1ec01b8c640c1788a4668f1de8ffb1427cbf113e
2
+ SHA256:
3
+ metadata.gz: 4fffde69cad6cbd10d4fc4bc0e5ca8c2148dad349621778dcdd89dcff6fd4a04
4
+ data.tar.gz: 1835c6a095c92206331410d9a5dee93c16aa856cfe4fde0cacb4e0a0d0904d3b
5
5
  SHA512:
6
- metadata.gz: e039c7cfce3ea31ddf7b013e4250e942d835fa975ee1a8fbda57eda779fea9c4bcec72c6a0763afac893bcbbc02245d74ae2ca98fccd743688c1cdcb8d90373f
7
- data.tar.gz: 2d208660a67c829b4e3fa2c5a81685a9e65311dfe3f1ba3e12733263bac9b1b54d9a9ba2c0f1c81c3430c1788aea4da3b4c8559294721f582e5c18d57406cbda
6
+ metadata.gz: b6acf41642a5ac6d062c5a4f061b561b0ac2ee08e5f6a43e6fba5621a2f3128468fe909907eece7896864c3305adcad0b6af9795b609fd51cf3dc45bbb8ad1e7
7
+ data.tar.gz: e73c2e947e014e02785deb668467ea8720f24210b56113a1761229e4b4ff50472297d6dfe6be9bb5c78198c4ab25336fa8d52d5fe5deaa68c755be975b0bd15d
@@ -13,19 +13,22 @@ class Filesize
13
13
 
14
14
  # Set of rules describing file sizes according to SI units.
15
15
  SI = {
16
- :regexp => /^([\d,.]+)?\s?([kmgtpezy]?)b$/i,
16
+ :regexp => /^([\d,.]+)?[[:space:]]?([kmgtpezy]?)b?$/i,
17
17
  :multiplier => 1000,
18
18
  :prefixes => TYPE_PREFIXES[:SI],
19
19
  :presuffix => '' # deprecated
20
20
  }
21
21
  # Set of rules describing file sizes according to binary units.
22
22
  BINARY = {
23
- :regexp => /^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i,
23
+ :regexp => /^([\d,.]+)?[[:space:]]?(?:([kmgtpezy])i)?b?$/i,
24
24
  :multiplier => 1024,
25
25
  :prefixes => TYPE_PREFIXES[:BINARY],
26
26
  :presuffix => 'i' # deprecated
27
27
  }
28
28
 
29
+ # Set default precision
30
+ PRECISION = 2
31
+
29
32
  # @param [Number] size A file size, in bytes.
30
33
  # @param [SI, BINARY] type Which type to use for conversions.
31
34
  def initialize(size, type = BINARY)
@@ -61,8 +64,10 @@ class Filesize
61
64
  # @param (see #to_f)
62
65
  # @return [String] Same as {#to_f}, but as a string, with the unit appended.
63
66
  # @see #to_f
64
- def to_s(unit = 'B')
65
- "%.2f %s" % [to(unit).to_f.to_s, unit]
67
+ def to_s(unit = 'B', args = {})
68
+ precision = args[:precision] || PRECISION
69
+
70
+ "%.#{precision}f %s" % [to(unit).to_f.to_s, unit]
66
71
  end
67
72
 
68
73
  # Same as {#to_s} but with an automatic determination of the most
@@ -70,7 +75,7 @@ class Filesize
70
75
  #
71
76
  # @return [String]
72
77
  # @see #to_s
73
- def pretty
78
+ def pretty(args = {})
74
79
  size = @bytes
75
80
  if size < @type[:multiplier]
76
81
  unit = "B"
@@ -81,7 +86,7 @@ class Filesize
81
86
  unit = @type[:prefixes][pos-1] + "B"
82
87
  end
83
88
 
84
- to_s(unit)
89
+ to_s(unit, args)
85
90
  end
86
91
 
87
92
  # @return [Filesize]
@@ -132,7 +137,7 @@ class Filesize
132
137
  size = parts[:size]
133
138
  type = parts[:type]
134
139
 
135
- raise ArgumentError, "Unparseable filesize" unless type
140
+ raise ArgumentError, "Unparseable filesize: #{arg}" unless type
136
141
 
137
142
  offset = (type[:prefixes].map { |s| s[0].chr.downcase }.index(prefix.downcase) || -1) + 1
138
143
 
@@ -38,6 +38,10 @@ describe Filesize do
38
38
  it 'parses megabytes' do
39
39
  expect(Filesize.from('1 MB').to).to eq 1000 * 1000
40
40
  end
41
+
42
+ it 'parses megabytes without b suffix' do
43
+ expect(Filesize.from('1 M').to).to eq 1000 * 1000
44
+ end
41
45
  end
42
46
 
43
47
  context 'BINARY units' do
@@ -48,6 +52,10 @@ describe Filesize do
48
52
  it 'parses megabytes' do
49
53
  expect(Filesize.from('1 MiB').to).to eq 1024 * 1024
50
54
  end
55
+
56
+ it 'parses mebibytes without b suffix' do
57
+ expect(Filesize.from('1 Mi').to).to eq 1024 * 1024
58
+ end
51
59
  end
52
60
  end
53
61
 
@@ -94,6 +102,10 @@ describe Filesize do
94
102
  it 'returns the number of for the specified unit with its unit (SI)' do
95
103
  expect(Filesize.new(1000).to_s('kB')).to eq '1.00 kB'
96
104
  end
105
+
106
+ it 'returns the number with another precision' do
107
+ expect(Filesize.new(1360).to_s('kB', :precision => 1)).to eq '1.4 kB'
108
+ end
97
109
  end
98
110
 
99
111
  describe '#pretty' do
@@ -101,9 +113,16 @@ describe Filesize do
101
113
  expect(Filesize.new(1024).pretty).to eq '1.00 KiB'
102
114
  end
103
115
 
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'
116
+ describe 'returns the number of the most matching prefix with its unit (SI)' do
117
+ it 'default precision Filesize::PRECISION' do
118
+ expect(Filesize.new(1000, Filesize::SI).pretty).to eq '1.00 kB'
119
+ end
120
+
121
+ it 'zero decimals' do
122
+ expect(Filesize.new(1000, Filesize::SI).pretty(:precision => 0)).to eq '1 kB'
123
+ end
106
124
  end
125
+
107
126
  end
108
127
 
109
128
  describe '::Floppy' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filesize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Honnef
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-05 00:00:00.000000000 Z
11
+ date: 2018-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -56,10 +56,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project:
59
- rubygems_version: 2.4.5.1
59
+ rubygems_version: 2.7.7
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: filesize is a small class for handling filesizes with both the SI and binary
63
63
  prefixes, allowing conversion from any size to any other size.
64
64
  test_files: []
65
- has_rdoc: yard