Class: C11n::Table

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

Overview

Translations in tabular form. The schema of translations table must match to the schema specified below:

1. If category information exists

      [Category] [Key] [Locale 1] [Locale 2] ... [Locale N]

2. If category information does not exist

      [Key] [Locale 1] [Locale 2] ... [Locale N]

Columns composing the schema must appear on the first row of the sheet.

Constant Summary collapse

CATEGORY_COLUMN =
'Category'.freeze
TYPE_COLUMN =
'Type'.freeze
KEY_COLUMN =
'Key'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(raw_table, options = {}) ⇒ Table

Returns a new instance of Table.



21
22
23
24
25
26
# File 'lib/c11n/table.rb', line 21

def initialize(raw_table, options = {})
  @raw_table = raw_table
  @deserializer_class = options[:deserializer_class] || C11n::Conversion::PlainDeserializer
  @categories = {}
  @types = {}
end

Instance Method Details

#categoriesObject



50
51
52
# File 'lib/c11n/table.rb', line 50

def categories
  {}
end

#localesObject



46
47
48
# File 'lib/c11n/table.rb', line 46

def locales
  has_categories? ? schema[2..-1] : schema[1..-1]
end

#rowsObject



71
72
73
# File 'lib/c11n/table.rb', line 71

def rows
  @raw_table
end

#schemaObject



42
43
44
# File 'lib/c11n/table.rb', line 42

def schema
  @raw_table[0]
end

#set(locale, key, translation, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/c11n/table.rb', line 75

def set(locale, key, translation, options = {})
  unless locales.map(&:to_s).map(&:downcase).include? locale.to_s.downcase
    schema[schema.length] = locale
  end

  row_with_key(key)[locale_index_of(locale)] = translation
  row_with_key(key)[category_index] = options[:category] if has_categories? && options[:category]
  row_with_key(key)[type_index] = options[:type] if has_types? && options[:type]
end

#table_for(locale) ⇒ Object

Returns key-value mappings for specified locale as an array



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/c11n/table.rb', line 29

def table_for(locale)
  @raw_table[1..-1].map do |columns|
    if columns
      key = key_column_from(columns)
      translation = translation_column_from(columns, locale) || ''

      key ? [key, translation] : nil
    else
      nil
    end
  end.compact
end

#to_hashObject



67
68
69
# File 'lib/c11n/table.rb', line 67

def to_hash
  Hash[locales.map { |locale| [locale.to_sym, @deserializer_class.new(table_for(locale)).deserialize] }]
end

#typesObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/c11n/table.rb', line 54

def types
  if has_types?
    @types = @raw_table[1..-1].inject({}) do |types, columns|
      key = columns[key_column_index]
      type = columns[type_index]

      types.merge(key => type) if type
    end
  else
    {}
  end
end