Class: Dovado::Router::Traffic::Amount
- Inherits:
-
Numeric
- Object
- Numeric
- Dovado::Router::Traffic::Amount
- 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.
Constant Summary collapse
- DEFAULT_KILO_BASE =
The default base of a kilobyte.
1024
Instance Method Summary collapse
-
#bytes ⇒ Object
The Amount in bytes.
-
#gigabytes ⇒ Object
The Amount in gigabytes.
-
#initialize(value, base = DEFAULT_KILO_BASE) ⇒ Amount
constructor
Create a new Amount object.
-
#kilobytes ⇒ Object
The Amount in kilobytes.
-
#megabytes ⇒ Object
The Amount in megabytes.
Constructor Details
#initialize(value, base = DEFAULT_KILO_BASE) ⇒ Amount
Create a new Amount object.
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
#bytes ⇒ Object
The Dovado::Router::Traffic::Amount in bytes
25 26 27 |
# File 'lib/dovado/router/traffic/amount.rb', line 25 def bytes @value * @base end |
#gigabytes ⇒ Object
The Dovado::Router::Traffic::Amount in gigabytes
40 41 42 |
# File 'lib/dovado/router/traffic/amount.rb', line 40 def gigabytes (megabytes / @base.to_f).round(2) end |
#kilobytes ⇒ Object
The Dovado::Router::Traffic::Amount in kilobytes
30 31 32 |
# File 'lib/dovado/router/traffic/amount.rb', line 30 def kilobytes @value end |
#megabytes ⇒ Object
The Dovado::Router::Traffic::Amount in megabytes
35 36 37 |
# File 'lib/dovado/router/traffic/amount.rb', line 35 def megabytes (kilobytes / @base.to_f).round(2) end |