Class: Fbe::Award::Bylaw
- Inherits:
-
Object
- Object
- Fbe::Award::Bylaw
- Defined in:
- lib/fbe/award.rb
Overview
A class for generating human-readable bylaws.
This class builds textual descriptions of award bylaws including introductions, calculation steps, and variable substitutions. It produces Markdown-formatted output describing how awards are calculated.
Instance Attribute Summary collapse
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Instance Method Summary collapse
-
#initialize ⇒ Bylaw
constructor
Creates a new empty bylaw.
-
#intro(text) ⇒ String
Sets the introductory text for the bylaw.
-
#let(key, value) ⇒ Object
Registers a variable with its value for substitution in lines.
-
#line(line) ⇒ nil
Adds a line of text to the bylaw, replacing variable references.
-
#markdown ⇒ String
Generates a Markdown-formatted representation of the bylaw.
-
#revert(num) ⇒ Array
Removes the specified number of most recently added lines.
Constructor Details
#initialize ⇒ Bylaw
Creates a new empty bylaw.
409 410 411 412 413 |
# File 'lib/fbe/award.rb', line 409 def initialize @lines = [] @intro = '' @lets = {} end |
Instance Attribute Details
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
403 404 405 |
# File 'lib/fbe/award.rb', line 403 def vars @vars end |
Instance Method Details
#intro(text) ⇒ String
Sets the introductory text for the bylaw.
435 436 437 |
# File 'lib/fbe/award.rb', line 435 def intro(text) @intro = text end |
#let(key, value) ⇒ Object
Registers a variable with its value for substitution in lines.
460 461 462 |
# File 'lib/fbe/award.rb', line 460 def let(key, value) @lets[key] = value end |
#line(line) ⇒ nil
Adds a line of text to the bylaw, replacing variable references.
447 448 449 450 |
# File 'lib/fbe/award.rb', line 447 def line(line) line = line.gsub(/\$\{([a-z_0-9]+)\}/) { |_x| "**#{@lets[Regexp.last_match[1].to_sym]}**" } @lines << line end |
#markdown ⇒ String
Generates a Markdown-formatted representation of the bylaw.
473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/fbe/award.rb', line 473 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 |
#revert(num) ⇒ Array
Removes the specified number of most recently added lines.
424 425 426 |
# File 'lib/fbe/award.rb', line 424 def revert(num) @lines.slice!(-num, num) end |