Class: Tml::Source
Instance Attribute Summary
Attributes inherited from Base
#attributes
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
attributes, belongs_to, has_many, hash_value, #hash_value, #method_missing, #to_hash, #update_attributes
Constructor Details
#initialize(attrs = {}) ⇒ Source
62
63
64
65
|
# File 'lib/tml/source.rb', line 62
def initialize(attrs = {})
super
self.key ||= Tml::Source.generate_key(attrs[:source])
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Tml::Base
Class Method Details
.cache_key(locale, source) ⇒ Object
58
59
60
|
# File 'lib/tml/source.rb', line 58
def self.cache_key(locale, source)
File.join(locale, 'sources', source.split('/'))
end
|
.generate_key(source) ⇒ Object
54
55
56
|
# File 'lib/tml/source.rb', line 54
def self.generate_key(source)
"#{Digest::MD5.hexdigest("#{source}")}~"[0..-2]
end
|
.normalize(url) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/tml/source.rb', line 40
def self.normalize(url)
return nil if url.nil? or url == ''
uri = URI.parse(url)
path = uri.path
return '/' if uri.path.nil? or uri.path == ''
return path if path == '/'
path = "/#{path}" if path[0] != '/'
path = path[0..-2] if path[-1] == '/'
path
end
|
Instance Method Details
#cached_translations(locale, key) ⇒ Object
96
97
98
99
100
|
# File 'lib/tml/source.rb', line 96
def cached_translations(locale, key)
self.translations ||= {}
self.translations[locale] ||= {}
self.translations[locale][key]
end
|
#fetch_translations(locale) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/tml/source.rb', line 67
def fetch_translations(locale)
self.translations ||= {}
return self if self.translations[locale]
self.translations[locale] = {}
results = self.application.api_client.get(
"sources/#{self.key}/translations",
{:locale => locale, :per_page => 10000},
{:cache_key => Tml::Source.cache_key(locale, self.source)}
)
results.each do |key, data|
translations_data = data.is_a?(Hash) ? data['translations'] : data
self.translations[locale][key] = translations_data.collect do |t|
Tml::Translation.new(
:locale => t['locale'] || locale,
:label => t['label'],
:context => t['context']
)
end
end
self
rescue Tml::Exception => ex
self.translations = {}
self
end
|
#reset_cache ⇒ Object
102
103
104
105
106
|
# File 'lib/tml/source.rb', line 102
def reset_cache
application.languages.each do |lang|
Tml.cache.delete(Tml::Source.cache_key(lang.locale, self.source))
end
end
|