Module: CIAT::ERBHelpers

Included in:
Feedback::HtmlFeedback, TemplateBinder
Defined in:
lib/ciat/erb_helpers.rb

Overview

If it were easy for you to write your own HTML templates, then the methods in this module would be useful.

I don’t know if it’s easy to write your own HTML template!

Instance Method Summary collapse

Instance Method Details

#light_to_sentence(prefix, light) ⇒ Object

Turns a traffic light in a sentence wrapped in a classed span.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ciat/erb_helpers.rb', line 20

def light_to_sentence(prefix, light)
	"<span class=\"#{light.setting}\">#{prefix} " +
	case
	when light.red? then "failed"
	when light.yellow? then "errored"
	when light.green? then "passed"
	when light.unset? then "not run"
 else
   raise "cannot turn #{light} into a sentence"
	end +
	".</span>"
end

#light_to_word(light) ⇒ Object

Turns a traffic light into a more “testing” word: “FAILURE”, “ERROR”, “passed”, “n/a”.



8
9
10
11
12
13
14
15
16
17
# File 'lib/ciat/erb_helpers.rb', line 8

def light_to_word(light)
	case
	when light.red? then "FAILURE"
	when light.yellow? then "ERROR"
	when light.green? then "passed"
	when light.unset? then "n/a"
 else
   raise "cannot turn #{light} into word"
	end
end

#new_grouping?(result) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/ciat/erb_helpers.rb', line 44

def new_grouping?(result)
  if @old_grouping == result.grouping
    false
  else
    @old_grouping = result.grouping
    true
  end
end

#render(file, locals) ⇒ Object

Recursively renders another template. If it’s possible to write your own templates for reports, this will probably play a key role.



55
56
57
58
59
# File 'lib/ciat/erb_helpers.rb', line 55

def render(file, locals)
  erb = ERB.new(read(file))
  erb.filename = file
  erb.result(CIAT::TemplateBinder.new(locals).get_binding)
end

#replace_tabs(string) ⇒ Object

Replaces tabs with spaces because Firefox does really wacky things with tabs in a pre in a table.



40
41
42
# File 'lib/ciat/erb_helpers.rb', line 40

def replace_tabs(string)
	string.gsub("\t", "    ")
end

#title(text) ⇒ Object

Capitalizes string as a title.



34
35
36
# File 'lib/ciat/erb_helpers.rb', line 34

def title(text)
  text.gsub(/\b\w/){$&.upcase}
end