require 'fileutils' require_relative 'base_generator' require 'erb' require 'find' require 'ro_core_ext/string' require 'active_support' module RoCommands module Generators class SpecFile include BaseGenerator class << self def create(file_name) super if defined? super @file_name = file_name FileUtils.mkpath dir template end private def dir @dir ||= 'spec' end # get dir file def get_df if file_name f = File.basename file_name if file_name.match(%r{/}) d = file_name.split("/").first else d = '' end end return d, f end def constant_name if path r = File.basename(path).gsub(%r{_spec\.rb}) do |m| "" end r.camelize end end def path lib_d, lib_f = get_df #puts "d:#{d} line:#{__LINE__}" if test(?d, 'lib') lib_dirs = Find.find('lib').select do |f| test(?d, f) end #dir_full_path = lib_dirs.grep(%r{#{d.implicit}}).first || 'lib' if lib_d.blank? Find.find("lib") do |path| if test(?f, path) basename = File.basename(path).gsub(%r{\.\w+$}, "") if basename.match(%r{#{lib_f.implicit}}) @file = path @dir_full_path = File.dirname(path) end end end else @dir_full_path = lib_dirs.select do |dir| dir.split("/").last.match(%r{#{lib_d.implicit}}) end.first files = Find.find(@dir_full_path).select do |file| if test(?f, file) basename = File.basename file basename.match(%r{^#{lib_f.implicit}}) end end @file = files.first.gsub(%r{\.rb}, "") end basename = File.basename(@file).gsub(%r{\.\w+$}, "") spec_file = "#{basename}_spec.rb" File.join(dir, @dir_full_path, spec_file) end end def to super if defined? super unless test(?f, path) file_write(path) end @created_file = path end end end end end