Created
December 17, 2013 05:28
-
-
Save zbjornson/8000471 to your computer and use it in GitHub Desktop.
Designed to rough-out the spec folder of your Ruby on Rails application. For example, you have a ton of models living in WEB-INF/app/models and want WEB-INF/spec/models to have corresponding model_xxx_spec.rb files, complete with the header (e.g. schema, copyright--anything in the top of the file that starts with a #) and "describe #{method}" st…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "in #{Dir.pwd}\n" | |
Dir.entries("app/helpers").each do |file_name| | |
file_path = "#{Dir.pwd}/app/helpers/#{file_name}" | |
puts "Processing #{file_name}\n" | |
unless File.directory?(file_path) | |
spec_file_name = file_name.sub(/\.rb/, "_spec.rb") | |
unless File.file?("spec/helpers/#{spec_file_name}") | |
class_name = nil | |
first_line = File.open(file_path, &:readline) | |
File.open("spec/helpers/#{spec_file_name}", 'w') do |output| | |
File.open(file_path, 'r') do |stream| | |
if first_line.include?(file_name) | |
# This file includes the copyright and schema. Copy it. | |
line = stream.gets | |
while line.start_with?("#") do | |
output << line | |
line = stream.gets | |
end | |
output << "\n" | |
end | |
stream.rewind | |
line = stream.gets | |
while class_name.nil? do | |
#class_name = line.match(/^class ([A-Za-z]+)/)[1] if line.start_with?("class") | |
class_name = line.match(/^module ([A-Za-z]+)/)[1] if line.start_with?("module") | |
line = stream.gets | |
end | |
puts " Processing #{class_name}\n" | |
output << "require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')\n" | |
output << "\n" | |
output << "describe #{class_name} do\n" | |
while !line.nil? do | |
begin | |
if line.strip.start_with?("def ") | |
instance_method = line.include?("self") ? false : true | |
if instance_method | |
function_name = line.match(/def ([A-Za-z_\?=]+)/)[1] | |
output << " describe '##{function_name}' do\n end\n\n" | |
else | |
function_name = line.match(/def self.([A-Za-z_\?=]+)/)[1] | |
output << " describe '::#{function_name}' do\n end\n\n" | |
end | |
end | |
line = stream.gets | |
rescue Object => o | |
puts o | |
puts line | |
end | |
end | |
output << "end\n" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment