Class: PDK::AnswerFile

Inherits:
Object
  • Object
show all
Includes:
Util::Filesystem
Defined in:
lib/pdk/answer_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Filesystem

directory?, exist?, expand_path, file?, fnmatch, glob, mkdir_p, read_file, readable?, rm, write_file

Constructor Details

#initialize(answer_file_path = nil) ⇒ AnswerFile

Initialises the AnswerFile object, which stores the responses to certain interactive questions.

Parameters:

  • answer_file_path (String, nil) (defaults to: nil)

    The path on disk to the file where the answers will be stored and read from. If not specified (or ‘nil`), the default path will be used (see #default_answer_file_path).

Raises:



34
35
36
37
# File 'lib/pdk/answer_file.rb', line 34

def initialize(answer_file_path = nil)
  @answer_file_path = answer_file_path || default_answer_file_path
  @answers = read_from_disk
end

Instance Attribute Details

#answer_file_pathObject (readonly)

Returns the value of attribute answer_file_path.



22
23
24
# File 'lib/pdk/answer_file.rb', line 22

def answer_file_path
  @answer_file_path
end

#answersObject (readonly)

Returns the value of attribute answers.



21
22
23
# File 'lib/pdk/answer_file.rb', line 21

def answers
  @answers
end

Instance Method Details

#[](question) ⇒ Object

Retrieve the stored answer to a question.

Parameters:

  • question (String)

    The question name/identifying string.

Returns:

  • (Object)

    The answer to the question, or ‘nil` if no answer found.



44
45
46
# File 'lib/pdk/answer_file.rb', line 44

def [](question)
  answers[question]
end

#update!(new_answers = {}) ⇒ Object

Update the stored answers in memory and then save them to disk.

Parameters:

  • new_answers (Hash{String => Object}) (defaults to: {})

    The new questions and answers to be merged into the existing answers.

Raises:



56
57
58
59
60
61
62
63
64
# File 'lib/pdk/answer_file.rb', line 56

def update!(new_answers = {})
  unless new_answers.is_a?(Hash)
    raise PDK::CLI::FatalError, _('Answer file can be updated only with a Hash')
  end

  answers.merge!(new_answers)

  save_to_disk
end