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 Attribute Summary collapse
Instance Method Summary collapse
-
#b ⇒ Object
Shortcut to #bytes.
-
#bytes ⇒ Object
The Amount in bytes.
-
#gb ⇒ Object
Shortcut to #gigabytes.
-
#gigabytes ⇒ Object
The Amount in gigabytes.
-
#initialize(value = 0, base = DEFAULT_KILO_BASE) ⇒ Amount
constructor
Create a new Amount object.
-
#kb ⇒ Object
Shortcut to #kilobytes.
-
#kilobytes ⇒ Object
The Amount in kilobytes.
-
#mb ⇒ Object
Shortcut to #megabytes.
-
#megabytes ⇒ Object
The Amount in megabytes.
Constructor Details
#initialize(value = 0, base = DEFAULT_KILO_BASE) ⇒ Amount
Create a new Amount object.
Note: You can set the default base for a kilobyte in the router admin interface to 1000
or 1024
, you should use the same base here to get the right figures. The base can differ from one operator to another.
25 26 27 28 29 |
# File 'lib/dovado/router/traffic/amount.rb', line 25 def initialize(value=0, base=DEFAULT_KILO_BASE) raise ArgumentError.new "Argument is not numeric: #{value}" unless value.is_a? Numeric @value = value @base = base end |
Instance Attribute Details
#sim_id ⇒ Object
14 15 16 |
# File 'lib/dovado/router/traffic/amount.rb', line 14 def sim_id @sim_id end |
Instance Method Details
#b ⇒ Object
Shortcut to #bytes.
37 38 39 |
# File 'lib/dovado/router/traffic/amount.rb', line 37 def b bytes end |
#bytes ⇒ Object
The Dovado::Router::Traffic::Amount in bytes.
32 33 34 |
# File 'lib/dovado/router/traffic/amount.rb', line 32 def bytes @value * @base end |
#gb ⇒ Object
Shortcut to #gigabytes.
67 68 69 |
# File 'lib/dovado/router/traffic/amount.rb', line 67 def gb gigabytes end |
#gigabytes ⇒ Object
The Dovado::Router::Traffic::Amount in gigabytes.
62 63 64 |
# File 'lib/dovado/router/traffic/amount.rb', line 62 def gigabytes (megabytes / @base.to_f).round(2) end |
#kb ⇒ Object
Shortcut to #kilobytes.
47 48 49 |
# File 'lib/dovado/router/traffic/amount.rb', line 47 def kb kilobytes end |
#kilobytes ⇒ Object
The Dovado::Router::Traffic::Amount in kilobytes.
42 43 44 |
# File 'lib/dovado/router/traffic/amount.rb', line 42 def kilobytes @value end |
#mb ⇒ Object
Shortcut to #megabytes.
57 58 59 |
# File 'lib/dovado/router/traffic/amount.rb', line 57 def mb megabytes end |
#megabytes ⇒ Object
The Dovado::Router::Traffic::Amount in megabytes.
52 53 54 |
# File 'lib/dovado/router/traffic/amount.rb', line 52 def megabytes (kilobytes / @base.to_f).round(2) end |