Class: Gm::Notepad::LineEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/gm/notepad/line_evaluator.rb

Overview

Responsible for recording entries and then dumping them accordingly.

Constant Summary collapse

TABLE_NAME_REGEXP =
%r{(?<table_name_container>\{(?<table_name>[^\{\}]+)\})}
DICE_REGEXP =
%r{(?<dice_container>\[(?<dice>[^\]]+)\])}

Instance Method Summary collapse

Instance Method Details

#call(line:, table_lookup_function:, expand_line: true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gm/notepad/line_evaluator.rb', line 8

def call(line:, table_lookup_function:, expand_line: true)
  return line unless expand_line
  match = line.match(TABLE_NAME_REGEXP)
  while match
    evaluated_table_name = table_lookup_function.call(table_name: match[:table_name].strip)
    line = line.sub(match[:table_name_container], evaluated_table_name)
    match = line.match(TABLE_NAME_REGEXP)
  end
  while match = line.match(DICE_REGEXP)
    if parsed_dice = Dice.parse(match[:dice])
      evaluated_dice = "#{parsed_dice.evaluate}"
    else
      evaluated_dice = "(#{match[:dice]})"
    end
    line = line.sub(match[:dice_container], evaluated_dice)
  end
  line
end