Class: Fbe::Award::Bill

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeBill

Creates a new empty bill.

Examples:

bill = Fbe::Award::Bill.new


343
344
345
346
# File 'lib/fbe/award.rb', line 343

def initialize
  @lines = []
  @vars = {}
end

Instance Attribute Details

#varsHash (readonly)

Returns Variables set in this bill.

Returns:

  • (Hash)

    Variables set in this bill



337
338
339
# File 'lib/fbe/award.rb', line 337

def vars
  @vars
end

Instance Method Details

#greetingString

Generates a human-readable summary of the bill.

Examples:

bill = Fbe::Award::Bill.new
bill.line(50, "for code review")
bill.line(25, "for documentation")
bill.greeting #=> "You've earned +75 points for this: +50 for code review; +25 for documentation. "

Returns:

  • (String)

    A description of the points earned



394
395
396
397
398
399
400
401
402
403
404
# File 'lib/fbe/award.rb', line 394

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

Note:

Zero-valued points are ignored

Adds a point value with explanatory text to the bill.

Examples:

bill = Fbe::Award::Bill.new
bill.line(50, "for code review")

Parameters:

  • value (Integer, Float)

    The point value to add

  • text (String)

    The explanation for these points

Returns:

  • (nil)


369
370
371
372
373
# File 'lib/fbe/award.rb', line 369

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

#pointsInteger

Calculates the total points in this bill.

Examples:

bill = Fbe::Award::Bill.new
bill.line(42.5, "for answer")
bill.points #=> 43

Returns:

  • (Integer)

    The sum of all point values, rounded to an integer



382
383
384
# File 'lib/fbe/award.rb', line 382

def points
  @lines.sum { |l| l[:v] }.to_f.round.to_i
end

#set(var, value) ⇒ Object

Sets a variable in the bill’s context.

Examples:

bill = Fbe::Award::Bill.new
bill.set(:lines_of_code, 500)

Parameters:

  • var (Symbol)

    The variable name

  • value (Object)

    The value to assign

Returns:

  • (Object)

    The assigned value



356
357
358
# File 'lib/fbe/award.rb', line 356

def set(var, value)
  @vars[var] = value
end