class Fbe::Award::Bylaw
It produces Markdown-formatted output describing how awards are calculated.
introductions, calculation steps, and variable substitutions.
This class builds textual descriptions of award bylaws including
A class for generating human-readable bylaws.
def initialize
@example
Creates a new empty bylaw.
def initialize @lines = [] @intro = '' @lets = {} end
def intro(text)
-
(String)
- The introductory text
Parameters:
-
text
(String
) -- The introductory text to set
def intro(text) @intro = text end
def let(key, value)
-
(Object)
- The assigned value
Parameters:
-
value
(Object
) -- The value to associate with the variable -
key
(Symbol
) -- The variable name
def let(key, value) @lets[key] = value end
def line(line)
-
(nil)
-
Parameters:
-
line
(String
) -- The line of text to add
def line(line) line = line.gsub(/\$\{([a-z_0-9]+)\}/) { |_x| "**#{@lets[Regexp.last_match[1].to_sym]}**" } @lines << line end
def markdown
-
(String)
- The bylaw formatted as Markdown text
def markdown pars = [] pars << "#{@intro}." unless @intro.empty? pars << 'Here is how it\'s calculated:' 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 ').gsub(/\s{2,}/, ' ') end
def revert(num)
-
(Array)
- The removed lines
Parameters:
-
num
(Integer
) -- The number of lines to remove from the end
def revert(num) @lines.slice!(-num, num) end