class Translatomatic::ResourceFile::CSV
CSV
resource file
Constants
- CONTEXT_COLUMN
- Cell
@private
- DEFAULT_COMMENT_COLUMN
- DEFAULT_KEY_COLUMN
- DEFAULT_VALUE_COLUMN
Public Class Methods
extensions()
click to toggle source
(see Base.extensions
)
# File lib/translatomatic/resource_file/csv.rb, line 8 def self.extensions %w[csv] end
Public Instance Methods
save(target = path, options = {})
click to toggle source
(see Base#save
)
# File lib/translatomatic/resource_file/csv.rb, line 23 def save(target = path, options = {}) use_headers = @options[:csv_headers] csv_options = { write_headers: use_headers } csv_options[:headers] = @headers if use_headers ::CSV.open(target, 'wb', csv_options) do |csv| @rows.each do |row| csv << row.collect(&:value) end end end
set(key, value)
click to toggle source
(see Base#set
)
Calls superclass method
Translatomatic::ResourceFile::Base#set
# File lib/translatomatic/resource_file/csv.rb, line 13 def set(key, value) super(key, value) if @cellmap.include?(key) @cellmap[key].value = value else add_row(key, value) end end
Private Instance Methods
add_row(key, value)
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 70 def add_row(key, value) @rows << load_row(@key_column => key, @value_column => value) end
find_cell(row, column_name)
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 184 def find_cell(row, column_name) row.find { |i| i.header == column_name } end
have_target_locale_column?()
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 167 def have_target_locale_column? @have_target_locale_column ||= begin @target_locale && @headers && @headers.include?(@target_locale.to_s) end end
init()
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 56 def init @rows = [] @cellmap = {} # map of String key -> Cell @rownum = 0 @key_column = option(:csv_key_column, DEFAULT_KEY_COLUMN) @value_column = option(:csv_value_column, DEFAULT_VALUE_COLUMN) @comments_column = option(:csv_comment_column, DEFAULT_COMMENT_COLUMN) @translate = option(:csv_translate_columns) end
init_properties()
click to toggle source
initialise properties and cellmap from @rows
# File lib/translatomatic/resource_file/csv.rb, line 90 def init_properties @properties = {} @cellmap = {} @rows.each do |row| row.each do |cell| @properties[cell.key] = cell.value if cell.translate @cellmap[cell.key] = cell end end end
load()
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 74 def load contents = read_contents(@path) csv_options = { headers: @options[:csv_headers] } @rows = [] @rownum = 0 csv = ::CSV.parse(contents, csv_options) csv.each do |row| @rows << load_row(row) end init_properties end
load_array_row(row)
click to toggle source
row is an array of values @return [Array<Cell>] cells
# File lib/translatomatic/resource_file/csv.rb, line 126 def load_array_row(row) cells = [] row.each_with_index do |value, i| translate = translate_column?((i + 1).to_s) key = "key#{@rownum},#{i + 1}" cells << Cell.new("column#{i + 1}", key, value, translate) end parse_metadata(cells) cells end
load_hash_row(row)
click to toggle source
row is a hash of column -> value @return [Array<Cell>] cells
# File lib/translatomatic/resource_file/csv.rb, line 139 def load_hash_row(row) cells = [] # add a property for each column colnum = 0 row.each do |column, value| colnum += 1 translate = translate_column?(column) key = "key#{@rownum},#{colnum}" cells << Cell.new(column, key, value, translate) end parse_metadata(cells) cells end
load_row(row)
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 110 def load_row(row) @rownum += 1 if row.is_a?(::CSV::Row) @headers = row.headers load_hash_row(row.to_h) elsif row.is_a?(Hash) load_hash_row(row) elsif row.is_a?(Array) load_array_row(row) else raise "invalid row data: #{row}" end end
option(key, default = nil)
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 66 def option(key, default = nil) @options[key] || default end
parse_metadata(row)
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 174 def parse_metadata(row) comments_cell = find_cell(row, @comments_column) return unless comments_cell @metadata.parse_comment(comments_cell.value) row.each do |cell| @metadata.assign_key(cell.key, keep_context: true) if cell.translate end @metadata.clear_context end
row_to_hash(row)
click to toggle source
@param row [Array<Cell>] row to convert to hash
# File lib/translatomatic/resource_file/csv.rb, line 102 def row_to_hash(row) hash = {} row.each do |cell| hash[cell.header] = cell.value end hash end
translate_column?(column)
click to toggle source
# File lib/translatomatic/resource_file/csv.rb, line 153 def translate_column?(column) if @translate.present? # translation columns specified @translate.include?(column) elsif have_target_locale_column? # if there is a column matching the target locale, translate that # column only. column == @target_locale.to_s else # translate if it's not the key column or comments column column != @key_column && column != @comments_column end end