Class: BabelishRnc::Csv2Base

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

Direct Known Subclasses

CSV2Android, CSV2JSON, CSV2Php, CSV2Strings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, langs, args = {}) ⇒ Csv2Base

Returns a new instance of Csv2Base.

Raises:

  • (ArgumentError)


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
38
39
40
41
42
43
44
# File 'lib/babelish_rnc/csv2base.rb', line 13

def initialize(filename, langs, args = {})
  default_args = {
    :excluded_states => [],
    :state_column => nil,
    :keys_column => 0,
    :csv_separator => ","
  }

  args = default_args.merge!(args)
  args = Thor::CoreExt::HashWithIndifferentAccess.new(args)
  @langs = langs

  # check input types
  raise ArgumentError.new("wrong value of filename: #{filename.inspect}") unless filename.is_a?(String)
  if !@langs.is_a?(Hash) || @langs.size == 0
    raise ArgumentError.new("wrong format or/and langs parameter: #{@langs.inspect}")
  end

  @output_basename = args[:output_basename]
  @output_dir = args[:output_dir].to_s
  @csv_filename = filename
  @excluded_states = args[:excluded_states]
  @state_column = args[:state_column]
  @keys_column = args[:keys_column]
  @comments_column = args[:comments_column]
  @default_lang = args[:default_lang]
  @csv_separator = args[:csv_separator]
  @ignore_lang_path = args[:ignore_lang_path]
  @stripping = args[:stripping]
  @languages = []
  @comments = {}
end

Instance Attribute Details

#comments_columnObject

Returns the value of attribute comments_column.



10
11
12
# File 'lib/babelish_rnc/csv2base.rb', line 10

def comments_column
  @comments_column
end

#csv_filenameObject

Returns the value of attribute csv_filename.



7
8
9
# File 'lib/babelish_rnc/csv2base.rb', line 7

def csv_filename
  @csv_filename
end

#csv_separatorObject

Returns the value of attribute csv_separator.



9
10
11
# File 'lib/babelish_rnc/csv2base.rb', line 9

def csv_separator
  @csv_separator
end

#default_langObject

Returns the value of attribute default_lang.



8
9
10
# File 'lib/babelish_rnc/csv2base.rb', line 8

def default_lang
  @default_lang
end

#excluded_statesObject

Returns the value of attribute excluded_states.



10
11
12
# File 'lib/babelish_rnc/csv2base.rb', line 10

def excluded_states
  @excluded_states
end

#ignore_lang_pathObject

Returns the value of attribute ignore_lang_path.



5
6
7
# File 'lib/babelish_rnc/csv2base.rb', line 5

def ignore_lang_path
  @ignore_lang_path
end

#keys_columnObject

Returns the value of attribute keys_column.



10
11
12
# File 'lib/babelish_rnc/csv2base.rb', line 10

def keys_column
  @keys_column
end

#langsObject

Returns the value of attribute langs.



6
7
8
# File 'lib/babelish_rnc/csv2base.rb', line 6

def langs
  @langs
end

#languagesObject

Returns the value of attribute languages.



11
12
13
# File 'lib/babelish_rnc/csv2base.rb', line 11

def languages
  @languages
end

#output_basenameObject

Returns the value of attribute output_basename.



4
5
6
# File 'lib/babelish_rnc/csv2base.rb', line 4

def output_basename
  @output_basename
end

#output_dirObject

Returns the value of attribute output_dir.



4
5
6
# File 'lib/babelish_rnc/csv2base.rb', line 4

def output_dir
  @output_dir
end

#state_columnObject

Returns the value of attribute state_column.



10
11
12
# File 'lib/babelish_rnc/csv2base.rb', line 10

def state_column
  @state_column
end

Instance Method Details

#commentsObject



64
65
66
# File 'lib/babelish_rnc/csv2base.rb', line 64

def comments
  @comments  
end

#convert(name = @csv_filename) ⇒ Object

Convert csv file to multiple Localizable.strings files for each column



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/babelish_rnc/csv2base.rb', line 97

def convert(name = @csv_filename)
  rowIndex     = 0
  excludedCols = []
  defaultCol   = 0

  CSV.foreach(name, :quote_char => '"', :col_sep => @csv_separator, :row_sep => :auto) do |row|

    if rowIndex == 0
      #check there's at least two columns
      return unless row.count > 1
    else
      #skip empty lines (or sections)
      next if row == nil or row[@keys_column].nil?
    end

    # go through columns
    row.size.times do |i|
      next if excludedCols.include? i

      #header
      if rowIndex == 0
        # defaultCol can be the keyValue
        defaultCol = i if self.default_lang == row[i]
        # ignore all headers not listed in langs to create files
        (excludedCols << i and next) unless @langs.has_key?(row[i])

        language = Language.new(row[i])
        if @langs[row[i]].is_a?(Array)
          @langs[row[i]].each do |id|
            language.add_language_id(id.to_s)
          end
        else
          language.add_language_id(@langs[row[i]].to_s)
        end
        @languages[i] = language
      elsif !@state_column || (row[@state_column].nil? || row[@state_column] == '' || !@excluded_states.include?(row[@state_column]))
        key = row[@keys_column]
        comment = @comments_column ? row[@comments_column] : nil
        key.strip! if @stripping
        default_value =  self.default_lang ? row[defaultCol] : nil
        value = self.process_value(row[i], default_value)
        @comments[key] = comment
        @languages[i].add_content_pair(key, value)
      end
    end

    rowIndex += 1
  end

  write_content
end

#create_file_from_path(file_path) ⇒ Object



46
47
48
49
50
# File 'lib/babelish_rnc/csv2base.rb', line 46

def create_file_from_path(file_path)
  path = File.dirname(file_path)
  FileUtils.mkdir_p path
  return File.new(file_path, "w")
end

#default_filepathObject



78
79
80
# File 'lib/babelish_rnc/csv2base.rb', line 78

def default_filepath
  Pathname.new(@output_dir) + "#{output_basename}.#{extension}"
end

#extensionObject



73
74
75
76
# File 'lib/babelish_rnc/csv2base.rb', line 73

def extension
  #implement in subclass
  ""
end

#get_row_format(row_key, row_value, comment = nil, indentation = 0, defaultlang = "") ⇒ Object



91
92
93
94
# File 'lib/babelish_rnc/csv2base.rb', line 91

def get_row_format(row_key, row_value, comment = nil, indentation = 0, defaultlang = "")
  # ignoring comment by default
  "\"#{row_key}\"" + " " * indentation + " = \"#{row_value}\""
end

#hash_to_output(content = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/babelish_rnc/csv2base.rb', line 176

def hash_to_output(content = {})
  output = ''
  indentation = content.map(&:first).max { |a, b| a.length <=> b.length }.length
  if content && content.size > 0
    content.each do |key, value|
      comment = @comments[key]
      defaultlang  =  @languages[1]
      default_value = defaultlang ? defaultlang.content[key] : ""
      output += get_row_format(key, value, comment, indentation - key.length, default_value)
    end
  end
  return output
end

#keysObject



52
53
54
55
56
57
58
# File 'lib/babelish_rnc/csv2base.rb', line 52

def keys
  @languages.each do |lang|
    next unless lang
    return lang.content.keys unless lang.content.keys.empty?
  end
  return []
end

#language_filepaths(language) ⇒ Object



68
69
70
71
# File 'lib/babelish_rnc/csv2base.rb', line 68

def language_filepaths(language)
  #implement in subclass
  []
end

#process_value(row_value, default_value) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/babelish_rnc/csv2base.rb', line 82

def process_value(row_value, default_value)
  value = row_value.nil? ? default_value : row_value
  value = "" if value.nil?
  value.gsub!(/\\*\"/, "\\\"") # escape double quotes
  value.gsub!(/\n/, "\\n") # replace new lines with \n, dont strip
  value.strip! if @stripping
  return value.to_utf8
end

#tableObject



60
61
62
# File 'lib/babelish_rnc/csv2base.rb', line 60

def table
  output_basename
end

#write_contentObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/babelish_rnc/csv2base.rb', line 149

def write_content
  info = "List of created files:\n"
  count = 0
  @languages.each do |language|
    next if language.nil?

    files = []
    if @ignore_lang_path
      files << create_file_from_path(default_filepath)
    else
      language_filepaths(language).each do |filename|
        files << create_file_from_path(filename)
      end
    end
    files.each do |file|
      file.write hash_to_output(language.content)
      info += "- #{File.absolute_path(file)}\n"
      count += 1

      file.close
    end
  end

  info = "Created #{count} files.\n" + info
  return info
end