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:



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

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.



7
8
9
# File 'lib/pdk/answer_file.rb', line 7

def answer_file_path
  @answer_file_path
end

#answersObject (readonly)

Returns the value of attribute answers.



6
7
8
# File 'lib/pdk/answer_file.rb', line 6

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.



29
30
31
# File 'lib/pdk/answer_file.rb', line 29

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:



41
42
43
44
45
46
47
48
49
# File 'lib/pdk/answer_file.rb', line 41

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