Class: AwesomeTranslations::CacheDatabaseGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_translations/cache_database_generator.rb

Defined Under Namespace

Classes: Group, Handler, HandlerTranslation, ScannedFile, TranslationKey, TranslationValue

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ CacheDatabaseGenerator

Returns a new instance of CacheDatabaseGenerator.



12
13
14
15
16
17
18
19
20
# File 'lib/awesome_translations/cache_database_generator.rb', line 12

def initialize(args = {})
  require "baza"

  @args = args
  @debug = @args[:debug] || false

  init_database
  execute_migrations
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



6
7
8
# File 'lib/awesome_translations/cache_database_generator.rb', line 6

def db
  @db
end

Class Method Details

.currentObject



8
9
10
# File 'lib/awesome_translations/cache_database_generator.rb', line 8

def self.current
  @current ||= AwesomeTranslations::CacheDatabaseGenerator.new
end

.database_pathObject



88
89
90
# File 'lib/awesome_translations/cache_database_generator.rb', line 88

def self.database_path
  @database_path ||= Rails.root.join("db/awesome_translations.sqlite3").to_s
end

Instance Method Details

#cache_handler_translationsObject



80
81
82
# File 'lib/awesome_translations/cache_database_generator.rb', line 80

def cache_handler_translations
  cache_translations_in_handlers
end

#cache_translationsObject



60
61
62
63
64
65
# File 'lib/awesome_translations/cache_database_generator.rb', line 60

def cache_translations
  cache_yml_translations
  cache_handler_translations

  clean_up_not_found
end

#cache_yml_translationsObject



76
77
78
# File 'lib/awesome_translations/cache_database_generator.rb', line 76

def cache_yml_translations
  cache_translations_in_dir(Rails.root.join("config/locales"))
end

#closeObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/awesome_translations/cache_database_generator.rb', line 47

def close
  @initialized = false

  @db&.close
  @db = nil

  AwesomeTranslations::CacheDatabaseGenerator::Group.db = nil
  AwesomeTranslations::CacheDatabaseGenerator::Handler.db = nil
  AwesomeTranslations::CacheDatabaseGenerator::TranslationKey.db = nil
  AwesomeTranslations::CacheDatabaseGenerator::HandlerTranslation.db = nil
  AwesomeTranslations::CacheDatabaseGenerator::TranslationValue.db = nil
end

#database_pathObject



92
93
94
# File 'lib/awesome_translations/cache_database_generator.rb', line 92

def database_path
  @database_path ||= self.class.database_path
end

#init_databaseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/awesome_translations/cache_database_generator.rb', line 22

def init_database
  @initialized = true
  @db = Baza::Db.new(type: :sqlite3, path: database_path, debug: false, type_translation: true)

  BazaModels.primary_db = @db

  AwesomeTranslations::CacheDatabaseGenerator::Group.db = @db
  AwesomeTranslations::CacheDatabaseGenerator::Group.table_name = "groups"

  AwesomeTranslations::CacheDatabaseGenerator::Handler.db = @db
  AwesomeTranslations::CacheDatabaseGenerator::Handler.table_name = "handlers"

  AwesomeTranslations::CacheDatabaseGenerator::ScannedFile.db = @db
  AwesomeTranslations::CacheDatabaseGenerator::ScannedFile.table_name = "scanned_files"

  AwesomeTranslations::CacheDatabaseGenerator::TranslationKey.db = @db
  AwesomeTranslations::CacheDatabaseGenerator::TranslationKey.table_name = "translation_keys"

  AwesomeTranslations::CacheDatabaseGenerator::HandlerTranslation.db = @db
  AwesomeTranslations::CacheDatabaseGenerator::HandlerTranslation.table_name = "handler_translations"

  AwesomeTranslations::CacheDatabaseGenerator::TranslationValue.db = @db
  AwesomeTranslations::CacheDatabaseGenerator::TranslationValue.table_name = "translation_values"
end

#initialized?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/awesome_translations/cache_database_generator.rb', line 84

def initialized?
  @initialized ||= false
end

#update_groups_for_handler(handler_model) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/awesome_translations/cache_database_generator.rb', line 111

def update_groups_for_handler(handler_model)
  @groups_found ||= {}
  handler = handler_model.at_handler

  handler.groups.each do |group|
    debug "Updating group: #{group.name}"
    group_model = AwesomeTranslations::CacheDatabaseGenerator::Group.find_or_initialize_by(
      handler_id: handler_model.id,
      identifier: group.id
    )
    group_model.assign_attributes(name: group.name)
    group_model.save!

    @groups_found[group_model.id] = true

    group_model.at_group = group
    yield group_model if block_given?
  end
end

#update_handlersObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/awesome_translations/cache_database_generator.rb', line 96

def update_handlers
  @handlers_found ||= {}

  AwesomeTranslations::Handler.all.each do |handler|
    debug "Updating handler: #{handler.name}"
    handler_model = AwesomeTranslations::CacheDatabaseGenerator::Handler.find_or_initialize_by(identifier: handler.id)
    handler_model.assign_attributes(name: handler.name)
    handler_model.save!

    @handlers_found[handler_model.id] = true

    yield handler_model if block_given?
  end
end

#update_translations_for_group(handler_model, group_model) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/awesome_translations/cache_database_generator.rb', line 131

def update_translations_for_group(handler_model, group_model)
  group = group_model.at_group
  @translation_keys_found ||= {}
  @handler_translations_found ||= {}

  group.translations.each do |translation|
    debug "Updating translation: #{translation.key}"

    translation_key = AwesomeTranslations::CacheDatabaseGenerator::TranslationKey.find_or_initialize_by(key: translation.key)
    translation_key.assign_attributes(group_id: group_model.id, handler_id: handler_model.id)
    translation_key.save!

    raise "KEY ERROR: #{translation_key.inspect}" unless translation_key.id.to_i.positive?

    @translation_keys_found[translation_key.id] = true

    handler_translation = AwesomeTranslations::CacheDatabaseGenerator::HandlerTranslation.find_or_initialize_by(
      translation_key_id: translation_key.id,
      handler_id: handler_model.id,
      group_id: group_model.id
    )
    handler_translation.assign_attributes(
      default: translation.default,
      file_path: translation.file_path,
      line_no: translation.line_no,
      key_show: translation.key_show,
      full_path: translation.full_path,
      dir: translation.dir
    )

    if @transactioner && handler_translation.persisted?
      @transactioner.save!(handler_translation)
    else
      handler_translation.save!
    end

    @handler_translations_found[handler_translation.id] = true
  end
end

#with_transactionerObject



67
68
69
70
71
72
73
74
# File 'lib/awesome_translations/cache_database_generator.rb', line 67

def with_transactioner
  require "active-record-transactioner"

  ActiveRecordTransactioner.new do |transactioner|
    @transactioner = transactioner
    yield
  end
end