drnic / drnic_js_test_helpers

JavaScript helper libraries for unit testing

This URL has Read+Write access

drnic_js_test_helpers / lib / protodoc.rb
100644 37 lines (29 sloc) 0.675 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'erb'
 
class String
  def lines
    split $/
  end
  
  def strip_whitespace_at_line_ends
    lines.map {|line| line.gsub(/\s+$/, '')} * $/
  end
end
 
module Protodoc
  module Environment
    def include(*filenames)
      filenames.map {|filename| Preprocessor.new(filename).to_s}.join("\n")
    end
  end
  
  class Preprocessor
    include Environment
    
    def initialize(filename)
      @filename = File.expand_path(filename)
      @template = ERB.new(IO.read(@filename), nil, '%')
    end
    
    def to_s
      @template.result(binding).strip_whitespace_at_line_ends
    end
  end
end
 
if __FILE__ == $0
  print Protodoc::Preprocessor.new(ARGV.first)
end