Class: FileManager

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

Instance Method Summary collapse

Constructor Details

#initialize(language, items, output_folder, platform, dryrun = false) ⇒ FileManager

Returns a new instance of FileManager.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/file_manager.rb', line 7

def initialize language, items, output_folder, platform, dryrun=false

  if language==nil
    puts "INTERRUPTION:\n"
    puts "Something is wrong with one of your language columns (maybe it's missing?)\n".red
    exit 1
  end

  @language = language.downcase
  @platform = platform
  @dryrun = dryrun
  @items = items
  @path = output_folder
  @template = File.read(get_template_path)
end

Instance Method Details

#create_fileObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/file_manager.rb', line 35

def create_file
  file_name = get_file_name
  destination_file_path = "#{@path}#{file_name}"

  pretty_print destination_file_path

  unless @dryrun

    dirname = File.dirname(destination_file_path)
    unless File.directory?(dirname)
      FileUtils.mkdir_p(dirname)
    end
    File.open(destination_file_path, 'w') { |f| f.write(render) }
  else
    puts render.yellow
  end
end

#get_file_nameObject



27
28
29
30
31
32
33
# File 'lib/file_manager.rb', line 27

def get_file_name
  if @platform.eql?'android'
    "values-#{@language}/translation.xml"
  else
    "#{@language}.lbproj/Localizable.strings"
  end
end

#get_template_pathObject



23
24
25
# File 'lib/file_manager.rb', line 23

def get_template_path
  File.join(File.dirname(__FILE__), "templates/#{@platform}.erb")
end

#pretty_print(path_to_file) ⇒ Object



53
54
55
56
# File 'lib/file_manager.rb', line 53

def pretty_print(path_to_file)
  subtracted = path_to_file.gsub("#{Dir.pwd}/",'')
  puts "created #{subtracted.green} successfully"
end

#renderObject



58
59
60
# File 'lib/file_manager.rb', line 58

def render
  ERB.new(@template).result(binding)
end