Class: PromptEngine::VariableDetector
- Inherits:
-
Object
- Object
- PromptEngine::VariableDetector
- Defined in:
- app/services/prompt_engine/variable_detector.rb
Constant Summary collapse
- VARIABLE_PATTERN =
Regex pattern to match {variable_name} syntax Allows letters, numbers, underscores, and dots for nested variables
/\{\{([a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*)\}\}/
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Instance Method Summary collapse
-
#extract_variables ⇒ Object
Extract all variables from the content.
-
#has_variables? ⇒ Boolean
Check if content contains variables.
-
#initialize(content) ⇒ VariableDetector
constructor
A new instance of VariableDetector.
-
#render(variables = {}) ⇒ Object
Replace variables with provided values.
-
#validate_variables(provided_variables = {}) ⇒ Object
Validate that all required variables are provided.
-
#variable_count ⇒ Object
Count unique variables.
-
#variable_names ⇒ Object
Get just the variable names as an array.
Constructor Details
#initialize(content) ⇒ VariableDetector
Returns a new instance of VariableDetector.
9 10 11 |
# File 'app/services/prompt_engine/variable_detector.rb', line 9 def initialize(content) @content = content.to_s end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
7 8 9 |
# File 'app/services/prompt_engine/variable_detector.rb', line 7 def content @content end |
Instance Method Details
#extract_variables ⇒ Object
Extract all variables from the content
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/services/prompt_engine/variable_detector.rb', line 14 def extract_variables variables = [] content.scan(VARIABLE_PATTERN) do |match| variable_name = match.first variables << { name: variable_name, placeholder: "{{#{variable_name}}}", position: Regexp.last_match.offset(0), type: infer_type(variable_name), required: true } end # Remove duplicates while preserving order variables.uniq { |v| v[:name] } end |
#has_variables? ⇒ Boolean
Check if content contains variables
38 39 40 |
# File 'app/services/prompt_engine/variable_detector.rb', line 38 def has_variables? content.match?(VARIABLE_PATTERN) end |
#render(variables = {}) ⇒ Object
Replace variables with provided values
48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/services/prompt_engine/variable_detector.rb', line 48 def render(variables = {}) rendered = content.dup variables.each do |key, value| # Support both string and symbol keys placeholder = "{{#{key}}}" rendered.gsub!(placeholder, value.to_s) end rendered end |
#validate_variables(provided_variables = {}) ⇒ Object
Validate that all required variables are provided
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/services/prompt_engine/variable_detector.rb', line 61 def validate_variables(provided_variables = {}) missing = [] stringified_keys = provided_variables.stringify_keys variable_names.each do |var_name| unless stringified_keys.key?(var_name) missing << var_name end end { valid: missing.empty?, missing_variables: missing } end |
#variable_count ⇒ Object
Count unique variables
43 44 45 |
# File 'app/services/prompt_engine/variable_detector.rb', line 43 def variable_count variable_names.count end |
#variable_names ⇒ Object
Get just the variable names as an array
33 34 35 |
# File 'app/services/prompt_engine/variable_detector.rb', line 33 def variable_names extract_variables.map { |v| v[:name] } end |