Class: EasyRspec::RspecFileBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_rspec/rspec_file_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass_name) ⇒ RspecFileBuilder

Returns a new instance of RspecFileBuilder.



5
6
7
# File 'lib/easy_rspec/rspec_file_builder.rb', line 5

def initialize(klass_name)
  @klass_name = klass_name
end

Instance Method Details

#buildObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/easy_rspec/rspec_file_builder.rb', line 9

def build
  raise "RSpec file already exists at #{rspec_file.path}" if File.file?(rspec_file.path)

  FileUtils.mkdir_p rspec_file.directory
  File.new(rspec_file.path, 'w')

  File.open(rspec_file.path, "w+") do |f|
    f.write("describe #{original_file.klass_name} do")

    file_contents.instance_methods.each do |instance_method|
      write_method_spec(f, "##{instance_method}")
    end

    file_contents.class_methods.each do |class_method|
      write_method_spec(f, ".#{class_method}")
    end

    f.write("\n\nend\n")
  end

  "RSpec file created successfully at #{rspec_file.path}"
end