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

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

Overview

– Copyright © 2014 Michael Berkovich, 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

#cache(key, data) ⇒ Object

Raises:



44
45
46
# File 'lib/tr8n_core/generators/cache/base.rb', line 44

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

#cache_applicationObject



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

def cache_application
  log("Downloading application...")
  app = Tr8n.session.application.get("application", :definition => true)
  cache(Tr8n::Application.cache_key(app["key"]), app)
  log("Application has been cached.")
  app
end

#cache_languagesObject



77
78
79
80
81
82
83
84
85
# File 'lib/tr8n_core/generators/cache/base.rb', line 77

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

#cache_pathObject

Raises:



40
41
42
# File 'lib/tr8n_core/generators/cache/base.rb', line 40

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

#executeObject

Raises:



48
49
50
# File 'lib/tr8n_core/generators/cache/base.rb', line 48

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

#finalizeObject



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

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


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

def generate_symlink
  FileUtils.rm(symlink_path) if File.exist?(symlink_path)
  FileUtils.ln_s(cache_path, symlink_path)
end

#log(msg) ⇒ Object



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

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

#prepareObject



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

def prepare
  @started_at = Time.now
end

#runObject



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

def run
  prepare
  execute
  finalize
end

Raises:



87
88
89
# File 'lib/tr8n_core/generators/cache/base.rb', line 87

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