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
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
-
#opts ⇒ Object
Returns the value of attribute opts.
Instance Method Summary collapse
-
#context_for(obj) ⇒ #get_binding
Creates a class with methods for rendering an ERB template.
-
#finish ⇒ Object
Call this on exit so that coverage is written.
-
#format(data, template) ⇒ Object
Renders a template with the data given.
-
#result ⇒ Covs
Gets the results from the Coverage, filtered against the patterm given to #start.
-
#start(opts = {}) ⇒ Object
Start coverage tracking, needs to be called before any files are loaded.
-
#write(data, template) ⇒ Object
Renders then writes a file based on the url in the data given.
-
#write_file(text, path) ⇒ Object
Writes a file, creating directories where necessary.
-
#write_resources ⇒ Object
Writes all the resources to the correct directory.
Instance Attribute Details
#opts ⇒ Object
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.
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 |
#finish ⇒ Object
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.
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 |
#result ⇒ Covs
Gets the results from the Coverage, filtered against the patterm given to #start.
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.
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.
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.
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_resources ⇒ Object
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 |