Class: Observ::NullPrompt
- Inherits:
-
Object
- Object
- Observ::NullPrompt
- Defined in:
- app/models/observ/null_prompt.rb
Overview
Null Object pattern for Prompt Used when a prompt is not found, providing a fallback with the same interface
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
Instance Method Summary collapse
- #archived? ⇒ Boolean
-
#compile(variables = {}) ⇒ Object
Compile prompt with Mustache templating (same as Prompt).
-
#compile_with_validation(variables = {}) ⇒ Object
Compile with validation (raises if missing top-level variables) Note: Variables inside sections (loops) are validated at render time by Mustache.
- #draft? ⇒ Boolean
- #id ⇒ Object
-
#initialize(name:, fallback_text:) ⇒ NullPrompt
constructor
A new instance of NullPrompt.
- #inspect ⇒ Object
- #persisted? ⇒ Boolean
- #production? ⇒ Boolean
-
#required_variables ⇒ Object
Extract top-level variables from template (for validation purposes).
-
#state ⇒ Object
Null prompts are always in a "fallback" state.
-
#to_s ⇒ Object
For logging/debugging.
-
#version ⇒ Object
Returns nil to indicate this is not a real prompt.
Constructor Details
#initialize(name:, fallback_text:) ⇒ NullPrompt
Returns a new instance of NullPrompt.
11 12 13 14 15 |
# File 'app/models/observ/null_prompt.rb', line 11 def initialize(name:, fallback_text:) @name = name @prompt = fallback_text @config = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'app/models/observ/null_prompt.rb', line 9 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'app/models/observ/null_prompt.rb', line 9 def name @name end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
9 10 11 |
# File 'app/models/observ/null_prompt.rb', line 9 def prompt @prompt end |
Instance Method Details
#archived? ⇒ Boolean
67 68 69 |
# File 'app/models/observ/null_prompt.rb', line 67 def archived? false end |
#compile(variables = {}) ⇒ Object
Compile prompt with Mustache templating (same as Prompt)
23 24 25 26 27 |
# File 'app/models/observ/null_prompt.rb', line 23 def compile(variables = {}) return @prompt if variables.empty? Mustache.render(@prompt, variables) end |
#compile_with_validation(variables = {}) ⇒ Object
Compile with validation (raises if missing top-level variables) Note: Variables inside sections (loops) are validated at render time by Mustache
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/observ/null_prompt.rb', line 31 def compile_with_validation(variables = {}) required_vars = required_variables provided_keys = variables.keys.map(&:to_s) missing_vars = required_vars.reject do |var| # Handle dot notation (e.g., "user.name" - check if "user" key exists) root_key = var.split(".").first provided_keys.include?(var) || provided_keys.include?(root_key) end if missing_vars.any? raise Observ::VariableSubstitutionError, "Missing variables: #{missing_vars.join(', ')}" end compile(variables) end |
#draft? ⇒ Boolean
59 60 61 |
# File 'app/models/observ/null_prompt.rb', line 59 def draft? false end |
#id ⇒ Object
75 76 77 |
# File 'app/models/observ/null_prompt.rb', line 75 def id nil end |
#inspect ⇒ Object
84 85 86 |
# File 'app/models/observ/null_prompt.rb', line 84 def inspect "#<Observ::NullPrompt name: #{name.inspect}, fallback: #{prompt[0..50].inspect}...>" end |
#persisted? ⇒ Boolean
71 72 73 |
# File 'app/models/observ/null_prompt.rb', line 71 def persisted? false end |
#production? ⇒ Boolean
63 64 65 |
# File 'app/models/observ/null_prompt.rb', line 63 def production? false end |
#required_variables ⇒ Object
Extract top-level variables from template (for validation purposes)
49 50 51 52 |
# File 'app/models/observ/null_prompt.rb', line 49 def required_variables template_without_sections = strip_sections(@prompt) template_without_sections.scan(/\{\{([^#\^\/!>\{\s][^}\s]*)\}\}/).flatten.uniq end |
#state ⇒ Object
Null prompts are always in a "fallback" state
55 56 57 |
# File 'app/models/observ/null_prompt.rb', line 55 def state "fallback" end |
#to_s ⇒ Object
For logging/debugging
80 81 82 |
# File 'app/models/observ/null_prompt.rb', line 80 def to_s "NullPrompt(#{name})" end |
#version ⇒ Object
Returns nil to indicate this is not a real prompt
18 19 20 |
# File 'app/models/observ/null_prompt.rb', line 18 def version nil end |