Module: Duvet

Extended by:
Duvet
Included in:
Duvet
Defined in:
lib/duvet.rb,
lib/duvet/cov.rb,
lib/duvet/covs.rb,
lib/duvet/version.rb

Defined Under Namespace

Classes: Cov, Covs

Constant Summary collapse

DEFAULTS =
{
  dir:    'cov',
  style:  'rcov',
  filter: ''
}
TEMPLATE_HASH =
{
  name:    'duvet',
  time:    Time.now,
  version: VERSION
}
TEMPLATE_PATH =
Pathname.new(__FILE__).dirname + '..' + 'templates'
VERSION =
'0.4.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



19
20
21
# File 'lib/duvet.rb', line 19

def opts
  @opts
end

Instance Method Details

#context_for(obj) ⇒ #get_binding

Creates a class with methods for rendering an ERB template.

Parameters:

  • obj (Object)

    Object to create context for

Returns:

  • (#get_binding)

    Object which gives a binding for ERB templates



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/duvet.rb', line 64

def context_for(obj)
  case obj
  when Array
    obj.map {|i| context_for(i) }
  when Hash
    klass = Class.new
    klass.send(:define_method, :get_binding) { binding }

    obj.each do |k,v|
      klass.send(:define_method, k) { Duvet.context_for(v) }
    end

    klass.new
  else
    obj
  end
end

#finishObject

Call this on exit so that coverage is written.



125
126
127
128
129
# File 'lib/duvet.rb', line 125

def finish
  FileUtils.mkdir_p @opts[:dir]
  result.write
  write_resources
end

#format(data, template) ⇒ Object

Renders a template with the data given.

Parameters:

  • data (Hash)

    Data to insert into template

  • template (String)

    Path to template from TEMPLATE_PATH



86
87
88
89
90
91
# File 'lib/duvet.rb', line 86

def format(data, template)
  t = (TEMPLATE_PATH + template).read
  vars = context_for TEMPLATE_HASH.merge(data)

  ERB.new(t).result(vars.get_binding)
end

#resultCovs

Gets the results from the Coverage, filtered against the patterm given to #start.

Returns:



52
53
54
55
56
57
58
# File 'lib/duvet.rb', line 52

def result
  cov = Coverage.result.find_all do |path, c|
    @opts[:filter] =~ path
  end

  Duvet::Covs.from_data cov
end

#start(opts = {}) ⇒ Object

Start coverage tracking, needs to be called before any files are loaded.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :dir (String, Pathname)

    Directory to write coverage to

  • :filter (String, Regexp)

    Pattern files must match to be written



40
41
42
43
44
45
46
# File 'lib/duvet.rb', line 40

def start(opts={})
  @opts = DEFAULTS.merge(opts)
  @opts[:filter] = Regexp.new(@opts[:filter])
  @opts[:dir]    = Pathname.new(@opts[:dir])

  Coverage.start
end

#write(data, template) ⇒ Object

Renders then writes a file based on the url in the data given.

Parameters:

  • data (Hash)

    Data to insert into template

  • template (String)

    Path to template from TEMPLATE_PATH



97
98
99
# File 'lib/duvet.rb', line 97

def write(data, template)
  write_file format(data, template), data[:file][:url]
end

#write_file(text, path) ⇒ Object

Writes a file, creating directories where necessary.

Parameters:

  • text (String)

    Text to write to file

  • path (String)

    Path to write file to



105
106
107
108
109
110
111
112
# File 'lib/duvet.rb', line 105

def write_file(text, path)
  write_to = @opts[:dir] + path

  FileUtils.mkdir_p write_to.dirname
  File.open write_to, 'w' do |f|
    f.write text
  end
end

#write_resourcesObject

Writes all the resources to the correct directory.



115
116
117
118
119
120
121
122
# File 'lib/duvet.rb', line 115

def write_resources
  paths = %w(css/styles.css js/main.js js/jquery.js js/plugins.js)

  paths.each do |path|
    path = Pathname.new(TEMPLATE_PATH + path)
    write_file path.read, path.basename
  end
end