tml-rails 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ #--
2
+ # Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com
3
+ #
4
+ # _______ _ _ _ ______ _
5
+ # |__ __| | | | | (_) | ____| | |
6
+ # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
7
+ # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
8
+ # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
9
+ # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
10
+ # __/ |
11
+ # |___/
12
+ # Permission is hereby granted, free of charge, to any person obtaining
13
+ # a copy of this software and associated documentation files (the
14
+ # "Software"), to deal in the Software without restriction, including
15
+ # without limitation the rights to use, copy, modify, merge, publish,
16
+ # distribute, sublicense, and/or sell copies of the Software, and to
17
+ # permit persons to whom the Software is furnished to do so, subject to
18
+ # the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be
21
+ # included in all copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
+ #++
31
+
32
+ module TmlRails
33
+ class ToolsController < ApplicationController
34
+
35
+ def upgrade
36
+ Tml.cache.upgrade_version
37
+ redirect_back
38
+ end
39
+
40
+ def enable
41
+ tml_toggle_tools(true)
42
+ redirect_back
43
+ end
44
+
45
+ def disable
46
+ tml_toggle_tools(false)
47
+ redirect_back
48
+ end
49
+
50
+ private
51
+
52
+ def redirect_back
53
+ redirect_to(request.env['HTTP_REFERER'] || '/')
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,82 @@
1
+ <% if tml_session.tools_enabled? or type.blank? %>
2
+ <%
3
+ if tml_session.tools_enabled?
4
+ opts[:method] = 'Tml.UI.LanguageSelector.show(); return false;'
5
+ end
6
+ %>
7
+
8
+ <%= link_to('#',
9
+ :onclick => (opts[:method] || "alert('Custom language selector can be specified by passing it in the method attribute of the language selector tag.');"),
10
+ :style => (opts[:style] || 'padding-right:5px;text-decoration:none'),
11
+ :class => opts[:class]
12
+ ) do %>
13
+ <% unless opts[:hide_flag] %>
14
+ <%= image_tag(tml_current_language.flag_url, :style => "align:middle") %>
15
+ &nbsp;
16
+ <% end %>
17
+ <% if opts[:language] == :native %>
18
+ <%= tml_current_language.native_name %>
19
+ <% elsif opts[:language] == :english %>
20
+ <%= tml_current_language.english_name %>
21
+ <% else %>
22
+ <%= tml_current_language.english_name %>
23
+ <% end %>
24
+ <% end %>
25
+
26
+ <% elsif type == :list %>
27
+ <div style="padding-top:10px;">
28
+ <% tml_application.languages.each_with_index do |lang, index| %>
29
+ <% next if opts[:limit] and index >= opts[:limit] %>
30
+ <%= link_to(params.merge(:locale => lang.locale), :style => (opts[:style] || 'padding-right:5px;text-decoration:none'), :class => opts[:class]) do %>
31
+ <% if opts[:flags_only] or opts[:flags] %>
32
+ <%= image_tag(lang.flag_url) %>
33
+ <% end %>
34
+ <% unless opts[:flags_only] %>
35
+ <%= lang.english_name %>
36
+ <% end %>
37
+ <% end %>
38
+ <% end %>
39
+ </div>
40
+
41
+ <% elsif type == :dropdown %>
42
+
43
+ <script>
44
+ function tml_change_locale(selector) {
45
+ var query_parts = window.location.href.split('?');
46
+ var query = query_parts.length > 1 ? query_parts[1] : null;
47
+ var params = {};
48
+ if (query) {
49
+ var vars = query.split('&');
50
+ for (var i = 0; i < vars.length; i++) {
51
+ var pair = vars[i].split('=');
52
+ params[pair[0]] = pair[1];
53
+ }
54
+ }
55
+ params['locale'] = selector.options[selector.selectedIndex].value;
56
+
57
+ query = [];
58
+ var keys = Object.keys(params);
59
+ for (var i = 0; i < keys.length; i++) {
60
+ query.push(encodeURIComponent(keys[i]) + "=" + encodeURIComponent(params[keys[i]]));
61
+ }
62
+
63
+ var destination = query_parts[0] + '?' + query.join("&");
64
+ window.location = destination;
65
+ }
66
+ </script>
67
+
68
+ <select id="tml_language_selector" onchange="tml_change_locale(this)" style="<%=opts[:style]%>" class="<%=opts[:class]%>">
69
+ <% tml_application.languages.each do |lang| %>
70
+ <option dir='ltr' value="<%=lang.locale%>" <%="selected" if lang.locale == tml_current_locale %>>
71
+ <% if opts[:language] == :native %>
72
+ <%= lang.native_name %>
73
+ <% elsif opts[:language] == :english %>
74
+ <%= lang.english_name %>
75
+ <% else %>
76
+ <%= lang.english_name %>
77
+ <% end %>
78
+ </option>
79
+ <% end %>
80
+ </select>
81
+
82
+ <% end %>
@@ -0,0 +1,20 @@
1
+ <% if Tml.config.enabled? %>
2
+ <% featured_languages = tml_application.featured_languages %>
3
+ <% if featured_languages.any? %>
4
+ <style>
5
+ .tml_language_strip {text-align:center; color:#777;}
6
+ .tml_language_strip a {text-decoration:none; color:#777; outline:none; border:0px; font-size:10px;}
7
+ .tml_language_strip a img {border:0px;}
8
+ .tml_language_strip a:hover {text-decoration:underline}
9
+ </style>
10
+ <div class="tml_language_strip" style="font-size:12px;">
11
+ <% featured_languages.each do |lang| %>
12
+ <%= tml_language_name_tag(lang, opts) %> &nbsp;
13
+ <% end %>
14
+
15
+ <% if featured_languages.size > 0 %>
16
+ <%=link_to_function("&raquo;".html_safe, "Tml.UI.LanguageSelector.show()") %>
17
+ <% end %>
18
+ </div>
19
+ <% end %>
20
+ <% end %>
@@ -0,0 +1,18 @@
1
+ <div style="padding-top:40px; color: #ccc; text-align:center; width:100%">
2
+ <%= tr("Powered by {trex}", :trex => link_to("TranslationExchange.com", "http://translationexchange.com", :style=>"color:#ccc;")) %>
3
+
4
+ <style>
5
+ .trex_logo {
6
+ margin-top:10px;
7
+ padding: 40px;
8
+ background-repeat: no-repeat;
9
+ background-image:url(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAEMAAABQCAYAAABCiMhGAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYxIDY0LjE0MDk0OSwgMjAxMC8xMi8wNy0xMDo1NzowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNS4xIE1hY2ludG9zaCIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpCQTg4MTEyOEU0NkIxMUUzODhCMEJEOUNDRDQ0QkU0MiIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpCQTg4MTEyOUU0NkIxMUUzODhCMEJEOUNDRDQ0QkU0MiI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOkJBODgxMTI2RTQ2QjExRTM4OEIwQkQ5Q0NENDRCRTQyIiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOkJBODgxMTI3RTQ2QjExRTM4OEIwQkQ5Q0NENDRCRTQyIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+mszKSAAAAqFJREFUeNrsnE1IFVEYhr+53QiKEIkgpCBwJSH9SITkqkVELXJZ0iYIqRaBCZWgoWKikRBI1KVNmyg3gVKQ0CZE+9kHQYukaGP2I6iEP1ffc+dc7iwK5s5Mx3PPvC88zGxm5s4zc85853JmvNardwTxwC1wGuyWdGQGvARduaH23wUJkJHFcgoclnTmKzgIIT8yWOlNsQiVPeB+8c74hmWNMNXqzqimh0JqlIxVeigkr2Tk6aGQtQwdlEIZlEEZlEEZEZONuf0YmASLYNNGPRI1e8FFsHUjZNwAg5Zd3GHwHuw02Uw+WShCZRr0m+4zPljc9D+alpG1WMYW0zJsfgplxMGTYp1BGZRBGZRBGZRBGZRBGZRBGZRBGQxlUAZlUAZlUAZlUAZlUAZlUAZlUIbDMmyeYr3KO6OUJdMyGiyWcdS0DPV+ykMLRRwD16JuHGc60gV9Fd6KP/Vxs/hTEDvBbIT9nQLNEdp8XkpTH0/GMRl3bladJpi7EWQcB89dfLSW24EdAeOsM0T2gQkWXX4bf6P7mlTL2KFFbE97Ob4NvAO70j42UcdTbyHU2jo28Qwe7zXYb2nl6mXE3DzwF6DJ5kGrkjFv4EBP4laHBjKjZDz7zwfJgTOWi3iVG2qfVTKui//ZhMTaXmD9Hmi1XMSf4m9UMubAIfAY/Epg58t62QcuWyxhQfx37A7grvgcHKipgdU5UPWX538LuBlh8Deu+4oVS2X8BN8h4p+j1jlNMOW+5lQ8+QmpsIQpusr5vsYV8EUqNElWoKojHpYKTlIyusFtqfAkIWMA9IgDiStD/cXXIY4kjowHoE0cSlQZj8AlcSxRZDwF58XBhJGxFlgfBWfF0YSR4QXK62ZxOGFk1OtR7QlxPGFkjIBGSUHWBRgAlJNpO4bVinwAAAAASUVORK5CYII=);
10
+ }
11
+ </style>
12
+
13
+ <div style="text-align: center">
14
+ <%=link_to('http://translationexchange.com') do %>
15
+ <img class="trex_logo" src="" alt="" />
16
+ <% end %>
17
+ </div>
18
+ </div>
@@ -0,0 +1,44 @@
1
+ <% if Tml.config.enabled? %>
2
+ <style type="text/css">
3
+ .tml_fltr {<%=tml_style_attribute_tag('float', 'right')%> !important;}
4
+ .tml_fltl {<%=tml_style_attribute_tag('float', 'left')%> !important;}
5
+ .tml_txtr {<%=tml_style_attribute_tag('text-align', 'right')%> !important;}
6
+ .tml_txtl {<%=tml_style_attribute_tag('text-align', 'left')%> !important;}
7
+ <%= tml_application.css %>
8
+ </style>
9
+
10
+ <% if tml_session.tools_enabled? %>
11
+ <script>
12
+ (function() {
13
+ if (window.tml_already_initialized) return;
14
+ window.tml_already_initialized = true;
15
+
16
+ var script = window.document.createElement('script');
17
+ script.setAttribute('id', 'tml-tools');
18
+ script.setAttribute('type', 'application/javascript');
19
+ script.setAttribute('src', '<%= tml_application.tools['javascript'] %>');
20
+ script.setAttribute('charset', 'UTF-8');
21
+ script.onload = function() {
22
+ Tml.Utils.insertCSS(window.document, "<%= tml_application.tools['stylesheet'] %>", false);
23
+ Tml.app_key = '<%= tml_application.key %>';
24
+ Tml.host = '<%= tml_application.tools['host'] %>';
25
+ Tml.locale = '<%= tml_current_language.locale %>';
26
+ Tml.sources = [];
27
+ <%
28
+ if tml_application.feature_enabled?(:shortcuts)
29
+ tml_application.shortcuts.each do |key, script|
30
+ %>
31
+ shortcut.add('<%=key.html_safe%>', function() {<%= script.html_safe %>});
32
+ <%
33
+ end
34
+ end
35
+ %>
36
+ if (typeof(tml_on_ready) === 'function') {
37
+ tml_on_ready();
38
+ }
39
+ };
40
+ window.document.getElementsByTagName('head')[0].appendChild(script);
41
+ })();
42
+ </script>
43
+ <% end %>
44
+ <% end %>
data/config/routes.rb ADDED
@@ -0,0 +1,36 @@
1
+ #--
2
+ # Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com
3
+ #
4
+ # _______ _ _ _ ______ _
5
+ # |__ __| | | | | (_) | ____| | |
6
+ # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
7
+ # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
8
+ # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
9
+ # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
10
+ # __/ |
11
+ # |___/
12
+ # Permission is hereby granted, free of charge, to any person obtaining
13
+ # a copy of this software and associated documentation files (the
14
+ # "Software"), to deal in the Software without restriction, including
15
+ # without limitation the rights to use, copy, modify, merge, publish,
16
+ # distribute, sublicense, and/or sell copies of the Software, and to
17
+ # permit persons to whom the Software is furnished to do so, subject to
18
+ # the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be
21
+ # included in all copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
+ #++
31
+
32
+ Rails.application.routes.draw do
33
+ get '/tml/upgrade' => 'tml_client_sdk/tools#upgrade'
34
+ get '/tml/on' => 'tml_client_sdk/tools#enable'
35
+ get '/tml/off' => 'tml_client_sdk/tools#disable'
36
+ end
@@ -0,0 +1,62 @@
1
+ #--
2
+ # Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com
3
+ #
4
+ # _______ _ _ _ ______ _
5
+ # |__ __| | | | | (_) | ____| | |
6
+ # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
7
+ # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
8
+ # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
9
+ # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
10
+ # __/ |
11
+ # |___/
12
+ # Permission is hereby granted, free of charge, to any person obtaining
13
+ # a copy of this software and associated documentation files (the
14
+ # "Software"), to deal in the Software without restriction, including
15
+ # without limitation the rights to use, copy, modify, merge, publish,
16
+ # distribute, sublicense, and/or sell copies of the Software, and to
17
+ # permit persons to whom the Software is furnished to do so, subject to
18
+ # the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be
21
+ # included in all copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
+ #++
31
+
32
+ require 'i18n/backend/base'
33
+ require 'tml'
34
+
35
+ module I18n
36
+ module Backend
37
+ class Tml < I18n::Backend::Simple
38
+
39
+ module Implementation
40
+ include Base, Flatten
41
+
42
+ def application
43
+ ::Tml.session.application
44
+ end
45
+
46
+ def available_locales
47
+ application.locales
48
+ end
49
+
50
+ def lookup(locale, key, scope = [], options = {})
51
+ default_key = super(application.default_locale, key, scope, options)
52
+ default_key ||= key
53
+ application.language(locale.to_s).translate(default_key)
54
+ end
55
+
56
+ end
57
+
58
+ include Implementation
59
+ end
60
+ end
61
+ end
62
+
@@ -0,0 +1,60 @@
1
+ #--
2
+ # Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com
3
+ #
4
+ # _______ _ _ _ ______ _
5
+ # |__ __| | | | | (_) | ____| | |
6
+ # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
7
+ # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
8
+ # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
9
+ # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
10
+ # __/ |
11
+ # |___/
12
+ # Permission is hereby granted, free of charge, to any person obtaining
13
+ # a copy of this software and associated documentation files (the
14
+ # "Software"), to deal in the Software without restriction, including
15
+ # without limitation the rights to use, copy, modify, merge, publish,
16
+ # distribute, sublicense, and/or sell copies of the Software, and to
17
+ # permit persons to whom the Software is furnished to do so, subject to
18
+ # the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be
21
+ # included in all copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
+ #++
31
+
32
+ require 'tml'
33
+
34
+ namespace :tml do
35
+
36
+ namespace :cache do
37
+
38
+ desc 'upgrades shared translation cache'
39
+ task :upgrade => :environment do
40
+ Tml.cache.upgrade_version
41
+ end
42
+
43
+ desc 'generates local file cache'
44
+ task :generate => :environment do
45
+ Tml::Generators::Cache::File.new.run
46
+ end
47
+
48
+ desc 'rolls back to the previous version'
49
+ task :rollback => :environment do
50
+ Tml::Generators::Cache::File.new.rollback
51
+ end
52
+
53
+ desc 'rolls up to the next version'
54
+ task :rollup => :environment do
55
+ Tml::Generators::Cache::File.new.rollup
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com
4
+ #
5
+ # _______ _ _ _ ______ _
6
+ # |__ __| | | | | (_) | ____| | |
7
+ # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
8
+ # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
9
+ # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
10
+ # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
11
+ # __/ |
12
+ # |___/
13
+ # Permission is hereby granted, free of charge, to any person obtaining
14
+ # a copy of this software and associated documentation files (the
15
+ # "Software"), to deal in the Software without restriction, including
16
+ # without limitation the rights to use, copy, modify, merge, publish,
17
+ # distribute, sublicense, and/or sell copies of the Software, and to
18
+ # permit persons to whom the Software is furnished to do so, subject to
19
+ # the following conditions:
20
+ #
21
+ # The above copyright notice and this permission notice shall be
22
+ # included in all copies or substantial portions of the Software.
23
+ #
24
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+ #++
32
+
33
+ class Tml::CacheAdapters::Rails < Tml::Cache
34
+
35
+ def initialize
36
+ Tml.logger.info('Initializing Rails cache...')
37
+ @cache = Rails.cache
38
+ end
39
+
40
+ def cache_name
41
+ @cache.class.name
42
+ end
43
+
44
+ def read_only?
45
+ false
46
+ end
47
+
48
+ def fetch(key, opts = {})
49
+ miss = false
50
+ data = @cache.fetch(versioned_key(key, opts)) do
51
+ info("Cache miss: #{key}")
52
+ miss = true
53
+ if block_given?
54
+ yield
55
+ else
56
+ nil
57
+ end
58
+ end
59
+ info("Cache hit: #{key}") unless miss
60
+ data
61
+ rescue Exception => ex
62
+ warn("Failed to retrieve data: #{ex.message}")
63
+ return nil unless block_given?
64
+ yield
65
+ end
66
+
67
+ def store(key, data, opts = {})
68
+ info("Cache store: #{key}")
69
+ @cache.write(versioned_key(key, opts), data)
70
+ data
71
+ rescue Exception => ex
72
+ warn("Failed to store data: #{ex.message}")
73
+ key
74
+ end
75
+
76
+ def delete(key, opts = {})
77
+ info("Cache delete: #{key}")
78
+ @cache.delete(versioned_key(key, opts))
79
+ key
80
+ rescue Exception => ex
81
+ warn("Failed to delete data: #{ex.message}")
82
+ key
83
+ end
84
+
85
+ def exist?(key, opts = {})
86
+ data = @cache.fetch(versioned_key(key, opts))
87
+ not data.nil?
88
+ rescue Exception => ex
89
+ warn("Failed to check if key exists: #{ex.message}")
90
+ false
91
+ end
92
+
93
+ def clear(opts = {})
94
+ info("Cache clear")
95
+ rescue Exception => ex
96
+ warn("Failed to clear cache: #{ex.message}")
97
+ end
98
+ end