Module: Coco::Helpers

Defined in:
lib/coco/helpers.rb

Overview

Public: Collection of application’s helpers methods.

Class Method Summary collapse

Class Method Details

.expand(files) ⇒ Object

Public: Expands a bulk of filenames into full path filenames.

files - List of filenames as an Array of String.

Returns an Array of String.



65
66
67
# File 'lib/coco/helpers.rb', line 65

def self.expand(files)
  files.map { |file| File.expand_path(file) }
end

.index_titleObject

Public: Get page title for the index.html file.

Returns String.



53
54
55
56
57
# File 'lib/coco/helpers.rb', line 53

def self.index_title
  project_name = File.basename(Dir.pwd)
  version = File.read(File.join(Coco::ROOT, 'VERSION')).strip
  "#{project_name} - Code coverage (coco #{version})"
end

.level_class(percentage) ⇒ Object

Public: Get the CSS class given a percentage. To use in the index file.

percentage - A Fixnum.

Examples

Helpers.level_class(100)
#=> 'green'

Returns the String CSS class name.



93
94
95
96
97
98
99
100
# File 'lib/coco/helpers.rb', line 93

def self.level_class(percentage)
  case percentage
  when 100 then 'green'
  when 80..99 then 'yellow'
  else
    'red'
  end
end

.name_for_html(name) ⇒ Object

Public: Get a String (ruby) source filename ready to be displayed in the index file.

name - String full path filename (normaly full path but, who

knows? may be relative path).

Examples

name = '/home/user/my_project/lib/source.rb'
Helpers.name_for_html(name)
#=> 'lib/<b>source.rb</b>'

Returns the formatted String.



21
22
23
24
25
26
27
# File 'lib/coco/helpers.rb', line 21

def self.name_for_html(name)
  name = File.expand_path(name)
  name = name.sub(Dir.pwd, '')
  name = name.sub(%r{^/}, '')
  base = File.basename(name)
  name.sub(base, "<b>#{base}</b>")
end

.rb2html(name) ⇒ Object

Public: Get html filename (from a ruby filename) suitable for the coverage directory.

name - String full path filename.

Examples

ruby = '/home/user/my_project/lib/source.rb'
html = Helpers.rb2html(ruby)
#=> '_lib_source.rb.html'

Returns String HTML filename.



42
43
44
45
46
47
# File 'lib/coco/helpers.rb', line 42

def self.rb2html(name)
  name = name.sub(Dir.pwd, '')
  name = name.sub(%r{^/}, '')
  name = name.tr('/\\', '_')
  name + '.html'
end

.rb_files_from(directory) ⇒ Object

Public: Get all ruby files from a directory, including sub-directories.

directory - String directory to look into.

Returns an Array of String.



76
77
78
79
# File 'lib/coco/helpers.rb', line 76

def self.rb_files_from(directory)
  rb_files = File.join(directory, '**', '*.rb')
  Dir.glob(rb_files)
end