Class: Fbe::Award::Bylaw

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeBylaw

Creates a new empty bylaw.

Examples:

bylaw = Fbe::Award::Bylaw.new


420
421
422
423
424
# File 'lib/fbe/award.rb', line 420

def initialize
  @lines = []
  @intro = ''
  @lets = {}
end

Instance Attribute Details

#varsHash (readonly)

Returns Variables defined in this bylaw.

Returns:

  • (Hash)

    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.

Examples:

bylaw = Fbe::Award::Bylaw.new
bylaw.intro("This bylaw determines rewards for code contributions")

Parameters:

  • text (String)

    The introductory text to set

Returns:

  • (String)

    The introductory text



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.

Examples:

bylaw = Fbe::Award::Bylaw.new
bylaw.let(:points, 50)

Parameters:

  • key (Symbol)

    The variable name

  • value (Object)

    The value to associate with the variable

Returns:

  • (Object)

    The assigned value



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.

Examples:

bylaw = Fbe::Award::Bylaw.new
bylaw.let(:points, 50)
bylaw.line("award ${points} points")

Parameters:

  • line (String)

    The line of text to add

Returns:

  • (nil)


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

#markdownString

Generates a Markdown-formatted representation of the bylaw.

Examples:

bylaw = Fbe::Award::Bylaw.new
bylaw.intro("This bylaw determines rewards for code contributions")
bylaw.line("award **50** points")
bylaw.markdown
#=> "This bylaw determines rewards for code contributions. Just award **50** points."

Returns:

  • (String)

    The bylaw formatted as Markdown text



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.

Examples:

bylaw = Fbe::Award::Bylaw.new
bylaw.line("award 50 points")
bylaw.line("award 30 points")
bylaw.revert(1) # Removes "award 30 points"

Parameters:

  • num (Integer)

    The number of lines to remove from the end

Returns:

  • (Array)

    The removed lines



435
436
437
# File 'lib/fbe/award.rb', line 435

def revert(num)
  @lines.slice!(-num, num)
end