Class: AwesomeTranslations::TranslatedValue
- Inherits:
-
Object
- Object
- AwesomeTranslations::TranslatedValue
- Defined in:
- app/models/awesome_translations/translated_value.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#key ⇒ Object
Returns the value of attribute key.
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #array_key ⇒ Object
- #array_no ⇒ Object
- #array_translation? ⇒ Boolean
- #file_extension ⇒ Object
-
#initialize(data) ⇒ TranslatedValue
constructor
A new instance of TranslatedValue.
-
#save! ⇒ Object
rubocop:disable Metrics/AbcSize.
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(data) ⇒ TranslatedValue
Returns a new instance of TranslatedValue.
6 7 8 9 10 11 |
# File 'app/models/awesome_translations/translated_value.rb', line 6 def initialize(data) @file = data.fetch(:file) @locale = data.fetch(:locale) @key = data.fetch(:key) @value = data.fetch(:value) end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
4 5 6 |
# File 'app/models/awesome_translations/translated_value.rb', line 4 def file @file end |
#key ⇒ Object
Returns the value of attribute key.
4 5 6 |
# File 'app/models/awesome_translations/translated_value.rb', line 4 def key @key end |
#locale ⇒ Object
Returns the value of attribute locale.
4 5 6 |
# File 'app/models/awesome_translations/translated_value.rb', line 4 def locale @locale end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'app/models/awesome_translations/translated_value.rb', line 4 def value @value end |
Instance Method Details
#array_key ⇒ Object
25 26 27 28 29 |
# File 'app/models/awesome_translations/translated_value.rb', line 25 def array_key return unless (match = @key.match(/\A(.+)\[(\d+)\]\Z/)) match[1] end |
#array_no ⇒ Object
31 32 33 34 35 |
# File 'app/models/awesome_translations/translated_value.rb', line 31 def array_no return unless (match = @key.match(/\A(.+)\[(\d+)\]\Z/)) match[2].to_i end |
#array_translation? ⇒ Boolean
19 20 21 22 23 |
# File 'app/models/awesome_translations/translated_value.rb', line 19 def array_translation? return true if /\[(\d+)\]\Z/.match?(@key) false end |
#file_extension ⇒ Object
37 38 39 |
# File 'app/models/awesome_translations/translated_value.rb', line 37 def file_extension @file_extension ||= File.extname(@file) end |
#save! ⇒ Object
rubocop:disable Metrics/AbcSize
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/awesome_translations/translated_value.rb', line 41 def save! # rubocop:disable Metrics/AbcSize if file_extension == ".yml" translations = YAML.safe_load(File.read(file)) if File.exist?(file) elsif file_extension == ".json" translations = JSON.parse(File.read(file)) if File.exist?(file) else raise "Unhandled file extension: #{file_extension}" end translations ||= {} translations[@locale.to_s] ||= {} insert_translation_into_hash(translations) number_of_translactions = count_translations(translations) update_models if number_of_translactions.positive? dir = File.dirname(file) FileUtils.mkdir_p(dir) unless File.exist?(dir) if file_extension == ".yml" File.write(file, YAML.dump(translations)) elsif file_extension == ".json" File.write(file, JSON.pretty_generate(translations)) else raise "Unhandled file extension: #{file_extension}" end I18n.load_path << file unless I18n.load_path.include?(file) else File.unlink(file) if File.exist?(file) I18n.load_path.reject! do |load_path_value| load_path_value == file end end end |
#to_s ⇒ Object Also known as: inspect
13 14 15 |
# File 'app/models/awesome_translations/translated_value.rb', line 13 def to_s "<AwesomeTranslations::TranslatedValue file=\"#{@file}\" locale=\"#{@locale}\" key=\"#{@key}\" value=\"#{@value}\">" end |