Class: Dovado::Router::Traffic::Amount

Inherits:
Numeric
  • Object
show all
Defined in:
lib/dovado/router/traffic/amount.rb

Overview

Data amount data type. Extends Numeric and can be used as an Integer with the addition of getting the amount as bytes, kilobytes, megabytes or gigabytes.

Since:

  • 1.0.2

Constant Summary collapse

DEFAULT_KILO_BASE =

The default base of a kilobyte.

Since:

  • 1.0.2

1024

Instance Method Summary collapse

Constructor Details

#initialize(value, base = DEFAULT_KILO_BASE) ⇒ Amount

Create a new Amount object.

Parameters:

Since:

  • 1.0.2



18
19
20
21
22
# File 'lib/dovado/router/traffic/amount.rb', line 18

def initialize(value, base=DEFAULT_KILO_BASE)
  raise ArgumentError.new "Argument is not numeric: #{value}" unless value.is_a? Numeric
  @value = value
  @base = base
end

Instance Method Details

#bytesObject

Since:

  • 1.0.2



25
26
27
# File 'lib/dovado/router/traffic/amount.rb', line 25

def bytes
  @value * @base
end

#gigabytesObject

Since:

  • 1.0.2



40
41
42
# File 'lib/dovado/router/traffic/amount.rb', line 40

def gigabytes
  (megabytes / @base.to_f).round(2)
end

#kilobytesObject

Since:

  • 1.0.2



30
31
32
# File 'lib/dovado/router/traffic/amount.rb', line 30

def kilobytes
  @value
end

#megabytesObject

Since:

  • 1.0.2



35
36
37
# File 'lib/dovado/router/traffic/amount.rb', line 35

def megabytes
  (kilobytes / @base.to_f).round(2)
end