require 'sinatra/base' require 'teatime/teafile' module Teatime class Server < Sinatra::Base def self.root_path(*chunks) File.expand_path(File.join('..', *chunks), __FILE__) end def self.load_teafile(teafile) if File.exist?(teafile) Teatime::Teafile.load(teafile) end end set :teatime, load_teafile(File.join(Dir.pwd, 'Teafile')) set :views, root_path('views') set :public_folder, teatime.lib_dir get '/' do haml :index, :locals => { :js_files => js_files, :css_files => css_files } end get '/test/*' do js_test_file = File.join(*params[:splat]) send_file File.join(settings.teatime.test_dir, js_test_file) end get '/vendor/*' do js_vendor_file = File.join(*params[:splat]) send_file File.join(settings.teatime.vendor_dir, js_vendor_file) end private def vendor_js_files %w{mocha.js chai.js}.map do |file_path| File.join 'vendor', 'javascripts', File.basename(file_path) end end def vendor_css_files %w{mocha.css}.map do |file_path| File.join 'vendor', 'stylesheets', File.basename(file_path) end end def js_helper_files Dir[File.join(settings.teatime.test_dir, settings.teatime.test_helpers)].map do |helper_file| File.join 'test', helper_file.gsub(%r{^#{settings.teatime.test_dir}}, '') end end def js_lib_files Dir[File.join(settings.teatime.lib_dir, settings.teatime.lib_files)].map do |lib_file| File.join lib_file.gsub(%r{^#{settings.teatime.lib_dir}}, '') end end def js_test_files Dir[File.join(settings.teatime.test_dir, settings.teatime.test_files)].map do |test_file| File.join 'test', test_file.gsub(%r{^#{settings.teatime.test_dir}}, '') end end def js_files vendor_js_files + js_lib_files + js_helper_files + js_test_files end def css_files vendor_css_files end end end