Class: TodoNext::CLI::SampleFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/todo_next/cli/sample_file_generator.rb

Constant Summary collapse

TEMPLATE_PATH =
File.dirname(__FILE__) + '/assets/rspec_example_file.rb.erb'

Class Method Summary collapse

Class Method Details

.generate(object_name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/todo_next/cli/sample_file_generator.rb', line 9

def self.generate(object_name)
  object_name = object_name && object_name.downcase
  target_file = sample_file_path(object_name)

  erb = ERB.new(File.read(TEMPLATE_PATH))
  classname = (object_name || 'foobar').capitalize  # for ERB/binding
  rspec_sample = erb.result(binding)

  if File.exist?(target_file)
    File.open(target_file, "r") do |orig|
      @original = orig.read()
    end
    if @original.include?(rspec_sample)
      puts "skipping : sample code already present in #{target_file}."
    else
      File.open(target_file, "r") do |orig|
        File.unlink(target_file)
        File.open(target_file, "w") do |new|
          new.write rspec_sample
          new.write(@original)
        end
        puts "sample code was inserted in #{target_file}."
      end
    end
  else
    File.open(target_file,'w') do |f| f.write(rspec_sample) end
    puts "#{target_file} was created."
  end
end