Class: ULL::ETSII::Alu3177::Quiz::Quiz

Inherits:
Object
  • Object
show all
Defined in:
lib/ULL-ETSII-Alu3177-Quiz/quiz.rb

Overview

Clase que permite la construcción de cuestionarios tipo test mediante un DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Quiz

Recibe el nombre del test y un bloque con las preguntas y sus respuestas

Ejemplo de uso:

quiz = Quiz.new("Test 1"){
  question "Pregunta 1",
    wrong => "Respuesta incorrecta 1",
    wrong => "Respuesta incorrecta 2",
    wrong => "Respuesta incorrecta 3",
    right => "Respuesta correcta"
}


28
29
30
31
32
33
34
35
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 28

def initialize(name, &block)
    @counter = 0
    @aciertos = 0
    @name = name
    @questions = []

    instance_eval &block
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 16

def name
  @name
end

#questionsObject

Returns the value of attribute questions.



16
17
18
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 16

def questions
  @questions
end

Instance Method Details

#question(title, anss) ⇒ Object

Método que recibe el título title de la pregunta y una serie de parámetros como respuestas



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 52

def question(title, anss)
    answers = []

    anss.each do |ans|
        a = Answer.new(ans)
        answers << a
    end

    q = Question.new(title, answers)
    questions << q
end

#rightObject

Pregunta correcta



46
47
48
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 46

def right
    :right
end

#runObject

Ejecuta el test por consola. Presenta cada pregunta y las posibles respuestas. Al final muestra los resultados.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 66

def run
    aciertos = 0
    puts name
    questions.each do |q|
        puts q
        print "Su respuesta: "
        resp = gets.chomp.to_i
        raise IndexError, "Answer must be between 1 and #{q.answers.size}." unless resp <= q.answers.size and resp > 0
        if q.answers[resp-1].state
            puts "Correcto!".colorize(:light_green)
            @aciertos += 1
        else
            correcta = q.answers.select { |ans| ans.state }.first
            puts "Fallo, la respuesta correcta era #{correcta}".colorize(:red)
        end
        puts
    end
    puts "Has acertado el #{(@aciertos/questions.size.to_f)*100}% de las preguntas [#{@aciertos} de #{questions.size}]."
end

#to_htmlObject

Genera el test en formato html. Dicho test es totalmente funcional, permitiendo la selección de respuestas y la corrección de las mismas

Raises:

  • (IOError)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 98

def to_html
    # SetUp del fichero de salida
    if not Dir.exist? "html"
        # Copiamos el directorio html con los ficheros básicos
        require 'fileutils'
        FileUtils.cp_r File.expand_path(File.dirname(__FILE__)) + '/html', 'html'
    end
    # Generamos HTML
    htmlFile = File.new("html/#{name.gsub(/[\ \\\/:]/, '_')}.html", "w")
    raise IOError, 'Can\'t access to html output file' unless htmlFile
    # Generamos JavaScript
    jsFile = File.new("html/js/quiz.js", "w")
    raise IOError, 'Can\'t access to javascript output file' unless jsFile
    
    # Construimos los ERB y los escribimos en los ficheros
    require 'templates'
    rhtml = ERB.new(HTML_TEMPLATE)
    htmlFile.syswrite(rhtml.result(binding))
    htmlFile.close
    
    rjs = ERB.new(JAVASCRIPT_TEMPLATE)
    jsFile.syswrite(rjs.result(binding))
    jsFile.close
    
    
end

#to_sObject

Representación visual de un Test en forma de String.



87
88
89
90
91
92
93
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 87

def to_s
    out = name + "\n"
    questions.each do |q|
        out << "#{q}\n"
    end
    out
end

#wrongObject

Pregunta incorrecta

Avoid collisions



40
41
42
43
# File 'lib/ULL-ETSII-Alu3177-Quiz/quiz.rb', line 40

def wrong
    @counter += 1
    [@counter, WRONG]
end