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
- #filename_to_id(filename) ⇒ Object
-
#light_to_sentence(prefix, light) ⇒ Object
Turns a traffic light in a sentence wrapped in a classed
span. -
#light_to_word(light) ⇒ Object
Turns a traffic light into a more “testing” word: “FAILURE”, “ERROR”, “passed”, “n/a”.
- #new_grouping?(result) ⇒ Boolean
-
#render(file, locals) ⇒ Object
Recursively renders another template.
-
#replace_tabs(string) ⇒ Object
Replaces tabs with spaces because Firefox does really wacky things with tabs in a
prein atable. -
#title(text) ⇒ Object
Capitalizes string as a title.
Instance Method Details
#filename_to_id(filename) ⇒ Object
34 35 36 |
# File 'lib/ciat/erb_helpers.rb', line 34 def filename_to_id(filename) filename.gsub(/[\/\-.]/, "_") end |
#light_to_sentence(prefix, light) ⇒ Object
Turns a traffic light in a sentence wrapped in a classed span.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ciat/erb_helpers.rb', line 21 def light_to_sentence(prefix, light) "<span class=\"#{light.color}\">#{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 18 |
# 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" when light.unneeded? then "unneeded" else raise "cannot turn #{light} into word" end end |
#new_grouping?(result) ⇒ Boolean
49 50 51 52 53 54 55 56 |
# File 'lib/ciat/erb_helpers.rb', line 49 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.
60 61 62 63 64 |
# File 'lib/ciat/erb_helpers.rb', line 60 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.
45 46 47 |
# File 'lib/ciat/erb_helpers.rb', line 45 def replace_tabs(string) string.gsub("\t", " ") end |
#title(text) ⇒ Object
Capitalizes string as a title.
39 40 41 |
# File 'lib/ciat/erb_helpers.rb', line 39 def title(text) text.gsub(/\b\w/){$&.upcase} end |