Class: Babelish::Csv2Base
- Inherits:
-
Object
- Object
- Babelish::Csv2Base
- Defined in:
- lib/babelish/csv2base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#csv_filename ⇒ Object
Returns the value of attribute csv_filename.
-
#default_lang ⇒ Object
Returns the value of attribute default_lang.
-
#excluded_states ⇒ Object
Returns the value of attribute excluded_states.
-
#ignore_lang_path ⇒ Object
Returns the value of attribute ignore_lang_path.
-
#keys_column ⇒ Object
Returns the value of attribute keys_column.
-
#langs ⇒ Object
Returns the value of attribute langs.
-
#languages ⇒ Object
Returns the value of attribute languages.
-
#output_basename ⇒ Object
Returns the value of attribute output_basename.
-
#output_dir ⇒ Object
Returns the value of attribute output_dir.
-
#state_column ⇒ Object
Returns the value of attribute state_column.
Instance Method Summary collapse
-
#convert(name = @csv_filename) ⇒ Object
Convert csv file to multiple Localizable.strings files for each column.
- #create_file_from_path(file_path) ⇒ Object
- #default_filepath ⇒ Object
- #extension ⇒ Object
- #get_row_format(row_key, row_value) ⇒ Object
- #hash_to_output(content = {}) ⇒ Object
-
#initialize(filename, langs, args = {}) ⇒ Csv2Base
constructor
A new instance of Csv2Base.
- #keys ⇒ Object
- #language_filepaths(language) ⇒ Object
- #process_value(row_value, default_value) ⇒ Object
- #table ⇒ Object
- #write_content ⇒ Object
Constructor Details
#initialize(filename, langs, args = {}) ⇒ Csv2Base
Returns a new instance of Csv2Base.
12 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 |
# File 'lib/babelish/csv2base.rb', line 12 def initialize(filename, langs, args = {}) default_args = { :excluded_states => [], :state_column => nil, :keys_column => 0 } 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] @default_lang = args[:default_lang] @ignore_lang_path = args[:ignore_lang_path] @languages = [] end |
Instance Attribute Details
#csv_filename ⇒ Object
Returns the value of attribute csv_filename.
7 8 9 |
# File 'lib/babelish/csv2base.rb', line 7 def csv_filename @csv_filename end |
#default_lang ⇒ Object
Returns the value of attribute default_lang.
8 9 10 |
# File 'lib/babelish/csv2base.rb', line 8 def default_lang @default_lang end |
#excluded_states ⇒ Object
Returns the value of attribute excluded_states.
9 10 11 |
# File 'lib/babelish/csv2base.rb', line 9 def excluded_states @excluded_states end |
#ignore_lang_path ⇒ Object
Returns the value of attribute ignore_lang_path.
5 6 7 |
# File 'lib/babelish/csv2base.rb', line 5 def ignore_lang_path @ignore_lang_path end |
#keys_column ⇒ Object
Returns the value of attribute keys_column.
9 10 11 |
# File 'lib/babelish/csv2base.rb', line 9 def keys_column @keys_column end |
#langs ⇒ Object
Returns the value of attribute langs.
6 7 8 |
# File 'lib/babelish/csv2base.rb', line 6 def langs @langs end |
#languages ⇒ Object
Returns the value of attribute languages.
10 11 12 |
# File 'lib/babelish/csv2base.rb', line 10 def languages @languages end |
#output_basename ⇒ Object
Returns the value of attribute output_basename.
4 5 6 |
# File 'lib/babelish/csv2base.rb', line 4 def output_basename @output_basename end |
#output_dir ⇒ Object
Returns the value of attribute output_dir.
4 5 6 |
# File 'lib/babelish/csv2base.rb', line 4 def output_dir @output_dir end |
#state_column ⇒ Object
Returns the value of attribute state_column.
9 10 11 |
# File 'lib/babelish/csv2base.rb', line 9 def state_column @state_column end |
Instance Method Details
#convert(name = @csv_filename) ⇒ Object
Convert csv file to multiple Localizable.strings files for each column
88 89 90 91 92 93 94 95 96 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 |
# File 'lib/babelish/csv2base.rb', line 88 def convert(name = @csv_filename) rowIndex = 0 excludedCols = [] defaultCol = 0 CSV.foreach(name, :quote_char => '"', :col_sep => ',', :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])) # TODO: add option to strip the constant or referenced language key = row[@keys_column].strip default_value = self.default_lang ? row[defaultCol] : nil value = self.process_value(row[i], default_value) @languages[i].add_content_pair(key, value) end end rowIndex += 1 end write_content end |
#create_file_from_path(file_path) ⇒ Object
40 41 42 43 44 |
# File 'lib/babelish/csv2base.rb', line 40 def create_file_from_path(file_path) path = File.dirname(file_path) FileUtils.mkdir_p path return File.new(file_path, "w") end |
#default_filepath ⇒ Object
68 69 70 |
# File 'lib/babelish/csv2base.rb', line 68 def default_filepath Pathname.new(@output_dir) + "#{output_basename}.#{extension}" end |
#extension ⇒ Object
63 64 65 66 |
# File 'lib/babelish/csv2base.rb', line 63 def extension #implement in subclass "" end |
#get_row_format(row_key, row_value) ⇒ Object
83 84 85 |
# File 'lib/babelish/csv2base.rb', line 83 def get_row_format(row_key, row_value) return "\"" + row_key + "\" = \"" + row_value + "\"" end |
#hash_to_output(content = {}) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/babelish/csv2base.rb', line 166 def hash_to_output(content = {}) output = '' if content && content.size > 0 content.each do |key, value| output += get_row_format(key, value) end end return output end |
#keys ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/babelish/csv2base.rb', line 46 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
58 59 60 61 |
# File 'lib/babelish/csv2base.rb', line 58 def language_filepaths(language) #implement in subclass [] end |
#process_value(row_value, default_value) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/babelish/csv2base.rb', line 72 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!(/\s*(\n|\\\s*n)\s*/, "\\n") #replace new lines with \n + strip value.gsub!(/%\s+([a-zA-Z@])([^a-zA-Z@]|$)/, "%\\1\\2") #repair string formats ("% d points" etc) value.gsub!(/([^0-9\s\(\{\[^])%/, "\\1 %") value.strip! return value.to_utf8 end |
#table ⇒ Object
54 55 56 |
# File 'lib/babelish/csv2base.rb', line 54 def table output_basename end |
#write_content ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/babelish/csv2base.rb', line 139 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 |