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
-
#initialize(data) ⇒ TranslatedValue
constructor
A new instance of TranslatedValue.
- #save! ⇒ Object
- #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 |
#save! ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/awesome_translations/translated_value.rb', line 37 def save! dir = File.dirname(@file) FileUtils.mkdir_p(dir) unless File.exist?(dir) File.write(@file, "#{@locale}:\n") unless File.exist?(@file) translations = YAML.safe_load(File.read(@file)) translations ||= {} translations[@locale.to_s] ||= {} insert_translation_into_hash(translations) update_models I18n.load_path << file unless I18n.load_path.include?(file) File.write(file, YAML.dump(translations)) 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 |