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

#file_extensionObject



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_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