Class: Tr8nCore::Generators::Cache::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tr8n_core/generators/cache/base.rb

Overview

– Copyright © 2015 Translation Exchange Inc. translationexchange.com

_______                  _       _   _             ______          _

|__ __| | | | | (_) | __| | |

| |_ __ __ _ _ __  ___| | __ _| |_ _  ___  _ __ | |__  __  _____| |__   __ _ _ __   __ _  ___
| | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \|  __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
| | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ >  < (__| | | | (_| | | | | (_| |  __/
|_|_|  \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
                                                                                    __/ |
                                                                                   |___/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Direct Known Subclasses

Cdb, File

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#finished_atObject

Returns the value of attribute finished_at.



34
35
36
# File 'lib/tr8n_core/generators/cache/base.rb', line 34

def finished_at
  @finished_at
end

#started_atObject

Returns the value of attribute started_at.



34
35
36
# File 'lib/tr8n_core/generators/cache/base.rb', line 34

def started_at
  @started_at
end

Instance Method Details

#api_clientObject



70
71
72
# File 'lib/tr8n_core/generators/cache/base.rb', line 70

def api_client
  Tr8n.session.application.api_client
end

#applicationObject



74
75
76
# File 'lib/tr8n_core/generators/cache/base.rb', line 74

def application
  @application ||= api_client.get('applications/current', {:definition => true})
end

#cache(key, data) ⇒ Object

Raises:



50
51
52
# File 'lib/tr8n_core/generators/cache/base.rb', line 50

def cache(key, data)
  raise Tr8n::Exception.new('Must be implemented by the subclass')
end

#cache_applicationObject



89
90
91
92
93
# File 'lib/tr8n_core/generators/cache/base.rb', line 89

def cache_application
  log('Downloading application...')
  cache(Tr8n::Application.cache_key, application)
  log('Application has been cached.')
end

#cache_languagesObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tr8n_core/generators/cache/base.rb', line 95

def cache_languages
  log('Downloading languages...')
  unless application['languages']
    log('No languages are available...')
    return
  end
  languages.each do |lang|
    language = api_client.get("languages/#{lang['locale']}", :definition => true)
    cache(Tr8n::Language.cache_key(language['locale']), language)
  end
  log("#{application['languages'].count} languages have been cached.")
end

#cache_pathObject

Raises:



42
43
44
# File 'lib/tr8n_core/generators/cache/base.rb', line 42

def cache_path
  raise Tr8n::Exception.new('Must be implemented by the subclass')
end

#cache_versionObject



46
47
48
# File 'lib/tr8n_core/generators/cache/base.rb', line 46

def cache_version
  @started_at.strftime('%Y%m%d%H%M%S')
end

#executeObject

Raises:



54
55
56
# File 'lib/tr8n_core/generators/cache/base.rb', line 54

def execute
  raise Tr8n::Exception.new('Must be implemented by the subclass')
end

#finalizeObject



82
83
84
85
86
87
# File 'lib/tr8n_core/generators/cache/base.rb', line 82

def finalize
  @finished_at = Time.now
  log("Cache has been stored in #{cache_path}")
  log("Cache generation took #{@finished_at - @started_at} mls.")
  log('Done.')
end


112
113
114
115
116
# File 'lib/tr8n_core/generators/cache/base.rb', line 112

def generate_symlink
  FileUtils.rm(symlink_path) if File.exist?(symlink_path)
  FileUtils.ln_s(cache_version, symlink_path)
  log('Symlink has been updated.')
end

#languagesObject



78
79
80
# File 'lib/tr8n_core/generators/cache/base.rb', line 78

def languages
  application['languages']
end

#log(msg) ⇒ Object



36
37
38
39
40
# File 'lib/tr8n_core/generators/cache/base.rb', line 36

def log(msg)
  msg = "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}: #{msg}"
  puts msg
  Tr8n.logger.debug(msg)
end

#prepareObject



64
65
66
67
68
# File 'lib/tr8n_core/generators/cache/base.rb', line 64

def prepare
  @started_at = Time.now
  Tr8n.session.init
  cache_path
end

#runObject



58
59
60
61
62
# File 'lib/tr8n_core/generators/cache/base.rb', line 58

def run
  prepare
  execute
  finalize
end


108
109
110
# File 'lib/tr8n_core/generators/cache/base.rb', line 108

def symlink_path
  "#{Tr8n.config.cache[:path]}/current"
end