Class: Babelish::Base2Csv

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

Direct Known Subclasses

Android2CSV, JSON2CSV, Php2CSV, Strings2CSV

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {:filenames => []}) ⇒ Base2Csv

Returns a new instance of Base2Csv.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/babelish/base2csv.rb', line 5

def initialize(args = {:filenames => []})
  raise ArgumentError.new("No filenames given") unless args[:filenames]
  if args[:headers]
    raise ArgumentError.new("number of headers and files don't match, don't forget the constant column") unless args[:headers].size == (args[:filenames].size + 1)
  end

  @filenames = args[:filenames]

  @csv_filename = args[:csv_filename] || "translations.csv"
  @default_lang = args[:default_lang]
  @headers = args[:headers] || default_headers
end

Instance Attribute Details

#csv_filenameObject

Returns the value of attribute csv_filename.



3
4
5
# File 'lib/babelish/base2csv.rb', line 3

def csv_filename
  @csv_filename
end

#default_langObject

Returns the value of attribute default_lang.



3
4
5
# File 'lib/babelish/base2csv.rb', line 3

def default_lang
  @default_lang
end

#filenamesObject

Returns the value of attribute filenames.



3
4
5
# File 'lib/babelish/base2csv.rb', line 3

def filenames
  @filenames
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/babelish/base2csv.rb', line 3

def headers
  @headers
end

Instance Method Details

#convert(write_to_file = true) ⇒ Hash

Process files and create csv

Parameters:

  • write_to_file (Boolean) (defaults to: true)

    create or not the csv file

Returns:

  • (Hash)

    the translations formatted if write_to_file



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/babelish/base2csv.rb', line 24

def convert(write_to_file = true)
  strings = {}
  keys = nil
  comments = {}

  @filenames.each do |fname|
    header = fname
    strings[header], file_comments = load_strings(fname)
    keys ||= strings[header].keys
    comments.merge!(file_comments)
  end

  if write_to_file
    # Create csv file
    puts "Creating #{@csv_filename}"
    create_csv_file(keys, strings, comments)
  else
    return keys, strings
  end
end