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.
407 408 409 410 411 |
# File 'lib/fbe/award.rb', line 407 def initialize @lines = [] @intro = '' @lets = {} end |
Instance Attribute Details
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
401 402 403 |
# File 'lib/fbe/award.rb', line 401 def vars @vars end |
Instance Method Details
#intro(text) ⇒ String
Sets the introductory text for the bylaw.
433 434 435 |
# File 'lib/fbe/award.rb', line 433 def intro(text) @intro = text end |
#let(key, value) ⇒ Object
Registers a variable with its value for substitution in lines.
458 459 460 |
# File 'lib/fbe/award.rb', line 458 def let(key, value) @lets[key] = value end |
#line(line) ⇒ nil
Adds a line of text to the bylaw, replacing variable references.
445 446 447 448 |
# File 'lib/fbe/award.rb', line 445 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.
471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/fbe/award.rb', line 471 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.
422 423 424 |
# File 'lib/fbe/award.rb', line 422 def revert(num) @lines.slice!(-num, num) end |