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 ⇒ Hash
readonly
Variables defined in this bylaw.
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.
420 421 422 423 424 |
# File 'lib/fbe/award.rb', line 420 def initialize @lines = [] @intro = '' @lets = {} end |
Instance Attribute Details
#vars ⇒ Hash (readonly)
Returns Variables defined in this bylaw.
414 415 416 |
# File 'lib/fbe/award.rb', line 414 def vars @vars end |
Instance Method Details
#intro(text) ⇒ String
Sets the introductory text for the bylaw.
446 447 448 |
# File 'lib/fbe/award.rb', line 446 def intro(text) @intro = text end |
#let(key, value) ⇒ Object
Registers a variable with its value for substitution in lines.
471 472 473 |
# File 'lib/fbe/award.rb', line 471 def let(key, value) @lets[key] = value end |
#line(line) ⇒ nil
Adds a line of text to the bylaw, replacing variable references.
458 459 460 461 |
# File 'lib/fbe/award.rb', line 458 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.
484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/fbe/award.rb', line 484 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.
435 436 437 |
# File 'lib/fbe/award.rb', line 435 def revert(num) @lines.slice!(-num, num) end |