bytes_converter 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,20 +2,57 @@ require "bytes_converter/version"
2
2
 
3
3
  module BytesConverter
4
4
 
5
+ # default available values
6
+ @sizes = {
7
+ "k" => 1024,
8
+ "m" => 1024*1024,
9
+ "g" => 1024*1024*1024
10
+ }
11
+
5
12
  # method converting size into bytes
6
13
  def self.convert size
7
- sizes = {
8
- "K" => 1024,
9
- "M" => 1024*1024,
10
- "G" => 1024*1024*1024
11
- }
12
- m = size.scan(/(\d*\.\d*)+(\w)?/).collect { |serwer| serwer }
13
14
 
14
- unless m[0].nil?
15
- out = m[0][0].to_f * sizes[m[0][1]].to_f
16
- else
17
- out = size
15
+ # replacing comma with dot
16
+ size_converted = size.downcase.gsub(',','.')
17
+
18
+ # separating digits (size) from rest of the string (unit)
19
+ x = size_converted.scan(/(\d*\.?\d*)(.*)/).collect { |found| found }
20
+
21
+ # stripping whitespaces
22
+ x[0][0].strip!
23
+ x[0][1].strip!
24
+
25
+ # limiting unit to first letter (k,m and so on)
26
+ x[0][1] = x[0][1][/\w{1}/]
27
+
28
+ # if unit is present (not nil) -> getting multiplier from sizes array
29
+ # and calculating bytes
30
+ unless x[0][1].nil?
31
+ out = x[0][0].to_f * @sizes[x[0][1]].to_f
32
+ else # if unit is not present -> we assume that size is already in bytes.
33
+ out = x[0][0].to_f
18
34
  end
35
+
36
+ # returning calculated (or not) value
19
37
  out
20
38
  end
39
+
40
+ # method adding new unit to @sizes hash
41
+ def self.add_unit new_unit
42
+ @sizes.merge!(new_unit)
43
+ true
44
+ end
45
+
46
+ # getter for sizes
47
+ def self.get_units
48
+ @sizes.each do | key, value |
49
+ puts "unit: #{key} = #{value}"
50
+ end
51
+ "end of list"
52
+ end
53
+
54
+ # method removing size
55
+ def self.remove_unit unit
56
+ @sizes.delete(unit) { |key| "Unit \"#{key}\" not found among sizes!" }
57
+ end
21
58
  end
@@ -1,3 +1,3 @@
1
1
  module BytesConverter
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bytes_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-17 00:00:00.000000000 Z
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Gem converting Kilobytes, Megabytes and Gigabytes to bytes
15
15
  email: