lib/fbe/award.rb in fbe-0.0.22 vs lib/fbe/award.rb in fbe-0.0.23

- old
+ new

@@ -22,11 +22,29 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. require 'factbase/syntax' -# Award generator. +# A generator of awards. +# +# First, you should create a policy, using the same Lisp-ish syntax as +# we use in queries to a Factbase, for example: +# +# require 'fbe/award' +# a = Fbe::Award.new('(award (in loc "lines") (give (times loc 5) "for LoC"))') +# +# Then, you can either get a bill from it: +# +# b = a.bill(loc: 345) +# puts b.points # how many points to reward, a number +# puts b.greeting # how to explain the reward, a text +# +# Or else, you can get a policy text: +# +# p = a.policy +# puts p.markdown # Markdown of the policy +# # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2024 Yegor Bugayenko # License:: MIT class Fbe::Award def initialize(query = J.pmp.hr.send($judge.gsub('-', '_'))) @@ -264,11 +282,18 @@ @lines.map { |l| l[:v] }.inject(&:+).to_i end def greeting items = @lines.map { |l| "#{format('%+d', l[:v])} #{l[:t]}" } - "You've earned #{format('%+d', points)} points for this: #{items.join('; ')}. " + 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 end # A policy. class Policy @@ -287,10 +312,10 @@ def intro(text) @intro = text end def line(line) - line = line.gsub(/\$\{([a-z_0-9]+)\}/) { |_x| @lets[Regexp.last_match[1].to_sym] } + line = line.gsub(/\$\{([a-z_0-9]+)\}/) { |_x| "**#{@lets[Regexp.last_match[1].to_sym]}**" } @lines << line end def let(key, value) @lets[key] = value