lib/fbe/award.rb in fbe-0.0.20 vs lib/fbe/award.rb in fbe-0.0.21

- old
+ new

@@ -65,10 +65,16 @@ @operands.each do |o| o.bill_to(bill) rescue StandardError => e raise "Failure in #{o}: #{e.message}" end + when :aka + @operands[0..-2].each do |o| + o.bill_to(bill) + rescue StandardError => e + raise "Failure in #{o}: #{e.message}" + end when :let, :set bill.set(@operands[0], to_val(@operands[1], bill)) when :give text = @operands[1] text = '' if text.nil? @@ -145,11 +151,11 @@ when :total 'total' when :if "if #{to_p(@operands[0])} then #{to_p(@operands[1])} else #{to_p(@operands[2])}" when :eq - "#{to_p(@operands[0])} == #{to_p(@operands[1])}" + "#{to_p(@operands[0])} = #{to_p(@operands[1])}" when :lt "#{to_p(@operands[0])} < #{to_p(@operands[1])}" when :lte "#{to_p(@operands[0])} ≤ #{to_p(@operands[1])}" when :gt @@ -181,16 +187,25 @@ @operands.each do |o| o.publish_to(policy) rescue StandardError => e raise "Failure in #{o}: #{e.message}" end + when :aka + @operands[0..-2].each do |o| + o.publish_to(policy) + rescue StandardError => e + raise "Failure in #{o}: #{e.message}" + end + policy.revert(@operands.size - 1) + policy.line(to_p(@operands[@operands.size - 1])) when :explain policy.intro(to_p(@operands[0])) when :in policy.line("assume that #{to_p(@operands[0])} is #{to_p(@operands[1])}") when :let policy.line("let #{to_p(@operands[0])} be equal to #{to_p(@operands[1])}") + policy.let(@operands[0], @operands[1]) when :set policy.line("set #{to_p(@operands[0])} to #{to_p(@operands[1])}") when :give policy.line("award #{to_p(@operands[0])}") else @@ -215,11 +230,11 @@ 7 => '₇', 8 => '₈', 9 => '₉' } s.gsub!(/([a-z]+)([0-9])/) { |_| "#{Regexp.last_match[1]}#{subs[Regexp.last_match[2].to_i]}" } - "_#{s.gsub('_', '\\_')}_" + "_#{s.gsub('_', '-')}_" when Integer, Float "**#{any}**" else any end @@ -260,24 +275,38 @@ attr_reader :vars def initialize @lines = [] @intro = '' + @lets = {} end + def revert(num) + @lines.slice!(-num, num) + end + def intro(text) @intro = text end def line(line) + 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 + end + def markdown pars = [] pars << "#{@intro}." unless @intro.empty? pars << 'Here is how it\'s calculated:' - pars += @lines.each_with_index.map { |t, i| "#{i.zero? ? 'First' : 'Then'}, #{t}." } + if @lines.size == 1 + pars << "Just #{@lines.first}." + else + pars += @lines.each_with_index.map { |t, i| "#{i.zero? ? 'First' : 'Then'}, #{t}." } + end pars.join(' ').gsub('. Then, award ', ', and award ') end end end