Class: Tr8nCore::Generators::Cache::Base
- Inherits:
-
Object
- Object
- Tr8nCore::Generators::Cache::Base
- 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. ++
Instance Attribute Summary collapse
-
#finished_at ⇒ Object
Returns the value of attribute finished_at.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
Instance Method Summary collapse
- #api_client ⇒ Object
- #application ⇒ Object
- #cache(key, data) ⇒ Object
- #cache_application ⇒ Object
- #cache_languages ⇒ Object
- #cache_path ⇒ Object
- #cache_version ⇒ Object
- #execute ⇒ Object
- #finalize ⇒ Object
- #generate_symlink ⇒ Object
- #languages ⇒ Object
- #log(msg) ⇒ Object
- #prepare ⇒ Object
- #run ⇒ Object
- #symlink_path ⇒ Object
Instance Attribute Details
#finished_at ⇒ Object
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_at ⇒ Object
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_client ⇒ Object
70 71 72 |
# File 'lib/tr8n_core/generators/cache/base.rb', line 70 def api_client Tr8n.session.application.api_client end |
#application ⇒ Object
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
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_application ⇒ Object
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_languages ⇒ Object
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_path ⇒ Object
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_version ⇒ Object
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 |
#execute ⇒ Object
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 |
#finalize ⇒ Object
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 |
#generate_symlink ⇒ Object
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 |
#languages ⇒ Object
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 |
#prepare ⇒ Object
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 |
#run ⇒ Object
58 59 60 61 62 |
# File 'lib/tr8n_core/generators/cache/base.rb', line 58 def run prepare execute finalize end |
#symlink_path ⇒ Object
108 109 110 |
# File 'lib/tr8n_core/generators/cache/base.rb', line 108 def symlink_path "#{Tr8n.config.cache[:path]}/current" end |