Class: Test

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Test

Returns a new instance of Test.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dsl_test/Test.rb', line 5

def initialize(name, &block)
    @name = name
    @preguntas = []
    @tipos = []
    @imagenes = []
    @respuestas = []
    @formato_presentar = []
    @formato_resultado = []

    
    if block_given?  
        if block.arity == 1
            yield self
            puts "yield self"
        else
            instance_eval(&block)
            puts "instance_eval"
            puts block.arity
        end
    end
end

Instance Attribute Details

#formato_presentarObject

Returns the value of attribute formato_presentar.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def formato_presentar
  @formato_presentar
end

#formato_resultadoObject

Returns the value of attribute formato_resultado.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def formato_resultado
  @formato_resultado
end

#imagenesObject

Returns the value of attribute imagenes.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def imagenes
  @imagenes
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def name
  @name
end

#preguntasObject

Returns the value of attribute preguntas.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def preguntas
  @preguntas
end

#respuesta_correctaObject

Returns the value of attribute respuesta_correcta.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def respuesta_correcta
  @respuesta_correcta
end

#respuestasObject

Returns the value of attribute respuestas.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def respuestas
  @respuestas
end

#tiposObject

Returns the value of attribute tipos.



3
4
5
# File 'lib/dsl_test/Test.rb', line 3

def tipos
  @tipos
end

Instance Method Details

#concatenar_csvObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dsl_test/Test.rb', line 133

def concatenar_csv
    data = File.new("./data/Data.csv", "a")
    #crea array de string con los elementos del directorio
    archivos = Dir.children("./csv")
    archivos.each_with_index do |filename,index|
        File.open("./csv/#{filename}","r") {|file| 
            data.write file.read
            data.write "\n"
        } 
    end

end

#pregunta(texto, options = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/dsl_test/Test.rb', line 36

def pregunta(texto, options = {})
    @preguntas << texto
    @tipos << options[:tipo]
    @imagenes << options[:imagen]
    @respuestas << options[:respuestas]
end

#presentacion(options = {}) ⇒ Object



27
28
29
30
# File 'lib/dsl_test/Test.rb', line 27

def presentacion( options = {})
    @formato_presentar << options[:presentar]

end

#resultado(options = {}) ⇒ Object



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

def resultado( options = {}) 
    @formato_resultado << options[:guardar_resultado_en] 
end

#to_csv(id, edad, respuestas = []) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dsl_test/Test.rb', line 117

def to_csv(id,edad,respuestas = [])
    filecsv = File.new("./csv/#{id}.csv", "w+")
    filecsv.write "#{id}"
    filecsv.write ","
    filecsv.write "#{edad}"
    filecsv.write ","
    respuestas.each_with_index do |x, index| 
        filecsv.write x 
        if index <= respuestas.size - 2
            filecsv.write ","
        end
    end

    filecsv
end

#to_html(name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dsl_test/Test.rb', line 47

def to_html(name)
    fileHtml = File.new("./views/#{@name}.html", "w+")
    fileHtml.puts "<!DOCTYPE html>"
    fileHtml.puts "<html lang='es-ES'>"
    fileHtml.puts "<head>"
    fileHtml.puts "<meta charset='UTF-8'>"
    fileHtml.puts "<link rel=\"stylesheet\" href=\"test.css\">"
    fileHtml.puts "<meta http-equiv='X-UA-Compatible' content='IE=edge'>"
    fileHtml.puts "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
    fileHtml.puts "<title>Documento</title>"
    fileHtml.puts "</head>"
    fileHtml.puts "<body>"
    fileHtml.puts "<h1>#@name</h1>"
    fileHtml.puts "<form action=\"\">"
    fileHtml.puts "<label for=\"id_usuario\">Identificador:</label>"
    fileHtml.puts "<input required type=\"text\" id=\"id_usuario\" name=\"id_usuario\">"
    fileHtml.puts "<label for=\"edad\">Edad:</label>"
    fileHtml.puts "<input required type=\"text\" id=\"edad\" name=\"edad\">"
    @preguntas.each_with_index do |pregunta, index|
        fileHtml.puts "<h3>#{index + 1}) #{pregunta}</h3>"
        fileHtml.puts "<h3>#{@tipos[index]}</h3>"
        fileHtml.puts "<img alt='Imagen pregunta #{index + 1}' src='../#{@imagenes[index]}'>"
        respuestasLocal = @respuestas[index].split("\n")
        fileHtml.puts "<input type ='radio' id='answer#{index + 1}_1' name='answer1' value='#{respuestasLocal[1]}' required>"
        fileHtml.puts "<label for=\"answer1\">#{respuestasLocal[1]}</label><br>"
        fileHtml.puts "<input type='radio' id='answer#{index + 1}_2' name='answer2' value='#{respuestasLocal[2]}' required>"
        fileHtml.puts "<label for=\"answer2\">#{respuestasLocal[2]}</label><br>"
        fileHtml.puts "<input type='radio' id='answer#{index + 1}_3' name='answer3' value='#{respuestasLocal[3]}' required>"
        fileHtml.puts "<label for=\"answer3\">#{respuestasLocal[3]}</label><br>"
        fileHtml.puts "<input type='radio' id='answer#{index + 1}_4' name='answer4' value='#{respuestasLocal[4]}' required>"
        fileHtml.puts "<label for=\"answer4\">#{respuestasLocal[4]}</label><br>"
    end
    fileHtml.puts "<button type=\"submit\" onclick=\"submitForm(event)\">Enviar</button>"
    fileHtml.puts "<script type=\"text/javascript\" src=\"./lib/dsl_test/index.js\"></script>"
    fileHtml.puts "</form>"
    fileHtml.puts "\t"
    fileHtml.puts "</body>"
    fileHtml.puts "</html>"
    fileHtml.close()

    fileHtml
end

#to_jsObject



43
44
45
# File 'lib/dsl_test/Test.rb', line 43

def to_js
    
end

#to_sObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dsl_test/Test.rb', line 90

def to_s
    output = @name
    output << "\n#{'=' * @name.size}\n\n"
    @preguntas.each_with_index do |pregunta, index|
        output << "#{index + 1}) #{pregunta}\n"
        output << " #{@tipos[index]}\n"
        output << " #{@imagenes[index]}\n"
        output << " #{@respuestas[index]}\n"
        output << "\n"
    end
    output
end

#to_textoObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dsl_test/Test.rb', line 103

def to_texto
    file = File.new("./views/test.txt", "w+")
    file.puts  "#@name"
    file.puts  "#{'=' * @name.size}\n\n"
    @preguntas.each_with_index do |pregunta, index|
        file.puts "#{index + 1}) #{pregunta}\n"
        file.puts " #{@tipos[index]}\n"
        file.puts " #{@imagenes[index]}\n"
        file.puts " #{@respuestas[index]}\n"
        file.puts "\n"
    end
    file
end