Class: Uniword::Assembly::VariableSubstitutor
- Inherits:
-
Object
- Object
- Uniword::Assembly::VariableSubstitutor
- Defined in:
- lib/uniword/assembly/variable_substitutor.rb
Overview
Performs variable substitution in document text.
Responsibility: Replace variable placeholders with values. Single Responsibility: Only handles text substitution.
The VariableSubstitutor:
-
Finds variable placeholders in text
-
Replaces with provided values
-
Supports nested variables (e.g., docdoc.title)
-
Type-safe substitution with validation
-
Preserves formatting during substitution
Constant Summary collapse
- VARIABLE_PATTERN =
Pattern to match variable placeholders
/\{([a-zA-Z0-9_.]+)\}/
Instance Attribute Summary collapse
-
#variables ⇒ Hash
readonly
Variables for substitution.
Instance Method Summary collapse
-
#get_variable(name) ⇒ Object?
Get variable value.
-
#initialize(variables = {}) ⇒ VariableSubstitutor
constructor
Initialize substitutor with variables.
-
#set_variable(name, value) ⇒ void
Add or update variable.
-
#substitute(text) ⇒ String
Substitute variables in text.
-
#substitute_document(document) ⇒ Document
Substitute variables in document.
-
#variable?(name) ⇒ Boolean
Check if variable is defined.
-
#variable_names ⇒ Array<String>
List all variables.
Constructor Details
#initialize(variables = {}) ⇒ VariableSubstitutor
Initialize substitutor with variables.
41 42 43 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 41 def initialize(variables = {}) @variables = normalize_variables(variables) end |
Instance Attribute Details
#variables ⇒ Hash (readonly)
Returns Variables for substitution.
27 28 29 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 27 def variables @variables end |
Instance Method Details
#get_variable(name) ⇒ Object?
Get variable value.
116 117 118 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 116 def get_variable(name) resolve_variable_path(normalize_key(name)) end |
#set_variable(name, value) ⇒ void
This method returns an undefined value.
Add or update variable.
108 109 110 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 108 def set_variable(name, value) @variables[normalize_key(name)] = value end |
#substitute(text) ⇒ String
Substitute variables in text.
52 53 54 55 56 57 58 59 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 52 def substitute(text) return text unless text.is_a?(String) text.gsub(VARIABLE_PATTERN) do |_match| var_name = ::Regexp.last_match(1) resolve_variable(var_name) end end |
#substitute_document(document) ⇒ Document
Substitute variables in document.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 68 def substitute_document(document) # Process all paragraphs document.paragraphs.each do |paragraph| substitute_paragraph(paragraph) end # Process sections (headers/footers) if document.respond_to?(:sections) document.sections.each do |section| # Process headers if section has them if section.respond_to?(:headers) section.headers.each do |header| header.paragraphs.each do |paragraph| substitute_paragraph(paragraph) end end end # Process footers if section has them next unless section.respond_to?(:footers) section..each do || .paragraphs.each do |paragraph| substitute_paragraph(paragraph) end end end end document end |
#variable?(name) ⇒ Boolean
Check if variable is defined.
124 125 126 127 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 124 def variable?(name) key = normalize_key(name) resolve_variable_path(key) != nil end |
#variable_names ⇒ Array<String>
List all variables.
132 133 134 |
# File 'lib/uniword/assembly/variable_substitutor.rb', line 132 def variable_names collect_all_keys(@variables) end |