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.
@@ -15,7 +15,7 @@ module Useful
15
15
  :defaults => {:precision => 2}
16
16
  }
17
17
  }.freeze
18
- STORAGE_UNITS = [:bytes, :kb, :mb, :gb, :tb].freeze
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.capitalize
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
@@ -3,7 +3,7 @@ module Useful
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 13
6
+ TINY = 14
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kelredd-useful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelredd