Class: Fbe::Award::Bill
- Inherits:
-
Object
- Object
- Fbe::Award::Bill
- Defined in:
- lib/fbe/award.rb
Overview
A bill class that accumulates points and explanations for rewards.
This class tracks variables, point values, and explanatory text for each award component. It provides methods to calculate total points and generate a human-readable summary of the rewards.
Instance Attribute Summary collapse
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Instance Method Summary collapse
-
#greeting ⇒ String
Generates a human-readable summary of the bill.
-
#initialize ⇒ Bill
constructor
Creates a new empty bill.
-
#line(value, text) ⇒ nil
Adds a point value with explanatory text to the bill.
-
#points ⇒ Integer
Calculates the total points in this bill.
-
#set(var, value) ⇒ Object
Sets a variable in the bill’s context.
Constructor Details
#initialize ⇒ Bill
Creates a new empty bill.
331 332 333 334 |
# File 'lib/fbe/award.rb', line 331 def initialize @lines = [] @vars = {} end |
Instance Attribute Details
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
325 326 327 |
# File 'lib/fbe/award.rb', line 325 def vars @vars end |
Instance Method Details
#greeting ⇒ String
Generates a human-readable summary of the bill.
382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/fbe/award.rb', line 382 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 |
#line(value, text) ⇒ nil
Zero-valued points are ignored
Adds a point value with explanatory text to the bill.
357 358 359 360 361 |
# File 'lib/fbe/award.rb', line 357 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 |
#points ⇒ Integer
Calculates the total points in this bill.
370 371 372 |
# File 'lib/fbe/award.rb', line 370 def points Integer(Float(@lines.sum { |l| l[:v] }).round.to_s, 10) end |
#set(var, value) ⇒ Object
Sets a variable in the bill’s context.
344 345 346 |
# File 'lib/fbe/award.rb', line 344 def set(var, value) @vars[var] = value end |