Class: Naseweis::Nase

Inherits:
Object
  • Object
show all
Defined in:
lib/naseweis.rb

Overview

A class to read a Weisfile and gather user input

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Nase

Create a new Naseweis::Nase which reads questions from the given file

Parameters:

  • path (String)

    path to the file with the questions



38
39
40
41
42
# File 'lib/naseweis.rb', line 38

def initialize(path)
  @filename = path
  @questions = {}
  @converter = Converter.new
end

Instance Attribute Details

#converterConverter (readonly)

The converter that is used to convert types

Returns:

  • (Converter)

    the current value of converter



32
33
34
# File 'lib/naseweis.rb', line 32

def converter
  @converter
end

#filenameString (readonly)

The path to the file which is used by this Naseweis::Nase

Returns:

  • (String)

    the current value of filename



32
33
34
# File 'lib/naseweis.rb', line 32

def filename
  @filename
end

#questionsArray (readonly)

All questions handled by this Naseweis::Nase.

To update the questions, use the #read method.

Returns:

  • (Array)

    the current value of questions



32
33
34
# File 'lib/naseweis.rb', line 32

def questions
  @questions
end

Instance Method Details

#interrogate(instream: $stdin, outstream: $stdout) ⇒ Hash

Start the question session and return the user answers

Parameters:

  • instream (File) (defaults to: $stdin)

    input stream, i.e. stream where data is read from

  • outstream (File) (defaults to: $stdout)

    output stream, i.e. stream where prompts are printed to

Returns:

  • (Hash)

    Hash of the user answers, where the keys are defined by the question file.



80
81
82
83
84
# File 'lib/naseweis.rb', line 80

def interrogate(instream: $stdin, outstream: $stdout)
  @io = HighLine.new instream, outstream
  ask @questions
  @io = nil
end

#readvoid

This method returns an undefined value.

Update the questions and re-read them from the file that the Nase was initialized with

Raises:



49
50
51
52
53
# File 'lib/naseweis.rb', line 49

def read
  questions = YAML.load_file(@filename)
  verify questions
  @questions = questions
end

#verify(q) ⇒ void

This method returns an undefined value.

Check whether the given question is wellformed

Parameters:

  • q (Hash, Array)

    the question or list of questions to check

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/naseweis.rb', line 60

def verify(q)
  # Currently only checks if the question type is valid
  if q.is_a? Array
    q.each { |x| verify x }
    return
  end
  type = q['type']
  qs = q['q']
  well = type.nil? || @converter.supported_types.include?(type.intern)
  raise WeisheitError, "invalid type #{type}" unless well
  verify qs if qs.is_a? Array
end