Class: DiceBag::MinMaxCalc
- Inherits:
-
Object
- Object
- DiceBag::MinMaxCalc
- Defined in:
- lib/dicebag/min_max_calc.rb
Overview
This is service class to calculate the Maximum/Minimum for the given Roll.
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#opposite ⇒ Object
readonly
Returns the value of attribute opposite.
-
#roll ⇒ Object
readonly
Returns the value of attribute roll.
Class Method Summary collapse
- .average(roll) ⇒ Object
- .call(roll, method) ⇒ Object
- .maximum(roll) ⇒ Object
- .minimum(roll) ⇒ Object
Instance Method Summary collapse
-
#calculate(oper, part) ⇒ Object
Update the total based on the oper.
-
#initialize(roll, method) ⇒ MinMaxCalc
constructor
A new instance of MinMaxCalc.
- #opposite_method ⇒ Object
- #perform ⇒ Object
Constructor Details
#initialize(roll, method) ⇒ MinMaxCalc
Returns a new instance of MinMaxCalc.
25 26 27 28 29 30 |
# File 'lib/dicebag/min_max_calc.rb', line 25 def initialize(roll, method) @roll = roll @method = method @opposite = opposite_method @total = 0 end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
6 7 8 |
# File 'lib/dicebag/min_max_calc.rb', line 6 def method @method end |
#opposite ⇒ Object (readonly)
Returns the value of attribute opposite.
7 8 9 |
# File 'lib/dicebag/min_max_calc.rb', line 7 def opposite @opposite end |
#roll ⇒ Object (readonly)
Returns the value of attribute roll.
5 6 7 |
# File 'lib/dicebag/min_max_calc.rb', line 5 def roll @roll end |
Class Method Details
.average(roll) ⇒ Object
9 10 11 |
# File 'lib/dicebag/min_max_calc.rb', line 9 def self.average(roll) (maximum(roll) + minimum(roll)) / 2.0 end |
.call(roll, method) ⇒ Object
21 22 23 |
# File 'lib/dicebag/min_max_calc.rb', line 21 def self.call(roll, method) new(roll, method).perform end |
.maximum(roll) ⇒ Object
13 14 15 |
# File 'lib/dicebag/min_max_calc.rb', line 13 def self.maximum(roll) call roll, :maximum end |
.minimum(roll) ⇒ Object
17 18 19 |
# File 'lib/dicebag/min_max_calc.rb', line 17 def self.minimum(roll) call roll, :minimum end |
Instance Method Details
#calculate(oper, part) ⇒ Object
Update the total based on the oper. For ‘additive’ operations, we use the method, but if it’s ‘subtractive’ then it uses the opposite method.
45 46 47 48 49 50 51 52 53 |
# File 'lib/dicebag/min_max_calc.rb', line 45 def calculate(oper, part) case oper when :start then @total = part.send(method) when :add then @total += part.send(method) when :sub then @total -= part.send(opposite) when :mul then @total *= part.send(method) when :div then @total /= part.send(opposite) end end |
#opposite_method ⇒ Object
55 56 57 |
# File 'lib/dicebag/min_max_calc.rb', line 55 def opposite_method method == :maximum ? :minimum : :maximum end |
#perform ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/dicebag/min_max_calc.rb', line 32 def perform roll.tree.each do |op, part| next unless part.is_a?(RollPart) || part.is_a?(StaticPart) calculate op, part end @total end |