Class: Lich::Gemstone::PlaceholderTemplate
- Inherits:
-
Object
- Object
- Lich::Gemstone::PlaceholderTemplate
- Defined in:
- documented/gemstone/creature.rb
Overview
Represents a template with placeholders for dynamic content. This class manages the template string and its placeholders.
Instance Method Summary collapse
-
#initialize(template, placeholders = {}) ⇒ PlaceholderTemplate
constructor
A new instance of PlaceholderTemplate.
- #placeholders ⇒ Object
- #template ⇒ Object
-
#to_display(subs = {}) ⇒ String
Converts the template to a display string, substituting placeholders with values.
- #to_regex(literals = {}) ⇒ Object
Constructor Details
#initialize(template, placeholders = {}) ⇒ PlaceholderTemplate
Returns a new instance of PlaceholderTemplate.
806 807 808 809 810 |
# File 'documented/gemstone/creature.rb', line 806 def initialize(template, placeholders = {}) @template = template @placeholders = placeholders @regex_cache = {} end |
Instance Method Details
#placeholders ⇒ Object
816 817 818 |
# File 'documented/gemstone/creature.rb', line 816 def placeholders @placeholders end |
#template ⇒ Object
812 813 814 |
# File 'documented/gemstone/creature.rb', line 812 def template @template end |
#to_display(subs = {}) ⇒ String
Converts the template to a display string, substituting placeholders with values.
823 824 825 826 827 828 829 830 |
# File 'documented/gemstone/creature.rb', line 823 def to_display(subs = {}) line = @template.dup @placeholders.each do |key, | value = subs[key] || .sample || "" line.gsub!("{#{key}}", value.to_s) end line end |
#to_regex(literals = {}) ⇒ Object
832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
# File 'documented/gemstone/creature.rb', line 832 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 |