class Tml::Source
Public Class Methods
cache_key(locale, source)
click to toggle source
# File lib/tml/source.rb, line 58 def self.cache_key(locale, source) File.join(locale, 'sources', source.split('/')) end
generate_key(source)
click to toggle source
# File lib/tml/source.rb, line 54 def self.generate_key(source) "#{Digest::MD5.hexdigest("#{source}")}~"[0..-2] end
new(attrs = {})
click to toggle source
Calls superclass method
Tml::Base::new
# File lib/tml/source.rb, line 62 def initialize(attrs = {}) super self.key ||= Tml::Source.generate_key(attrs[:source]) end
normalize(url)
click to toggle source
# 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 == '/' # always must start with / path = "/#{path}" if path[0] != '/' # should not end with / path = path[0..-2] if path[-1] == '/' path end
Public Instance Methods
cached_translations(locale, key)
click to toggle source
# File lib/tml/source.rb, line 113 def cached_translations(locale, key) self.translations ||= {} self.translations[locale] ||= {} self.translations[locale][key] end
fetch_translations(locale)
click to toggle source
# File lib/tml/source.rb, line 92 def fetch_translations(locale) self.translations ||= {} return self if Tml.session.block_option(:dry) return self if self.translations[locale] # Tml.logger.debug("Fetching #{source}") data = self.application.api_client.get( "sources/#{self.key}/translations", {:locale => locale, :all => true, :ignored => true, :app_id => self.application.key}, {:cache_key => Tml::Source.cache_key(locale, self.source), :raw_json => true} ) || {} update_translations(locale, data) self rescue Tml::Exception => ex self.translations = {} self end
ignored_key?(key)
click to toggle source
# File lib/tml/source.rb, line 67 def ignored_key?(key) return false if ignored_keys.nil? not ignored_keys.index(key).nil? end
reset_cache()
click to toggle source
# File lib/tml/source.rb, line 119 def reset_cache application.languages.each do |lang| Tml.cache.delete(Tml::Source.cache_key(lang.locale, self.source)) end end
update_translations(locale, data)
click to toggle source
# File lib/tml/source.rb, line 72 def update_translations(locale, data) self.translations ||= {} self.translations[locale] = {} self.ignored_keys = data['ignored_keys'] || [] data = data['results'] if data.is_a?(Hash) and data['results'] data.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'], locked: t['locked'], context: t['context'] ) end end end