Class: Rspec::EndpointGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/rspec/endpoint/endpoint_generator.rb

Instance Method Summary collapse

Instance Method Details

#capture_outputObject



10
11
12
13
14
15
16
17
18
# File 'lib/generators/rspec/endpoint/endpoint_generator.rb', line 10

def capture_output
  fake_stdout = StringIO.new
  old_stdout = $stdout
  $stdout = fake_stdout
  yield
ensure
  $stdout = old_stdout
  return fake_stdout.string
end

#copy_filesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/rspec/endpoint/endpoint_generator.rb', line 20

def copy_files
  Rails.application.load_tasks
  routes = capture_output { Rake::Task['routes'].invoke }
  @route = routes.split("\n").grep(Regexp.new "#{controller}##{file_name}").first || raise("#{controller}##{file_name} not found in routes")
  @http_verb = @route[/GET|POST|DELETE|PUT|PATCH/]
  @path = @route[/(\/.*)\(/, 1]
  @path_params = @path.split('/').select{|i| i[/:/]}
  @path_params.each do |param|
    @path.sub!(param, '#{' + param[1..-1] + '}')
  end
  empty_directory "spec/requests/#{controller}"
  template 'action_request_spec.rb', "spec/requests/#{controller}/#{file_name}_spec.rb"
end