Class: DInstaller::QuestionsManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#questionsArray<Question> (readonly)

Returns:



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.

Parameters:

Yield Parameters:

  • question (Question)

    added question

Returns:

  • (Boolean)

    whether the question was added



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.

Parameters:

Yield Parameters:

  • question (Question)

    deleted question

Returns:

  • (Boolean)

    whether the question was deleted



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

Parameters:

  • block (Proc)


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

Parameters:

  • block (Proc)


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

Parameters:

  • block (Proc)


106
107
108
# File 'lib/dinstaller/questions_manager.rb', line 106

def on_wait(&block)
  on_wait_callbacks << block
end

#waitObject

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