Class: Naseweis::Nase
- Inherits:
-
Object
- Object
- Naseweis::Nase
- Defined in:
- lib/naseweis.rb
Overview
A class to read a Weisfile
and gather user input
Instance Attribute Summary collapse
-
#converter ⇒ Converter
readonly
The converter that is used to convert types.
-
#filename ⇒ String
readonly
The path to the file which is used by this Nase.
-
#questions ⇒ Array
readonly
All questions handled by this Nase.
Instance Method Summary collapse
-
#initialize(path) ⇒ Nase
constructor
Create a new Nase which reads questions from the given file.
-
#interrogate(instream: $stdin, outstream: $stdout) ⇒ Hash
Start the question session and return the user answers.
-
#read ⇒ void
Update the questions and re-read them from the file that the Nase was initialized with.
-
#verify(q) ⇒ void
Check whether the given question is wellformed.
Constructor Details
#initialize(path) ⇒ Nase
Create a new Naseweis::Nase which reads questions from the given file
38 39 40 41 42 |
# File 'lib/naseweis.rb', line 38 def initialize(path) @filename = path @questions = {} @converter = Converter.new end |
Instance Attribute Details
#converter ⇒ Converter (readonly)
The converter that is used to convert types
32 33 34 |
# File 'lib/naseweis.rb', line 32 def converter @converter end |
#filename ⇒ String (readonly)
The path to the file which is used by this Naseweis::Nase
32 33 34 |
# File 'lib/naseweis.rb', line 32 def filename @filename end |
#questions ⇒ Array (readonly)
All questions handled by this Naseweis::Nase.
To update the questions, use the #read method.
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
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 |
#read ⇒ void
This method returns an undefined value.
Update the questions and re-read them from the file that the Nase was initialized with
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
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 |