Class: Chef::Provider::HouseKeeping::Humanize
- Defined in:
- lib/garcon/chef/provider/house_keeping.rb
Instance Method Summary collapse
- #from_size(places = 1) ⇒ Object
-
#initialize(size) ⇒ Humanize
constructor
A new instance of Humanize.
- #to_size(unit, places = 1) ⇒ Object
Constructor Details
#initialize(size) ⇒ Humanize
Returns a new instance of Humanize.
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/garcon/chef/provider/house_keeping.rb', line 182 def initialize(size) @units = { b: 1, kb: 1024**1, mb: 1024**2, gb: 1024**3, tb: 1024**4 } @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
203 204 205 206 |
# File 'lib/garcon/chef/provider/house_keeping.rb', line 203 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
196 197 198 199 200 201 |
# File 'lib/garcon/chef/provider/house_keeping.rb', line 196 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 sprintf("%.#{places}f", size).to_f end |