Class: AwesomeTranslations::TranslatedValue

Inherits:
Object
  • Object
show all
Defined in:
app/models/awesome_translations/translated_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fileObject

Returns the value of attribute file.



4
5
6
# File 'app/models/awesome_translations/translated_value.rb', line 4

def file
  @file
end

#keyObject

Returns the value of attribute key.



4
5
6
# File 'app/models/awesome_translations/translated_value.rb', line 4

def key
  @key
end

#localeObject

Returns the value of attribute locale.



4
5
6
# File 'app/models/awesome_translations/translated_value.rb', line 4

def locale
  @locale
end

#valueObject

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_keyObject



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_noObject



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

Returns:

  • (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.open(@file, "w") { |fp| fp.write("#{@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.open(file, "w") { |fp| fp.write(YAML.dump(translations)) }
end

#to_sObject 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