Class: RSpec::Scaffold::SpecLocationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/scaffold/spec_location_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_root, file_path) ⇒ SpecLocationBuilder

Returns a new instance of SpecLocationBuilder.

Parameters:

  • project_root (Pathname)
  • file_path (Pathname)


8
9
10
11
12
# File 'lib/rspec/scaffold/spec_location_builder.rb', line 8

def initialize(project_root, file_path)
  @project_root = Pathname(project_root)
  @file_path    = Pathname(file_path)
  @relative_file_path = (@file_path.relative? ? Pathname(@file_path) : ( Pathname(@file_path.to_s.sub(@project_root.to_s, '')) ))
end

Instance Method Details

#spec_locationObject

RSpec::Scaffold::SpecLocationBuilder.new(“/home/dev/projects/some_app”, “/app/models/user.rb”).spec_location



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/scaffold/spec_location_builder.rb', line 15

def spec_location
  # for default rails app dir code
  @spec_location ||= if @relative_file_path.to_s[%r'\A/?app/']
    # replace first 'app/' with 'spec/' and change ending a bit
    @project_root.join(@relative_file_path.sub(%r'\A/?app/', 'spec/'))
  else
    @project_root.join("spec").join(@relative_file_path.to_s.sub(%r'\A/', ''))
  end.sub(%r'\.rb\z', '_spec.rb')

  return @spec_location
end