Class: LazyFixtures::FactoryGirl::FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_fixtures/factory_girl/file_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ FileManager

Returns a new instance of FileManager.



5
6
7
8
9
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 5

def initialize(name, options)
  @name = name
  @options = options
  @file_path = File.join(global_factory_directory, get_file_name)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



4
5
6
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 4

def file_path
  @file_path
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 4

def name
  @name
end

Instance Method Details

#check_existence?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 31

def check_existence?
  File.file?(file_path)
end

#create_fileObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 11

def create_file
  return unless @options[:create]
  if check_existence? && !@options[:overwrite]
    puts "There is a file already inside that folder with the same name #{get_file_name}"
    puts "Would you like to overwrite that file? (yes/no)"
    answer = gets.chomp
    if answer ==  'yes'
      puts 'The generator is overwriting your file'
      File.new(file_path, "w")
      true
    else
      puts 'Exiting the program'
      false
    end
  end
  puts "creating new file under #{global_factory_directory}/#{get_file_name}"
  File.new(file_path, "w")
  true
end

#get_file_nameObject



45
46
47
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 45

def get_file_name
  "#{name}.rb"
end

#global_factory_directoryObject



41
42
43
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 41

def global_factory_directory
  LazyFixtures.configuration.factory_directory
end

#write_file(text) ⇒ Object



35
36
37
38
39
# File 'lib/lazy_fixtures/factory_girl/file_manager.rb', line 35

def write_file(text)
  File.open(file_path, 'w') do |f|
    f.write text
  end
end