Class: ChefStash::FileSize
- Inherits:
-
Object
- Object
- ChefStash::FileSize
- Defined in:
- lib/chef_stash/utils.rb
Instance Method Summary collapse
- #from_size(places = 1) ⇒ Object
-
#initialize(size) ⇒ FileSize
constructor
A new instance of FileSize.
- #to_size(unit, places = 1) ⇒ Object
Constructor Details
#initialize(size) ⇒ FileSize
Returns a new instance of FileSize.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chef_stash/utils.rb', line 61 def initialize(size) @units = { b: 1, kb: 1024**1, mb: 1024**2, gb: 1024**3, tb: 1024**4, pb: 1024**5, eb: 1024**6 } @size_int = size.partition(/\D{1,2}/).at(0).to_i unit = size.partition(/\D{1,2}/).at(1).to_s.downcase case when unit.match(/[kmgtpe]{1}/) @size_unit = unit.concat('b') when unit.match(/[kmgtpe]{1}b/) @size_unit = unit else @size_unit = 'b' end end |
Instance Method Details
#from_size(places = 1) ⇒ Object
92 93 94 95 |
# File 'lib/chef_stash/utils.rb', line 92 def from_size(places = 1) unit_val = @units[@size_unit.to_s.downcase.to_sym] sprintf("%.#{places}f", @size_int * unit_val) end |
#to_size(unit, places = 1) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/chef_stash/utils.rb', line 84 def to_size(unit, places = 1) unit_val = @units[unit.to_s.downcase.to_sym] bytes = @size_int * @units[@size_unit.to_sym] size = bytes.to_f / unit_val.to_f value = sprintf("%.#{places}f", size).to_f "#{value} #{unit.upcase}" end |