kelredd-useful 0.1.13 → 0.1.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/useful/ruby_extensions/numeric.rb +5 -2
- data/lib/useful/version.rb +1 -1
- metadata +1 -1
@@ -15,7 +15,7 @@ module Useful
|
|
15
15
|
:defaults => {:precision => 2}
|
16
16
|
}
|
17
17
|
}.freeze
|
18
|
-
STORAGE_UNITS = [
|
18
|
+
STORAGE_UNITS = ['Bytes', 'KB', 'MB', 'GB', 'TB'].freeze
|
19
19
|
|
20
20
|
module ClassMethods; end
|
21
21
|
def self.included(klass)
|
@@ -179,16 +179,19 @@ module Useful
|
|
179
179
|
def to_storage_size(opts = {})
|
180
180
|
return nil if self.nil?
|
181
181
|
opts.symbolize_keys!
|
182
|
+
opt_precision = opts[:precision]
|
182
183
|
opts[:locale] ||= :en
|
183
184
|
locale = LOCALES[opts.delete(:locale)]
|
184
185
|
opts = locale[:defaults].merge(locale[:format]).merge(locale[:storage]).merge(opts) unless locale.nil?
|
185
186
|
opts[:format] ||= "%n %u"
|
187
|
+
opts[:precision] = 0 unless opt_precision
|
186
188
|
|
187
189
|
value = self.to_f
|
188
190
|
unit = ''
|
189
191
|
STORAGE_UNITS.each do |storage_unit|
|
190
|
-
unit = storage_unit.to_s
|
192
|
+
unit = storage_unit.to_s
|
191
193
|
return opts[:format].gsub(/%n/, value.with_precision(opts.only(:precision, :delimiter, :separator)).to_s).gsub(/%u/, unit.to_s) if value < 1024 || storage_unit == STORAGE_UNITS.last
|
194
|
+
opts[:precision] = 2 unless opt_precision
|
192
195
|
value /= 1024.0
|
193
196
|
end
|
194
197
|
end
|
data/lib/useful/version.rb
CHANGED