Module: Reviser::Components::Generators

Included in:
Generator
Defined in:
lib/reviser/components/generators.rb

Overview

Module containing all methods for writing results.

Convention over configuration !

To add a new format, you need maybe to install a gem. Find a gem which supports a specified format on rubygems.org. Add the line “gem " in the Gemfile and execute "bundle install"

Now, you can write the method corresponding to the format. The name of the method corresponds to the format. For example, if you want to generate a word file (.doc), the name of the method will be: “doc” Don’t forget to require the gem: “require " at the beginning of the method ! the header of method looks like the following block:

def (ext = '') require ... end

To write results, you have to go through in the data instance variable. data is a [Hash]:

  • key: The person’s name.
  • value: Results of analysis

Each value of data is also a [Hash]:

  • key: the name of criterion checked.
  • value: The result of criterion.

Instance Method Summary collapse

Instance Method Details

#csv(data, ext = '.csv') ⇒ Object

Generates the CSV file



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/reviser/components/generators.rb', line 59

def csv data, ext = '.csv'
  require 'csv'
  CSV.open(out(ext), 'wb') do |f|
    # Criterias as columns
    f << (criterias).unshift('projet')

    # Values for each project as rows
    data.keys.each do |proj|
      f << data[proj].values.unshift(proj)
    end
  end
end

#html(data, ext = '.html') ⇒ Object

Generates an HTML file



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/reviser/components/generators.rb', line 100

def html data, ext = '.html'
  out = '<!DOCTYPE html><html><head>'
  out += '<meta charset= "UTF-8">'
  out += "<link rel=\"stylesheet\" href=\"#{Cfg.resource('/css/component.css').to_path}\" />"
  out += "<link rel=\"stylesheet\" href=\"#{Cfg.resource('/css/normalize.css').to_path}\" />"
  out += '<title>Results</title>'
  out += "</head>\n<body><table><thead><tr>"

  criterias.unshift('Projet').each { |crit| out += "<th>#{crit}</th>" }
  
  out += '</tr></thead><tbody>'
    # Values for each project as rows
  data.keys.each do |proj|
    out += "<tr><th>#{proj}</th>"
    data[proj].each do |k, v|
      out += "<td><pre>#{v}</pre></td>"
    end
    out += '</tr>'
  end

  out += '</tbody></table></body></html>'

   File.open(out(ext), 'w') { |f| f.write(out) }
end

#xls(data, ext = '.xls') ⇒ Object

Generates a Excel file



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/reviser/components/generators.rb', line 73

def xls data, ext = '.xls'
  require_gem 'spreadsheet' unless defined? Spreadsheet

  Spreadsheet.client_encoding = 'UTF-8'
  book = Spreadsheet::Workbook.new
  sheet = book.create_worksheet :name => 'Results'

  # header
  format = Spreadsheet::Format.new :weight => :bold, :size => 14 , 
  :horizontal_align => :center

  (criterias.unshift('Projets')).each_with_index do |crit, i|
    sheet[0,i] = crit
    sheet.column(i).width = (crit.size * format.font.size/10) + 5
  end
  sheet.row(0).default_format = format
  sheet.row(0).height = 18

  # Values for each project as rows
  data.keys.each_with_index do |proj, i|
    sheet.insert_row(i+1, data[proj].values.unshift(proj))
  end  

  book.write out(ext)
end