class Tr8nCore::Generators::Cache::File

Public Instance Methods

cache(key, data) click to toggle source
# File lib/tr8n_core/generators/cache/file.rb, line 56
def cache(key, data)
  File.open(file_path(key), 'w') { |file| file.write(JSON.pretty_generate(data)) }
end
cache_path() click to toggle source
# File lib/tr8n_core/generators/cache/file.rb, line 34
def cache_path
  @cache_path ||= begin
    path = "#{Tr8n.config.cache[:path]}/#{cache_version}"
    log("Cache will be stored in #{path}")
    FileUtils.mkdir_p(path)
    FileUtils.chmod(0777, path)
    path
  end
end
cache_translations() click to toggle source
# File lib/tr8n_core/generators/cache/file.rb, line 67
def cache_translations
  log('Downloading translations...')

  languages.each do |language|
    log("Downloading #{language['english_name']} language...")

    if Tr8n.config.cache[:segmented]
      api_client.paginate('applications/current/sources') do |source|
        next unless source['source']

        cache_path = Tr8n::Source.cache_key(language['locale'], source['source'])
        log("Downloading #{source['source']} in #{language['locale']} to #{cache_path}...")

        data = api_client.get("sources/#{source['key']}/translations", {:locale => language['locale'], :original => true, :per_page => 1000})
        cache(cache_path, data)
      end
    else
      cache_path = Tr8n::Application.translations_cache_key(language['locale'])
      log("Downloading translations in #{language['locale']} to #{cache_path}...")
      data = {}
      api_client.paginate('applications/current/translations', {:locale => language['locale'], :original => true, :per_page => 1000}) do |translations|
        data.merge!(translations)
      end
      cache(cache_path, data)
    end
  end
end
execute() click to toggle source
# File lib/tr8n_core/generators/cache/file.rb, line 60
def execute
  cache_application
  cache_languages
  cache_translations
  generate_symlink
end
file_path(key) click to toggle source
# File lib/tr8n_core/generators/cache/file.rb, line 44
def file_path(key)
  path = key.split('/')
  if path.count > 1
    filename = path.pop
    path = File.join(cache_path, path)
    FileUtils.mkdir_p(path)
    File.join(path, "#{filename}.json")
  else
    File.join(cache_path, "#{path.first}.json")
  end
end
rollback() click to toggle source
# File lib/tr8n_core/generators/cache/file.rb, line 95
def rollback
  folders = Dir["#{Tr8n.config.cache[:path]}/*"]
  folders.delete_if{|e| e.index('current')}.sort!

  if File.exist?(symlink_path)
    current_dest = File.readlink("#{Tr8n.config.cache[:path]}/current")
    current_dest = "#{Tr8n.config.cache[:path]}/#{current_dest}"
  else
    current_dest = 'undefined'
  end

  index = folders.index(current_dest)

  if index == 0
    log('There are no earlier cache versions')
    return
  end

  if index.nil?
    new_version_path = folders[folders.size-1]
  else
    new_version_path = folders[index-1]
  end

  new_version_path = new_version_path.split('/').last

  FileUtils.rm(symlink_path) if File.exist?(symlink_path)
  FileUtils.ln_s(new_version_path, symlink_path)

  log("Cache has been rolled back to version #{new_version_path}.")
end
rollup() click to toggle source
# File lib/tr8n_core/generators/cache/file.rb, line 127
def rollup
  folders = Dir["#{Tr8n.config.cache[:path]}/*"]
  folders.delete_if{|e| e.index('current')}.sort!

  if File.exist?(symlink_path)
    current_dest = File.readlink("#{Tr8n.config.cache[:path]}/current")
    current_dest = "#{Tr8n.config.cache[:path]}/#{current_dest}"
  else
    current_dest = 'undefined'
  end

  index = folders.index(current_dest)

  if index == (folders.size - 1)
    log('You are on the latest version of the cache already. No further versions are available')
    return
  end

  if index.nil?
    new_version_path = folders[0]
  else
    new_version_path = folders[index+1]
  end

  new_version_path = new_version_path.split('/').last

  FileUtils.rm(symlink_path) if File.exist?(symlink_path)
  FileUtils.ln_s(new_version_path, symlink_path)

  log("Cache has been upgraded to version #{new_version_path}.")
end