Class: Fbe::Award::Bill

Inherits:
Object
  • Object
show all
Defined in:
lib/fbe/award.rb

Overview

A bill.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBill

Returns a new instance of Bill.



288
289
290
291
# File 'lib/fbe/award.rb', line 288

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

Instance Attribute Details

#varsObject (readonly)

Returns the value of attribute vars.



286
287
288
# File 'lib/fbe/award.rb', line 286

def vars
  @vars
end

Instance Method Details

#greetingObject



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/fbe/award.rb', line 307

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) ⇒ Object



297
298
299
300
301
# File 'lib/fbe/award.rb', line 297

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

#pointsObject



303
304
305
# File 'lib/fbe/award.rb', line 303

def points
  @lines.map { |l| l[:v] }.inject(&:+).to_f.round.to_i
end

#set(var, value) ⇒ Object



293
294
295
# File 'lib/fbe/award.rb', line 293

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