Class: Aidp::Harness::UI::QuestionCollector
- Inherits:
-
Base
- Object
- Base
- Aidp::Harness::UI::QuestionCollector
show all
- Defined in:
- lib/aidp/harness/ui/question_collector.rb
Overview
Handles interactive question collection using CLI UI prompts
Defined Under Namespace
Classes: CollectionError, QuestionError, ValidationError
Instance Method Summary
collapse
Constructor Details
#initialize(ui_components = {}, prompt: nil) ⇒ QuestionCollector
Returns a new instance of QuestionCollector.
17
18
19
20
21
|
# File 'lib/aidp/harness/ui/question_collector.rb', line 17
def initialize(ui_components = {}, prompt: nil)
super()
@prompt = prompt || ui_components[:prompt] || TTY::Prompt.new
@validator = ui_components[:validator] || QuestionValidator.new
end
|
Instance Method Details
#collect_questions(questions) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/aidp/harness/ui/question_collector.rb', line 23
def collect_questions(questions)
validate_questions_input(questions)
errors = get_validation_errors(questions)
raise ValidationError, errors.join("\n") unless errors.empty?
responses = {}
questions.each_with_index do |question, index|
question_key = question[:key] || "question_#{index + 1}"
responses[question_key] = collect_single_question(question, index + 1)
end
responses
rescue ValidationError => e
raise e
rescue => e
raise CollectionError, "Failed to collect questions: #{e.message}"
end
|
#collect_single_question(question, number) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/aidp/harness/ui/question_collector.rb', line 42
def collect_single_question(question, number)
validate_question_format(question)
question_text = format_question_text(question, number)
response = prompt_for_response(question_text, question)
validate_response(response, question)
response
rescue => e
raise QuestionError, "Failed to collect question #{number}: #{e.message}"
end
|
#get_validation_errors(questions) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/aidp/harness/ui/question_collector.rb', line 65
def get_validation_errors(questions)
errors = []
questions.each_with_index do |question, index|
validate_question_format(question)
rescue ValidationError => e
errors << "Question #{index + 1}: #{e.message}"
end
errors
end
|
77
78
79
80
81
82
83
84
|
# File 'lib/aidp/harness/ui/question_collector.rb', line 77
def validate_question_format(question)
raise ValidationError, "Question must be a hash" unless question.is_a?(Hash)
raise ValidationError, "Question must have :text key" unless question.key?(:text)
raise ValidationError, "Question text cannot be empty" if question[:text].to_s.strip.empty?
unless question.key?(:type) && question.key?(:required)
raise ValidationError, "Question missing required fields"
end
end
|
#validate_questions(questions) ⇒ Object
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/aidp/harness/ui/question_collector.rb', line 54
def validate_questions(questions)
return true if questions.empty?
questions.all? do |question|
validate_question_format(question)
true
rescue ValidationError
false
end
end
|
86
87
88
89
|
# File 'lib/aidp/harness/ui/question_collector.rb', line 86
def validate_questions_input(questions)
raise ValidationError, "Questions must be an array" unless questions.is_a?(Array)
end
|