jasmine-headless-webkit/lib/jasmine/headless/test_file.rb

63 lines
1.4 KiB
Ruby
Raw Normal View History

2011-11-17 21:18:25 +00:00
require 'rainbow'
2011-11-18 21:00:24 +00:00
require 'sprockets'
2011-11-16 20:28:02 +00:00
module Jasmine::Headless
class TestFile
2011-11-18 03:16:04 +00:00
attr_reader :path, :source_root
2011-11-16 20:28:02 +00:00
2011-11-18 03:16:04 +00:00
def initialize(path, source_root = nil)
@path, @source_root = path, source_root
2011-11-16 20:28:02 +00:00
end
2011-11-17 21:18:25 +00:00
def ==(other)
self.path == other.path
end
def to_html
process_data_by_filename(path)
2011-11-17 21:18:25 +00:00
end
2011-11-18 03:16:04 +00:00
def dependencies
return @dependencies if @dependencies
processor = Sprockets::DirectiveProcessor.new(path)
@dependencies = processor.directives.collect do |_, type, name|
if name[%r{^\.}]
2011-11-18 03:16:04 +00:00
name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '')
end
[ type, name ]
end
end
2011-11-18 21:00:24 +00:00
def logical_path
path.gsub(%r{^#{source_root}/}, '').gsub(%r{\..+$}, '')
end
private
def read
File.read(path)
end
def process_data_by_filename(path, data = nil)
case extension = File.extname(path)
when ''
data || ''
when '.js'
data || %{<script type="text/javascript" src="#{path}"></script>}
when '.css'
data || %{<link rel="stylesheet" href="#{path}" type="text/css" />}
else
if engine = Sprockets.engines(extension)
data = engine.new(path) { data || read }.render(self)
process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data)
else
data || ''
end
end
end
2011-11-16 20:28:02 +00:00
end
end
2011-11-18 03:16:04 +00:00