class RSpec::Scaffold::Runner

Attributes

file[R]

Public Class Methods

new(file) click to toggle source

@param [Pathname] file

# File lib/rspec/scaffold/runner.rb, line 7
def initialize(file)
  @file = Pathname.new(file)
end

Public Instance Methods

generate_spec(ruby_file) click to toggle source

Private

# File lib/rspec/scaffold/runner.rb, line 31
def generate_spec(ruby_file)
  spec = RSpec::Scaffold::Generator.new Pathname.new(File.expand_path(ruby_file))
  if spec.funcs.any?
    spec.perform
  else
    log "- #{ruby_file} - no methods", :gray
    nil
  end
rescue => e
  log "! #{ruby_file} - #{e.inspect.gsub /^#<|>$/, ''}", :red
end
log(msg = nil, color = :green) click to toggle source
# File lib/rspec/scaffold/runner.rb, line 72
def log(msg = nil, color = :green)
  HighLine.new.say %Q(  <%= color('#{msg}', :#{color}) %>)
end
perform() click to toggle source
# File lib/rspec/scaffold/runner.rb, line 11
def perform
  fail ArgumentError, %Q(File or directory does not exist: "#{file}") if !File.exists?(file) && !File.exists?("#{file}.rb")
  ruby_files.each do |ruby_file|
    rspec_file = Pathname.new(spec_file(ruby_file))
    spec_file_path = rspec_file.to_s[%r|/(spec/.+)|, 1]
    next if rspec_file.exist?.tap { |exists| log "- #{spec_file_path} - already exists", :gray if exists }
    spec = generate_spec(ruby_file)
    next unless spec
    log "+ #{spec_file_path}"
    FileUtils.mkdir_p(rspec_file.parent)
    File.open(rspec_file, 'wb') do |f|
      f << spec.join("\n")
    end
  end
end
ruby_files() click to toggle source
# File lib/rspec/scaffold/runner.rb, line 43
def ruby_files
  if File.directory?(file)
    Dir[File.join(file, '**', '*.rb')]
  else
    if file.extname.empty?
      ["#{file}.rb"]
    else
      [file]
    end
  end
end
spec_file(ruby_file) click to toggle source

@note subbing out /app/ is Rails specific

# File lib/rspec/scaffold/runner.rb, line 56
def spec_file(ruby_file)
  File.join(spec_path, "#{specify(ruby_file)}").sub '/app/', '/'
end
spec_path() click to toggle source
# File lib/rspec/scaffold/runner.rb, line 64
def spec_path
  if File.directory?(File.expand_path('spec'))
    File.expand_path('spec')
  else
    fail "Couldn't find spec directory"
  end
end
specify(file_name) click to toggle source
# File lib/rspec/scaffold/runner.rb, line 60
def specify(file_name)
  file_name.sub('.rb', '_spec.rb')
end