Class: Lich::Gemstone::PlaceholderTemplate

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

Overview

Templated message with #placeholders for dynamic game output matching and rendering.

Stores a message template and the allowed values for each placeholder (e.g., pronoun can be he/she/it/etc.). Supports rendering a display string by substituting placeholders with specific values (or random samples), and matching game output by building a regex from the template and capturing placeholder values.

Instance Method Summary collapse

Constructor Details

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

Initializes a PlaceholderTemplate.

Stores the template string and placeholder options (allowed values for each placeholder). Initializes an empty regex cache for to_regex optimization.

Parameters:

  • template (String) —

    the message template with placeholder markers

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

    placeholder names => allowed values, e.g. ['he', 'she', 'it']



1036
1037
1038
1039
1040
# File 'documented/gemstone/creature.rb', line 1036

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

Instance Method Details

#match(str, literals = {}) ⇒ Object

Public: Messaging#match calls this on the template it holds. It sat below the private keyword, so every templated message raised NoMethodError on match - the placeholder machinery could render a line but never recognize one.



1107
1108
1109
1110
1111
1112
1113
# File 'documented/gemstone/creature.rb', line 1107

def match(str, literals = {})
  regex = to_regex(literals)
  m = regex.match(str)
  return nil unless m

  m.names.any? ? m.named_captures.transform_keys(&:to_sym) : m.captures
end

#placeholders ⇒ Hash

Returns the placeholder definitions.

Returns:

  • (Hash) —

    placeholder names => allowed values



1052
1053
1054
# File 'documented/gemstone/creature.rb', line 1052

def placeholders
  @placeholders
end

#template ⇒ String

Returns the original message template.

Returns:



1045
1046
1047
# File 'documented/gemstone/creature.rb', line 1045

def template
  @template
end

#to_display(subs = {}) ⇒ String

Renders the template to a display string by substituting placeholders.

Replaces each placeholder with the value from subs if provided, otherwise picks a random value from the placeholder's allowed list (or empty string if the list is empty). Returns the fully-rendered line.

Examples:

template.to_display({pronoun: 'he'}) #=> "He stands up."

Parameters:

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

    placeholder substitutions, e.g. 'she'

Returns:

  • (String) —

    the rendered message



1066
1067
1068
1069
1070
1071
1072
1073
# File 'documented/gemstone/creature.rb', line 1066

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

Builds or retrieves a cached regex that matches the template.

Converts #placeholders into regex alternation groups, with literals and RAW: (raw regex) patterns handled specially. Results are memoized by literals hash to avoid rebuilding on repeated calls with the same substitutions. For templates containing arrays, builds a union of regexes for each element.

Examples:

template.to_regex({pronoun: 'he'}) #=> matches "He stands up."

Parameters:

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

    optional placeholder values for matching, e.g. 'he' (uses Messaging#PLACEHOLDER_MAP values by default)

Returns:

  • (Regexp) —

    regex that matches the template



1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'documented/gemstone/creature.rb', line 1088

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