Class: FileCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/ccios/file_creator.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_path, options = {}) ⇒ FileCreator

Returns a new instance of FileCreator.



4
5
6
7
8
# File 'lib/ccios/file_creator.rb', line 4

def initialize(source_path, options = {})
  @source_path = source_path
  @classes_path = source_path + '/Classes'
  @options = options
end

Instance Method Details

#create_file(prefix, suffix, group, target) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ccios/file_creator.rb', line 23

def create_file(prefix, suffix, group, target)
  file_path = @classes_path + '/' + prefix + suffix + '.swift'

  raise "File #{file_path} already exists" if File.exists?(file_path)
  file = File.new(file_path, 'w')

  templater_options = templater_options(target)
  code_templater = CodeTemplater.new(templater_options)
  file_content = code_templater.content_for_suffix(prefix, suffix)
  file.puts(file_content)

  file.close
  file_ref = group.new_reference(file_path)
  target.add_file_references([file_ref])
end

#git_usernameObject



19
20
21
# File 'lib/ccios/file_creator.rb', line 19

def git_username
  `cd #{@source_path}; git config user.name`.strip
end


39
40
41
42
43
44
45
46
47
48
# File 'lib/ccios/file_creator.rb', line 39

def print_file_content(prefix, suffix)
  file_path = @classes_path + '/' + suffix + '.swift'

  code_templater = CodeTemplater.new(@options)
  template = code_templater.content_for_suffix(prefix, suffix)

  puts "Add this snippet to #{file_path}"
  puts template
  puts "\n"
end

#templater_options(target) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ccios/file_creator.rb', line 10

def templater_options(target)
  defaults = {
    project_name: target.display_name,
    full_username: git_username,
    date: DateTime.now.strftime("%d/%m/%Y"),
  }
  defaults.merge(@options)
end