kaya 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +26 -0
  3. data/CHANGELOG +52 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/NOTAS +19 -0
  7. data/README.md +279 -0
  8. data/Rakefile +1 -0
  9. data/bin/kaya +4 -0
  10. data/documentation/api.md +47 -0
  11. data/documentation/api_execution_data.md +47 -0
  12. data/documentation/configuration.md +13 -0
  13. data/documentation/custom_parameters.md +117 -0
  14. data/documentation/document_kaya.md +122 -0
  15. data/documentation/link.md +12 -0
  16. data/documentation/results.md +119 -0
  17. data/documentation/start.md +19 -0
  18. data/documentation/suites.md +60 -0
  19. data/documentation/test_suite_information.md +9 -0
  20. data/kaya.gemspec +42 -0
  21. data/lib/generators/task_rack.rb +160 -0
  22. data/lib/generators/templates/Gemfile.tt +2 -0
  23. data/lib/generators/templates/config.ru.tt +2 -0
  24. data/lib/generators/templates/kaya_conf.tt +49 -0
  25. data/lib/generators/templates/kaya_log.tt +0 -0
  26. data/lib/generators/templates/sidekiq_log.tt +0 -0
  27. data/lib/generators/templates/tasks.kaya.tt +10 -0
  28. data/lib/generators/templates/unicorn.rb.tt +21 -0
  29. data/lib/kaya/API/error.rb +17 -0
  30. data/lib/kaya/API/execution.rb +141 -0
  31. data/lib/kaya/API/result.rb +49 -0
  32. data/lib/kaya/API/results.rb +31 -0
  33. data/lib/kaya/API/suite.rb +42 -0
  34. data/lib/kaya/API/suites.rb +36 -0
  35. data/lib/kaya/background_jobs/sidekiq.rb +35 -0
  36. data/lib/kaya/background_jobs/workers/execution_checker.rb +20 -0
  37. data/lib/kaya/background_jobs/workers/garbage_cleaner.rb +31 -0
  38. data/lib/kaya/commands/bye.rb +12 -0
  39. data/lib/kaya/commands/help.rb +13 -0
  40. data/lib/kaya/commands/install.rb +27 -0
  41. data/lib/kaya/commands/reset.rb +39 -0
  42. data/lib/kaya/commands/reset_suites.rb +40 -0
  43. data/lib/kaya/commands/restart.rb +9 -0
  44. data/lib/kaya/commands/start.rb +104 -0
  45. data/lib/kaya/commands/stop.rb +53 -0
  46. data/lib/kaya/cuba.rb +287 -0
  47. data/lib/kaya/cucumber/features.rb +15 -0
  48. data/lib/kaya/cucumber/task.rb +64 -0
  49. data/lib/kaya/custom/execution_data.rb +50 -0
  50. data/lib/kaya/custom/params.rb +34 -0
  51. data/lib/kaya/database/mongo_connector.rb +285 -0
  52. data/lib/kaya/error/errors.rb +55 -0
  53. data/lib/kaya/execution.rb +57 -0
  54. data/lib/kaya/results/result.rb +493 -0
  55. data/lib/kaya/results/results.rb +47 -0
  56. data/lib/kaya/suites/custom/params.rb +151 -0
  57. data/lib/kaya/suites/suite.rb +177 -0
  58. data/lib/kaya/suites/suites.rb +130 -0
  59. data/lib/kaya/support/clean.rb +12 -0
  60. data/lib/kaya/support/configuration.rb +254 -0
  61. data/lib/kaya/support/console.rb +11 -0
  62. data/lib/kaya/support/documentation.rb +34 -0
  63. data/lib/kaya/support/error_handler_helper.rb +24 -0
  64. data/lib/kaya/support/files_cleanner.rb +136 -0
  65. data/lib/kaya/support/git.rb +161 -0
  66. data/lib/kaya/support/if_config.rb +14 -0
  67. data/lib/kaya/support/logo.rb +23 -0
  68. data/lib/kaya/support/logs.rb +37 -0
  69. data/lib/kaya/support/notification.rb +127 -0
  70. data/lib/kaya/support/processes.rb +96 -0
  71. data/lib/kaya/support/query_string.rb +22 -0
  72. data/lib/kaya/support/request.rb +32 -0
  73. data/lib/kaya/support/risk.rb +9 -0
  74. data/lib/kaya/support/time_helper.rb +11 -0
  75. data/lib/kaya/support/update.rb +65 -0
  76. data/lib/kaya/version.rb +3 -0
  77. data/lib/kaya/view/body.mote +60 -0
  78. data/lib/kaya/view/custom/params/select_list.mote +14 -0
  79. data/lib/kaya/view/custom/params/text.mote +9 -0
  80. data/lib/kaya/view/custom/params.mote +9 -0
  81. data/lib/kaya/view/error_handler.mote +40 -0
  82. data/lib/kaya/view/features/feature.mote +14 -0
  83. data/lib/kaya/view/features/features_list.mote +10 -0
  84. data/lib/kaya/view/features.mote +57 -0
  85. data/lib/kaya/view/footer.mote +10 -0
  86. data/lib/kaya/view/git_information.mote +21 -0
  87. data/lib/kaya/view/help/main.mote +21 -0
  88. data/lib/kaya/view/help/page.mote +2 -0
  89. data/lib/kaya/view/help/search_result.mote +29 -0
  90. data/lib/kaya/view/help.mote +72 -0
  91. data/lib/kaya/view/javascript.mote +96 -0
  92. data/lib/kaya/view/logs/log.mote +3 -0
  93. data/lib/kaya/view/modals.mote +31 -0
  94. data/lib/kaya/view/navigation_bar.mote +48 -0
  95. data/lib/kaya/view/not_found.mote +40 -0
  96. data/lib/kaya/view/parser.rb +79 -0
  97. data/lib/kaya/view/results/all.mote +59 -0
  98. data/lib/kaya/view/results/console.mote +106 -0
  99. data/lib/kaya/view/results/detailed_info.mote +112 -0
  100. data/lib/kaya/view/results/report.mote +1 -0
  101. data/lib/kaya/view/results/result.mote +23 -0
  102. data/lib/kaya/view/results/results.mote +52 -0
  103. data/lib/kaya/view/screenshot.mote +42 -0
  104. data/lib/kaya/view/sections.rb +25 -0
  105. data/lib/kaya/view/styles.mote +58 -0
  106. data/lib/kaya/view/suites/suite.mote +73 -0
  107. data/lib/kaya/view/suites/suites.mote +9 -0
  108. data/lib/kaya/view/view.rb +72 -0
  109. data/lib/kaya.rb +175 -0
  110. data/test/features/install.feature +18 -0
  111. data/test/features/support/env.rb +3 -0
  112. metadata +395 -0
@@ -0,0 +1,493 @@
1
+ module Kaya
2
+ module Results
3
+ class Result
4
+
5
+ attr_accessor\
6
+ :id,
7
+ :started_at,
8
+ :saw,
9
+ :suite,
10
+ :suite_name,
11
+ :execution_name,
12
+ :command,
13
+ :custom_params,
14
+ :execution_data,
15
+ :kaya_command,
16
+ :html_report,
17
+ :finished_at,
18
+ :status,
19
+ :summary,
20
+ :show_as,
21
+ :kaya_report_file_name,
22
+ :console_output_file_name,
23
+ :console_output,
24
+ :bundle_output,
25
+ :git_log,
26
+ :pid,
27
+ :last_check_time,
28
+ :configuration_values,
29
+ :timeout
30
+
31
+ attr_reader :console_output
32
+
33
+
34
+ # data_for_result = {
35
+ # :suite_name,
36
+ # :execution_name,
37
+ # :type:}
38
+ def initialize data_for_result
39
+
40
+ if data_for_result["_id"]
41
+ # It comes from mongo because there is a result id
42
+ @id = data_for_result["_id"]
43
+ load_values(data_for_result)
44
+
45
+ else # It comes from a new execution request
46
+ @id = Kaya::Database::MongoConnector.generate_id
47
+ @suite = data_for_result['suite']
48
+ @execution_name = data_for_result["execution_name"] || ""
49
+ @custom_params = data_for_result["custom_params"]
50
+ @git_log = data_for_result["git_log"]
51
+ @started_at = now_in_seconds
52
+ @finished_at = nil
53
+ @status = "started"
54
+ @timeout = nil
55
+ @show_as = "pending"
56
+ @html_report = ""
57
+ @summary = "Not available yet"
58
+
59
+ # Save suite info
60
+ suite_data = Kaya::Database::MongoConnector.suite_data_for(@suite["id"])
61
+ @command = data_for_result['command']
62
+ @suite_name = suite_data["name"]
63
+ @command = suite_data["command"]
64
+ @console_output = ""
65
+ @last_check_time = now_in_seconds
66
+ @execution_data = {}
67
+ @configuration_values = Kaya::Support::Configuration.pretty_configuration_values
68
+ end
69
+ end
70
+
71
+ def load_values data
72
+ data.each_pair do |var, value|
73
+ begin
74
+ send("#{var}=",value) if send("#{var}").nil?
75
+ rescue; end
76
+ end
77
+ end
78
+
79
+ def self.get(result_id)
80
+ result_data = Kaya::Database::MongoConnector.result_data_for_id(result_id)
81
+ new(result_data) if result_data
82
+ end
83
+
84
+
85
+ def result_data_structure
86
+ {
87
+ "_id" => id,
88
+ "suite" => @suite,
89
+ "execution_name" => execution_name,
90
+ "command" => command,
91
+ "custom_params" => custom_params,
92
+ "kaya_command" => kaya_command,
93
+ "kaya_report_file_name" => kaya_report_file_name,
94
+ "html_report" => html_report,
95
+ "started_at" => started_at,
96
+ "finished_at" => finished_at,
97
+ "status" => status,
98
+ "timeout" => timeout,
99
+ "summary" => summary,
100
+ "show_as" => show_as,
101
+ "bundle_output" => bundle_output,
102
+ "console_output_file_name" => console_output_file_name,
103
+ "console_output" => console_output,
104
+ "git_log" => git_log,
105
+ "saw" => saw,
106
+ "pid" => pid,
107
+ "last_check_time" => last_check_time,
108
+ "execution_data" => execution_data,
109
+ "configuration_values" => configuration_values
110
+ }
111
+ end
112
+
113
+ def api_response
114
+ data = result_data_structure
115
+ data["has_report"] = self.has_report?
116
+ data["elapsed_time"] = self.elapsed_time
117
+ # List of fields to omit in api response
118
+ ["html_report","console_output_file_name","kaya_command","kaya_report_file_name","pid","last_check_time","console_output","git_log","bundle_output"].each{|field| data.delete(field)}
119
+ data
120
+ end
121
+
122
+ # Returns the string of custom params values
123
+ # @return [String] foo=bar john=doe
124
+ def custom_params_values
125
+ "kaya_custom_params='#{validate_params(@custom_params).to_json}'".gsub(',', ', ')
126
+ end
127
+
128
+
129
+ # Returns a hash with valid parameters. This is to prevent command line command with could cause problems
130
+ # @param [hash] custom params
131
+ # @return [hash] validated custom params
132
+ def validate_params custom_params={}
133
+ unless custom_params.nil?
134
+ validated = custom_params.select do |key, value|
135
+ unless value.nil?
136
+ Kaya::Support::Risk.secure? value
137
+ end
138
+ end
139
+ end
140
+ validated || {}
141
+ end
142
+
143
+ def add_execution_data key, value
144
+ @execution_data.store(key, value)
145
+ self.save!
146
+ end
147
+
148
+ def suite_id
149
+ @suite["id"]
150
+ end
151
+
152
+ # def suite_name
153
+ # @suite["name"]
154
+ # end
155
+
156
+ # Gets all the console log, status, etc values and update itself
157
+ # If detect report as finished kill the asociated process and return true
158
+ # else returns false wich means that the process is still runnnig
159
+ # @return [Boolean] true if process has been killed
160
+ def update_values!
161
+ $K_LOG.debug "[#{@id}] Updating values" if $K_LOG
162
+ self.save_report!
163
+ self.get_summary!
164
+ self.get_status!
165
+ self.append_result_to_console_output!
166
+ if (self.report_says_finished? and !self.stopped?)
167
+ self.finished!
168
+ $K_LOG.debug "[#{@id}] Values updated" if $K_LOG
169
+ finished = true
170
+
171
+ elsif (self.seconds_without_changes > Kaya::Support::Configuration.execution_time_to_live)
172
+ self.finished_by_timeout!
173
+ finished = true
174
+ else
175
+ return false
176
+ end
177
+
178
+ if finished
179
+ @summary = @status if @summary == "running"
180
+ self.save!
181
+ self.delete_asociated_files!
182
+ Kaya::Support::Processes.kill_by_result_id(self.id)
183
+ true
184
+ end
185
+ end
186
+
187
+
188
+ # Reads, copy html report from cucumber output file and saves it on a instance variable
189
+ # Persists changes on mongo and then deletes the html reporte file
190
+ def save_report!
191
+ if is_there_a_report_file?
192
+ new_content = Kaya::View::Parser.adapt_to_kaya(read_report, self)
193
+ if new_content.size > @html_report.size
194
+ @html_report= new_content
195
+ $K_LOG.debug "[#{@id}] Report saved" if $K_LOG
196
+ self.save!
197
+ end
198
+ end
199
+ end
200
+
201
+ def is_there_a_report_file?
202
+ begin
203
+ !open_report_file.nil?
204
+ rescue
205
+ false
206
+ end
207
+ end
208
+
209
+ def has_report?
210
+ !@html_report.empty?
211
+ end
212
+
213
+ def get_summary!
214
+ report = if is_there_a_report_file?
215
+ read_report
216
+ else
217
+ @html_report
218
+ end
219
+ @summary = Kaya::View::Parser.extract_summary(report) unless summary?
220
+ self.save!
221
+ end
222
+
223
+ def summary?
224
+ !(["Not available yet", "running"].include? @summary)
225
+ end
226
+
227
+ # Returns the html report file created by KAYA
228
+ # @param [String] html report file name
229
+ # @return [String] html code read from report
230
+ def read_report
231
+ begin
232
+ content = ''
233
+ open_report_file.each_line do |line|
234
+ content+= line
235
+ end
236
+ rescue; end
237
+ content
238
+ end
239
+
240
+ def open_report_file
241
+ begin
242
+ FileUtils.cp("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}", "#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~")
243
+ file_content = File.open "#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~", "r"
244
+
245
+ File.delete("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~")
246
+
247
+ rescue
248
+ false
249
+ end
250
+ file_content
251
+ end
252
+
253
+ def running!
254
+ @status = "running"
255
+ $K_LOG.debug "[#{@id}] Setted as running" if $K_LOG
256
+ end
257
+
258
+ def running?
259
+ @status == "running"
260
+ end
261
+
262
+ # tries to get status
263
+ def get_status!
264
+
265
+ value = Kaya::View::Parser.get_status(read_report) if is_there_a_report_file?
266
+
267
+ @status = @show_as = value if value
268
+ end
269
+
270
+ def append_result_to_console_output!
271
+ if is_there_console_output_file?
272
+ begin
273
+ output = []
274
+ open_console_output_file.each_line do |line|
275
+ output << line
276
+ end
277
+ text = output.join "\n"
278
+ if (text.size > @console_output.size )
279
+ save_console_output(text)
280
+ @last_check_time = now_in_seconds
281
+ end
282
+ true
283
+ rescue
284
+ false
285
+ end
286
+ end
287
+ end
288
+
289
+
290
+ def is_there_console_output_file?
291
+ begin
292
+ open_console_output_file and true
293
+ rescue
294
+ false
295
+ end
296
+ end
297
+
298
+ def open_console_output_file
299
+ begin
300
+ FileUtils.cp("#{Dir.pwd}/kaya/temp/#{console_output_file_name}", "#{Dir.pwd}/kaya/temp/#{console_output_file_name}~")
301
+ file_content = File.open "#{Dir.pwd}/kaya/temp/#{console_output_file_name}~", "r"
302
+ File.delete("#{Dir.pwd}/kaya/temp/#{console_output_file_name}~")
303
+ rescue Errno::ENOENT
304
+ false
305
+ end
306
+ file_content
307
+ end
308
+
309
+ # Append text to console output
310
+ # @param [String] text = the text to be appended
311
+ def append_to_console_output text
312
+ @console_output += text
313
+ self.save!
314
+ end
315
+
316
+ def save_to_bundle_output text
317
+ @bundle_output = text
318
+ self.save!
319
+ end
320
+
321
+
322
+ # Save console output text
323
+ # @param [String] text = the text to be appended
324
+ def save_console_output text
325
+ @console_output = text
326
+ self.save!
327
+ end
328
+
329
+ def finished!
330
+ @finished_at= now_in_seconds
331
+ @status = "finished"
332
+ save_report!
333
+ get_summary!
334
+ @summary = @status if @summary == "running"
335
+ $K_LOG.debug "[#{@id}] Executuion finished" if $K_LOG
336
+ self.save!
337
+ begin
338
+ $NOTIF.execution_finished self
339
+ rescue => e
340
+ $K_LOG.error "Error at notifying #{e}"
341
+ end
342
+ @summary
343
+ end
344
+
345
+ def finished_by_timeout!
346
+ reason = "Inactivity Timeout reached"
347
+ reset!(reason)
348
+ @timeout = "#{Kaya::Support::Configuration.execution_time_to_live}"
349
+ # @summary = @status if @summary == "running"
350
+ $K_LOG.debug "[#{@id}] Finished by timeout (#{Kaya::Support::Configuration.execution_time_to_live} sec)" if $K_LOG
351
+ begin
352
+ $NOTIF.execution_stopped self, "#{reason} - (@timeout) sec"
353
+ rescue => e
354
+ $K_LOG.error "Error at notifying #{e}"
355
+ end
356
+ self.save!
357
+ end
358
+
359
+ def started?
360
+ @status == "started"
361
+ end
362
+
363
+ def finished?
364
+ @status =~ /(finished|stopped)/i
365
+ end
366
+
367
+ def process_finished?
368
+ ! process_running?
369
+ end
370
+
371
+ def process_running?
372
+ Kaya::Support::Processes.process_running? pid
373
+ end
374
+
375
+ def is_finished?; self.finished?; end
376
+
377
+ def report_says_finished?
378
+ Kaya::View::Parser.finished_statement? @html_report
379
+ end
380
+
381
+ def stopped?
382
+ @status =~ /(reset|stopped)/i
383
+ end
384
+
385
+ def reset! reason=nil
386
+ status_text = "stopped"
387
+ status_text += " (#{reason})" if reason
388
+ self.status= self.summary= status_text
389
+ self.finished_at = now_in_seconds
390
+ $K_LOG.debug "[#{@id}] Execution stoppped (reset)" if $K_LOG
391
+ self.save!
392
+ end
393
+
394
+ def has_summary?
395
+ report_has_summary?
396
+ end
397
+
398
+ def report_has_summary?
399
+ @summary.include? "scenario"
400
+ end
401
+
402
+ def has_scenario_executed?
403
+ Kaya::View::Parser.has_scenarios_executed? @html_report
404
+ end
405
+
406
+ def mark_as_saw!
407
+ @saw = true
408
+ $K_LOG.debug "[#{@id}] Marked as saw" if $K_LOG
409
+ self.save!
410
+ end
411
+
412
+ def delete_console_output_file!
413
+ begin
414
+ File.delete("#{Dir.pwd}/kaya/temp/#{console_output_file_name}")
415
+ $K_LOG.debug "[#{@id}] Console output files deleted" if $K_LOG
416
+ true
417
+ rescue => e
418
+ false
419
+ end
420
+ end
421
+
422
+ def delete_kaya_report_file!
423
+ begin
424
+ File.delete("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}")
425
+ $K_LOG.debug "[#{@id}] Report files deleted" if $K_LOG
426
+ true
427
+ rescue => e
428
+ false
429
+ end
430
+ end
431
+
432
+ def delete_copy_kaya_report_file!
433
+ begin
434
+ File.delete("#{Dir.pwd}/kaya/temp/#{kaya_report_file_name}~")
435
+ true
436
+ rescue => e
437
+ false
438
+ end
439
+ end
440
+
441
+ def delete_asociated_files!
442
+ delete_console_output_file!
443
+ delete_kaya_report_file!
444
+ delete_copy_kaya_report_file!
445
+ end
446
+
447
+ # Returns the seconds that there is no console output changes only if it is not finished, else returns 0
448
+ # This is aimed to help to detect if execution is freezed (or may have a debugger statement)
449
+ def seconds_without_changes
450
+ (self.finished? or self.stopped?) ? 0 : (now_in_seconds - @last_check_time)
451
+ end
452
+
453
+ def elapsed_time
454
+ (finished_at || now_in_seconds) - started_at
455
+ end
456
+
457
+
458
+
459
+ # Returns actal timestamp
460
+ # @return [Time]
461
+ def now
462
+ Time.now.localtime
463
+ end
464
+
465
+ # Returns the started at time attribute in a format way (configured on kaya info)
466
+ # @return [String]
467
+ def started_at_formatted
468
+ Kaya::Support::TimeHelper.formatted_time_for @started_at
469
+ end
470
+
471
+ # Returns the finished at time attribute in a format way (configured on kaya info)
472
+ # @return [String]
473
+ def finished_at_formatted
474
+ Kaya::Support::TimeHelper.formatted_time_for @finished_at
475
+ end
476
+
477
+ # Returns the timestamp in seconds
478
+ # @param [Fixnum] timestamp
479
+ def now_in_seconds
480
+ now.to_i
481
+ end
482
+
483
+ # Persists the status of the object on mongo.
484
+ # If result exist update it, else creates it
485
+ # @return [Boolean] operation result
486
+ def save!
487
+ Kaya::Database::MongoConnector.result_data_for_id(id) ? Kaya::Database::MongoConnector.update_result(result_data_structure) : Kaya::Database::MongoConnector.insert_result(result_data_structure)
488
+ $K_LOG.debug "[#{@id}] Result saved" if $K_LOG
489
+ end
490
+
491
+ end
492
+ end
493
+ end
@@ -0,0 +1,47 @@
1
+ module Kaya
2
+ module Results
3
+
4
+ def self.all_results_for suite_id
5
+ Kaya::Database::MongoConnector.results_for suite_id
6
+ end
7
+
8
+ def self.results_ids_for suite_id
9
+ all_results_for(suite_id).map do |result|
10
+ result["_id"]
11
+ end
12
+ end
13
+
14
+ def self.all_results_ids
15
+ Kaya::Database::MongoConnector.all_results_ids
16
+ end
17
+
18
+ def self.find_for key
19
+ Kaya::Database::MongoConnector.find_results_for_key key
20
+ end
21
+
22
+ def self.find_for_status status
23
+ Kaya::Database::MongoConnector.find_results_for_status status
24
+ end
25
+
26
+ def self.all_results
27
+ Kaya::Database::MongoConnector.all_results
28
+ end
29
+
30
+ def self.find_all_for_key key
31
+ Kaya::Database::MongoConnector.find_results_for_key key
32
+ end
33
+
34
+ # Resets all results with running status
35
+ def self.reset_defuncts
36
+ Kaya::Database::MongoConnector.all_results.select do |result|
37
+ ["started","running"].include? result["status"]
38
+ end.each do |result|
39
+ Kaya::Support::Processes.kill_by_result_id(result["_id"])
40
+ end.each do |result|
41
+ result = Kaya::Results::Result.get(result["_id"])
42
+ result.reset!
43
+
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,151 @@
1
+ module Kaya
2
+ module Suites
3
+ module Custom
4
+ class Params
5
+
6
+ attr_reader :params
7
+
8
+ # This class provides methods related to custom params
9
+ # This is useful to ask about each params and its attributes
10
+
11
+ # It receives plain string with the definitions of each custom param
12
+ # @param [String] custom secion:
13
+ # a_text_field_param, required_text_param:*, with_a_default_value:foobar, required_and_with_default:foobar:* , a_select_list:[value1|value2|value3]
14
+ #
15
+ # Text fields examples
16
+ #
17
+ # 'a_text_field_param' -> will be interpreted as a text field accepting any value
18
+ # 'required_text_param:*' -> will be interpreted as a text field with a required value (* indicates required field)
19
+ # 'with_a_default_value:foobar' -> will be interpreted as a text field with a default value
20
+ # 'required_and_with_default:foobar:*' -> will be interpreted as a text field with a default value and as a required field
21
+ #
22
+ # Select list examples
23
+ # 'a_select_list:(value1|value2|value3)' -> will be interpreted as a select list with listed values. First is used as default value
24
+ # 'none_default_select_list:(none|value1|value2|value3)' -> will be interpreted as a select list with listed values. First is used as default value and none indicates as empty value
25
+ #
26
+
27
+ def initialize custom_params_definition
28
+ if custom_params_definition
29
+ plain_params = custom_params_definition.gsub(/\s+,/,",").gsub(/\,\s+/,",").split(",")
30
+ @params = plain_params.map do |param|
31
+ send(type_of(param), param)
32
+ end
33
+ else
34
+ @params = {}
35
+ end
36
+ @params
37
+ end
38
+
39
+
40
+ # Returns the type of the parameter according to its structure
41
+ def type_of entire_param
42
+ parts = entire_param.split(":")
43
+
44
+ if parts.last =~ /^\(.*\)$/ # It is a select list
45
+
46
+ "select_list"
47
+
48
+ else # It is a text field
49
+
50
+ case parts.size
51
+
52
+ when 1
53
+ "single_text_field"
54
+
55
+ when 2
56
+ return "required_text_field" if parts.last == "*"
57
+ "default_text_field"
58
+
59
+ when 3
60
+ if parts.last == "*"
61
+ "required_default_text_field"
62
+ else
63
+ raise "Malformed custom param"
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ # Returns the structure of a select list element
71
+ # @param [String] the plain structure of a custom param
72
+ # @return [Hash] a hash with the required structure
73
+ def select_list param
74
+ plus = Hash.new
75
+ name, values = param.split(":")
76
+ values.gsub!(/\(|\)/, "")
77
+ values = values.split("|").map do |val|
78
+ if val.include? "*"
79
+ h = val.split("*")
80
+ plus[h[0]] = h[1]
81
+ val = h[0]
82
+ end
83
+ val
84
+ end
85
+ {
86
+ "name" => name,
87
+ "type" => "select_list",
88
+ "options" => values,
89
+ "plus_options" => plus,
90
+ "required" => nil,
91
+ "value" => nil
92
+ }
93
+ end
94
+
95
+ # Returns the structure of a single text field
96
+ # @param [String] the plain structure of a custom param
97
+ # @return [Hash] a hash with the required structure
98
+ def single_text_field param
99
+ {
100
+ "name" => param.split.first,
101
+ "type" => "text",
102
+ "options" => nil,
103
+ "required" => nil,
104
+ "value" => nil
105
+ }
106
+ end
107
+
108
+ # Returns the structure of a required text field
109
+ # @param [String] the plain structure of a custom param
110
+ # @return [Hash] a hash with the required structure
111
+ def required_text_field param
112
+ {
113
+ "name" => param.split(":").first,
114
+ "type" => "text",
115
+ "options" => nil,
116
+ "required" => true,
117
+ "value" => nil
118
+ }
119
+ end
120
+
121
+ # Returns the structure of a text field with a default value
122
+ # @param [String] the plain structure of a custom param
123
+ # @return [Hash] a hash with the required structure
124
+ def default_text_field param
125
+ name, value = param.split ":"
126
+ {
127
+ "name" => name,
128
+ "type" => "text",
129
+ "options" => value,
130
+ "required" => false,
131
+ "value" => nil
132
+ }
133
+ end
134
+
135
+ # Returns the structure of a text field with a default value and required
136
+ # @param [String] the plain structure of a custom param
137
+ # @return [Hash] a hash with the required structure
138
+ def required_default_text_field param
139
+ name, value= param.split(":")
140
+ {
141
+ "name" => name,
142
+ "type" => "text",
143
+ "options" => value,
144
+ "required" => true,
145
+ "value" => nil
146
+ }
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end