class Fbe::Award::Bill
and generate a human-readable summary of the rewards.
for each award component. It provides methods to calculate total points
This class tracks variables, point values, and explanatory text
A bill class that accumulates points and explanations for rewards.
def greeting
-
(String)
- A description of the points earned
def greeting items = @lines.map { |l| "#{format('%+d', l[:v])} #{l[:t]}" } case items.size when 0 "You've earned nothing. " when 1 "You've earned #{format('%+d', points)} points. " else "You've earned #{format('%+d', points)} points for this: #{items.join('; ')}. " end end
def initialize
@example
Creates a new empty bill.
def initialize @lines = [] @vars = {} end
def line(value, text)
- Note: - Zero-valued points are ignored
Returns:
-
(nil)
-
Parameters:
-
text
(String
) -- The explanation for these points -
value
(Integer, Float
) -- The point value to add
def line(value, text) return if value.zero? text = text.gsub(/\$\{([a-z_0-9]+)\}/) { |_x| @vars[Regexp.last_match[1].to_sym] } @lines << { v: value, t: text } end
def points
-
(Integer)
- The sum of all point values, rounded to an integer
def points @lines.sum { |l| l[:v] }.to_f.round.to_i end
def set(var, value)
-
(Object)
- The assigned value
Parameters:
-
value
(Object
) -- The value to assign -
var
(Symbol
) -- The variable name
def set(var, value) @vars[var] = value end