Class: DInstaller::QuestionsManager
- Inherits:
-
Object
- Object
- DInstaller::QuestionsManager
- Defined in:
- lib/dinstaller/questions_manager.rb
Overview
Manager for questions
Allows to configure callbacks with the actions to perform when adding, deleting or waiting for questions.
Instance Attribute Summary collapse
- #questions ⇒ Array<Question> readonly
Instance Method Summary collapse
-
#add(question) {|question| ... } ⇒ Boolean
Adds a question.
-
#delete(question) {|question| ... } ⇒ Boolean
Deletes the given question.
-
#initialize(logger) ⇒ QuestionsManager
constructor
Constructor.
-
#on_add(&block) ⇒ Object
Registers a callback to be called when a new question is added.
-
#on_delete(&block) ⇒ Object
Registers a callback to be called when a question is deleted.
-
#on_wait(&block) ⇒ Object
Registers a callback to be called while waiting for questions be answered.
-
#wait ⇒ Object
Waits until all questions are answered.
Constructor Details
#initialize(logger) ⇒ QuestionsManager
Constructor
34 35 36 37 38 39 40 |
# File 'lib/dinstaller/questions_manager.rb', line 34 def initialize(logger) @logger = logger @questions = [] @on_add_callbacks = [] @on_delete_callbacks = [] @on_wait_callbacks = [] end |
Instance Attribute Details
#questions ⇒ Array<Question> (readonly)
29 30 31 |
# File 'lib/dinstaller/questions_manager.rb', line 29 def questions @questions end |
Instance Method Details
#add(question) {|question| ... } ⇒ Boolean
Adds a question
Callbacks are called after adding the question, see #on_add.
50 51 52 53 54 55 56 57 |
# File 'lib/dinstaller/questions_manager.rb', line 50 def add(question) return false if include?(question) questions << question on_add_callbacks.each { |c| c.call(question) } true end |
#delete(question) {|question| ... } ⇒ Boolean
Deletes the given question
Callbacks are called after deleting the question, see #on_delete.
67 68 69 70 71 72 73 74 |
# File 'lib/dinstaller/questions_manager.rb', line 67 def delete(question) return false unless include?(question) questions.delete(question) on_delete_callbacks.each { |c| c.call(question) } true end |
#on_add(&block) ⇒ Object
Registers a callback to be called when a new question is added
92 93 94 |
# File 'lib/dinstaller/questions_manager.rb', line 92 def on_add(&block) on_add_callbacks << block end |
#on_delete(&block) ⇒ Object
Registers a callback to be called when a question is deleted
99 100 101 |
# File 'lib/dinstaller/questions_manager.rb', line 99 def on_delete(&block) on_delete_callbacks << block end |
#on_wait(&block) ⇒ Object
Registers a callback to be called while waiting for questions be answered
106 107 108 |
# File 'lib/dinstaller/questions_manager.rb', line 106 def on_wait(&block) on_wait_callbacks << block end |
#wait ⇒ Object
Waits until all questions are answered
Callbacks are periodically called while waiting, see #on_wait.
79 80 81 82 83 84 85 86 87 |
# File 'lib/dinstaller/questions_manager.rb', line 79 def wait logger.info "Waiting for questions to be answered" loop do on_wait_callbacks.each(&:call) sleep(0.1) break if questions_answered? end end |