Class: Tml::Generators::Base

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

Overview

– Copyright © 2016 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

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/tml/generators/base.rb', line 34

def finished_at
  @finished_at
end

#started_atObject

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

Instance Method Details

#api_clientObject



79
80
81
# File 'lib/tml/generators/base.rb', line 79

def api_client
  Tml.session.application.api_client
end

#applicationObject



83
84
85
# File 'lib/tml/generators/base.rb', line 83

def application
  @application ||= api_client.get('projects/current/definition', {})
end

#cache(key, data) ⇒ Object

Raises:



60
61
62
# File 'lib/tml/generators/base.rb', line 60

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

#cache_pathObject



42
43
44
45
46
47
48
49
50
# File 'lib/tml/generators/base.rb', line 42

def cache_path
  @cache_path ||= begin
    path = Tml.config.cache[:path]
    path ||= 'config/tml'
    FileUtils.mkdir_p(path)
    FileUtils.chmod(0777, path)
    path
  end
end

#cache_versionObject



56
57
58
# File 'lib/tml/generators/base.rb', line 56

def cache_version
  @cache_version ||= api_client.get_cache_version
end

#current_pathObject



52
53
54
# File 'lib/tml/generators/base.rb', line 52

def current_path
  "#{cache_path}/current"
end

#executeObject

Raises:



64
65
66
# File 'lib/tml/generators/base.rb', line 64

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

#finalizeObject



91
92
93
94
95
# File 'lib/tml/generators/base.rb', line 91

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

#languagesObject



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

def languages
  application['languages']
end

#log(msg) ⇒ Object



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

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

#prepareObject



74
75
76
77
# File 'lib/tml/generators/base.rb', line 74

def prepare
  @started_at = Time.now
  Tml.session.init
end

#runObject



68
69
70
71
72
# File 'lib/tml/generators/base.rb', line 68

def run
  prepare
  execute
  finalize
end

#ungzip(tarfile) ⇒ Object



97
98
99
100
101
102
# File 'lib/tml/generators/base.rb', line 97

def ungzip(tarfile)
  z = Zlib::GzipReader.new(tarfile)
  unzipped = StringIO.new(z.read)
  z.close
  unzipped
end

#untar(io, destination) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/tml/generators/base.rb', line 104

def untar(io, destination)
  Gem::Package::TarReader.new io do |tar|
    tar.each do |tarfile|
      destination_file = File.join destination, tarfile.full_name

      if tarfile.directory?
        FileUtils.mkdir_p destination_file
      else
        destination_directory = File.dirname(destination_file)
        FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
        File.open destination_file, "wb" do |f|
          f.print tarfile.read
        end
      end
    end
  end
end