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



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

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/rspec/endpoint/endpoint_generator.rb', line 22

def copy_files
  Rails.application.load_tasks
  routes = capture_output { Rake::Task['routes'].invoke }
  @route = routes.split("\n").grep(Regexp.new "#{class_path.join('/')}##{file_name}").first || raise("#{class_path.join('/')}##{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
  if behavior == :revoke
    template 'action_request_spec.rb', "spec/requests/#{file_path}_spec.rb"
  elsif behavior == :invoke
    empty_directory Pathname.new('spec/requests').join(*class_path)
    template 'action_request_spec.rb', "spec/requests/#{file_path}_spec.rb"
  end
end