Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active April 26, 2016 17:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcreux/2094d96d99b4bffc2169 to your computer and use it in GitHub Desktop.
Save pcreux/2094d96d99b4bffc2169 to your computer and use it in GitHub Desktop.
mkspec - Generate spec file (in vim: `:!mkspec %`)
#!/usr/bin/env ruby
DESC = "
Generate spec file
Example: mkspec app/services/user/create.rb will generate spec/services/user/create_spec.rb
"
FILES = ARGV
if FILES.empty?
puts DESC
exit 1
end
require 'fileutils'
FILES.each do |file_path|
unless File.exists?(file_path)
puts "#{file_path} does not exists - Skipping..."
next
end
_, top_dir, *sub_dirs, file = file_path.split('/')
spec_dir_path = ["spec", top_dir, sub_dirs].flatten.join('/')
spec_file_path = [spec_dir_path, file.gsub(/\.rb$/, "_spec.rb")].join('/')
namespaces = sub_dirs.map { |str| str.split('_').map(&:capitalize).join }
klass_name = file.gsub(/\.rb$/, '').split('_').map(&:capitalize).join
full_klass_name = (namespaces + [klass_name]).join('::')
code = %|require "rails_helper"
describe #{full_klass_name} do
end
|
if File.exists?(spec_file_path)
puts "#{spec_file_path} exists - Skipping..."
else
puts "#{spec_file_path} created!"
FileUtils.mkpath spec_dir_path
File.open(spec_file_path, 'w') do |f|
f.puts code
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment