Class: Kanrisuru::Util::Bits

Inherits:
Object
  • Object
show all
Defined in:
lib/kanrisuru/util/bits.rb

Class Method Summary collapse

Class Method Details

.convert_bytes(value, from, to) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kanrisuru/util/bits.rb', line 45

def self.convert_bytes(value, from, to)
  from = from.downcase.to_sym
  to   = to.downcase.to_sym

  byte_translations = {
    b: :deca,
    kb: :kilo,
    mb: :mega,
    gb: :giga,
    tb: :tera,
    pb: :peta,
    byte: :deca,
    kilobyte: :kilo,
    megabyte: :mega,
    gigabyte: :giga,
    terabyte: :tera,
    petabyte: :peta
  }

  bit_translations = {
    kib: :kilo,
    mib: :mega,
    gib: :giga,
    tib: :tera,
    pib: :peta,
    bit: :deca,
    kilobit: :kilo,
    megabit: :mega,
    gigabit: :giga,
    terabit: :tera,
    petabit: :peta
  }

  bit_translation_from = false
  bit_translation_to   = false

  from =
    if bit_translations.key?(from)
      bit_translation_from = true
      bit_translations[from]
    elsif byte_translations.key?(from)
      byte_translations[from]
    else
      raise ArgumentError, 'Invalid data type'
    end

  to =
    if bit_translations.key?(to)
      bit_translation_to = true
      bit_translations[to]
    elsif byte_translations.key?(to)
      byte_translations[to]
    else
      raise ArgumentError, 'Invalid data type'
    end

  multiplier = if bit_translation_from && !bit_translation_to
                 (1.to_f / 8).to_f
               elsif !bit_translation_from && bit_translation_to
                 8
               else
                 1
               end

  power = convert_power(from, to)

  (value * 1000.pow(power).to_f) * multiplier
end

.convert_from_gb(value, metric) ⇒ Object



33
34
35
# File 'lib/kanrisuru/util/bits.rb', line 33

def self.convert_from_gb(value, metric)
  convert_bytes(value, :gigabyte, metric)
end

.convert_from_kb(value, metric) ⇒ Object



25
26
27
# File 'lib/kanrisuru/util/bits.rb', line 25

def self.convert_from_kb(value, metric)
  convert_bytes(value, :kilobyte, metric)
end

.convert_from_mb(value, metric) ⇒ Object



29
30
31
# File 'lib/kanrisuru/util/bits.rb', line 29

def self.convert_from_mb(value, metric)
  convert_bytes(value, :megabyte, metric)
end

.convert_from_pb(value, metric) ⇒ Object



41
42
43
# File 'lib/kanrisuru/util/bits.rb', line 41

def self.convert_from_pb(value, metric)
  convert_bytes(value, :petabyte, metric)
end

.convert_from_tb(value, metric) ⇒ Object



37
38
39
# File 'lib/kanrisuru/util/bits.rb', line 37

def self.convert_from_tb(value, metric)
  convert_bytes(value, :terabyte, metric)
end

.convert_power(from_metric, to_metric) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/kanrisuru/util/bits.rb', line 114

def self.convert_power(from_metric, to_metric)
  @conversions ||= {
    deca: {
      deca: 0,
      kilo: -1,
      mega: -2,
      giga: -3,
      tera: -4,
      peta: -5,
      exa: -6,
      zetta: -7,
      yotta: -8
    },
    kilo: {
      deca: 1,
      kilo: 0,
      mega: -1,
      giga: -2,
      tera: -3,
      peta: -4,
      exa: -5,
      zetta: -6,
      yotta: -7
    },
    mega: {
      deca: 2,
      kilo: 1,
      mega: 0,
      giga: -1,
      tera: -2,
      peta: -3,
      exa: -4,
      zetta: -5,
      yotta: -6
    },
    giga: {
      deca: 3,
      kilo: 2,
      mega: 1,
      giga: 0,
      tera: -1,
      peta: -2,
      exa: -3,
      zetta: -4,
      yotta: -5
    },
    tera: {
      deca: 4,
      kilo: 3,
      mega: 2,
      giga: 1,
      tera: 0,
      peta: -1,
      exa: -2,
      zetta: -3,
      yotta: -4
    },
    peta: {
      deca: 5,
      kilo: 4,
      mega: 3,
      giga: 2,
      tera: 1,
      peta: 0,
      exa: -1,
      zetta: -2,
      yotta: -3
    },
    exa: {
      deca: 6,
      kilo: 5,
      mega: 4,
      giga: 3,
      tera: 2,
      peta: 1,
      exa: 0,
      zetta: -1,
      yotta: -2
    },
    zetta: {
      deca: 7,
      kilo: 6,
      mega: 5,
      giga: 4,
      tera: 3,
      peta: 2,
      exa: 1,
      zetta: 0,
      yotta: -1
    },
    yotta: {
      deca: 8,
      kilo: 7,
      mega: 6,
      giga: 5,
      tera: 4,
      peta: 3,
      exa: 2,
      zetta: 1,
      yotta: 0
    }
  }

  values = @conversions[from_metric]
  values ? values[to_metric] : nil
end

.normalize_size(string) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kanrisuru/util/bits.rb', line 6

def self.normalize_size(string)
  size, unit = string.split
  size = size.to_f
  unit ||= 'b'

  return 0 if size.zero?

  case unit.downcase
  when 'b'
    Kanrisuru::Util::Bits.convert_bytes(size, :byte, :kilobyte)
  when 'kb', 'k', 'kib'
    size
  when 'mb', 'm', 'mib'
    Kanrisuru::Util::Bits.convert_from_mb(size, :kilobyte).to_i
  when 'gb', 'g', 'gib'
    Kanrisuru::Util::Bits.convert_from_gb(size, :kilobyte).to_i
  end
end