Class: Lich::Gemstone::PlaceholderTemplate

Inherits:
Object
  • Object
show all
Defined in:
documented/gemstone/creature.rb

Overview

Renders and matches dynamic message templates with placeholder substitution.

Used by Messaging to support dynamic text: templates contain placeholders like pronoun, direction, and weapon which are substituted with game values at display time. Supports random variation when substitutions are not provided, regex matching to extract captured groups, and caching of compiled regexes for performance.

Instance Method Summary collapse

Constructor Details

#initialize(template, placeholders = {}) ⇒ void

Initializes a placeholder template and caches for rendering/matching.

Parameters:

  • template (String)

    the template text with name placeholders

  • placeholders (Hash) (defaults to: {})

    map of placeholder name -> [options] for random substitution or regex generation



844
845
846
847
848
# File 'documented/gemstone/creature.rb', line 844

def initialize(template, placeholders = {})
  @template = template
  @placeholders = placeholders
  @regex_cache = {}
end

Instance Method Details

#placeholdersHash

Returns the placeholder definitions.

Returns:



860
861
862
# File 'documented/gemstone/creature.rb', line 860

def placeholders
  @placeholders
end

#templateString

Returns the template string.

Returns:



853
854
855
# File 'documented/gemstone/creature.rb', line 853

def template
  @template
end

#to_display(subs = {}) ⇒ String

Renders the template with placeholders substituted or filled randomly.

For each placeholder, uses the supplied substitution value if present, otherwise picks a random option from the placeholder definition, defaulting to an empty string if no options exist.

Examples:

tmpl = PlaceholderTemplate.new("A {adjective} orc",
  adjective: ["red", "blue"])
tmpl.to_display(adjective: "green") #=> "A green orc"
tmpl.to_display() #=> "A red orc" or "A blue orc"

Parameters:

  • subs (Hash) (defaults to: {})

    optional substitutions, e.g. { pronoun: "He" }

Returns:



877
878
879
880
881
882
883
884
# File 'documented/gemstone/creature.rb', line 877

def to_display(subs = {})
  line = @template.dup
  @placeholders.each do |key, options|
    value = subs[key] || options.sample || ""
    line.gsub!("{#{key}}", value.to_s)
  end
  line
end

#to_regex(literals = {}) ⇒ Regexp

Compiles the template to a regex pattern for matching.

Converts the template string to a regex where each placeholder becomes a named capture group or raw regex pattern. Results are cached by substitution hash to avoid rebuilding on repeated calls.

Examples:

tmpl = PlaceholderTemplate.new("The {noun} arrives from {direction}",
  noun: ["orc", "goblin"], direction: ["north", "south"])
tmpl.to_regex().match("The orc arrives from north")
#=> #<MatchData "The orc arrives from north" noun:"orc" direction:"north">

Parameters:

  • literals (Hash) (defaults to: {})

    optional specific literal values for placeholders; defaults to all options from the placeholder definition

Returns:

  • (Regexp)

    compiled regex pattern with named captures



900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'documented/gemstone/creature.rb', line 900

def to_regex(literals = {})
  # Use cache to avoid rebuilding regex on every call
  cache_key = literals.hash
  return @regex_cache[cache_key] if @regex_cache[cache_key]

  regex = if @template.is_a?(Array)
            regexes = @template.map { |t| self.class.new(t, @placeholders).to_regex(literals) }
            Regexp.union(*regexes)
          else
            build_regex(literals)
          end

  @regex_cache[cache_key] = regex
end