Class: MeterCat::Divide
- Inherits:
-
Object
- Object
- MeterCat::Divide
- Defined in:
- lib/meter_cat/calculator.rb
Overview
Divide
Constant Summary collapse
- FORMAT_RATIO =
'%0.1f'.freeze
- FORMAT_PERCENTAGE =
"%0.1f\%".freeze
Instance Attribute Summary collapse
-
#denominator ⇒ Object
Returns the value of attribute denominator.
-
#format ⇒ Object
Returns the value of attribute format.
-
#numerator ⇒ Object
Returns the value of attribute numerator.
Instance Method Summary collapse
-
#calculate(meters, date) ⇒ Object
TODO: Rename format attr rubocop:disable Style/FormatString.
- #dependencies ⇒ Object
-
#initialize(numerator, denominator, format) ⇒ Divide
constructor
A new instance of Divide.
Constructor Details
#initialize(numerator, denominator, format) ⇒ Divide
Returns a new instance of Divide.
52 53 54 55 56 |
# File 'lib/meter_cat/calculator.rb', line 52 def initialize(numerator, denominator, format) @numerator = numerator @denominator = denominator @format = format end |
Instance Attribute Details
#denominator ⇒ Object
Returns the value of attribute denominator.
47 48 49 |
# File 'lib/meter_cat/calculator.rb', line 47 def denominator @denominator end |
#format ⇒ Object
Returns the value of attribute format.
47 48 49 |
# File 'lib/meter_cat/calculator.rb', line 47 def format @format end |
#numerator ⇒ Object
Returns the value of attribute numerator.
47 48 49 |
# File 'lib/meter_cat/calculator.rb', line 47 def numerator @numerator end |
Instance Method Details
#calculate(meters, date) ⇒ Object
TODO: Rename format attr rubocop:disable Style/FormatString
60 61 62 63 64 65 66 67 68 |
# File 'lib/meter_cat/calculator.rb', line 60 def calculate(meters, date) numerator = meters[@numerator] ? (meters[@numerator][date] || 0) : 0 denominator = meters[@denominator] ? (meters[@denominator][date] || 0) : 0 value = denominator.zero? ? 0.0 : (numerator.to_f / denominator.to_f) value *= 100 if @format == FORMAT_PERCENTAGE return sprintf(@format, value) end |
#dependencies ⇒ Object
70 71 72 |
# File 'lib/meter_cat/calculator.rb', line 70 def dependencies [@numerator, @denominator] end |