Class: Lit::Cache
- Inherits:
-
Object
- Object
- Lit::Cache
- Defined in:
- lib/lit/cache.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete_key(key) ⇒ Object
- #delete_locale(key) ⇒ Object
-
#export ⇒ Object
this comes directly from copycopter.
- #find_locale(locale_key) ⇒ Object
- #get_global_hits_counter(key) ⇒ Object
- #get_hits_counter(key) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #init_key_with_value(key, value) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #keys ⇒ Object
- #load_all_translations ⇒ Object
- #nested_string_keys_to_hash(db_localizations) ⇒ Object
- #refresh_key(key) ⇒ Object
- #reset ⇒ Object (also: #clear)
- #restore_hits_counter ⇒ Object
- #stop_hits_counter ⇒ Object
- #sync ⇒ Object
- #update_cache(key, value) ⇒ Object
- #update_locale(key, value, force_array = false) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
21 22 23 24 |
# File 'lib/lit/cache.rb', line 21 def initialize @hits_counter = Lit.get_key_value_engine @hits_counter_working = true end |
Instance Method Details
#[](key) ⇒ Object
26 27 28 29 30 |
# File 'lib/lit/cache.rb', line 26 def [](key) update_hits_count(key) ret = localizations[key] ret end |
#[]=(key, value) ⇒ Object
32 33 34 |
# File 'lib/lit/cache.rb', line 32 def []=(key, value) update_locale(key, value) end |
#delete_key(key) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/lit/cache.rb', line 92 def delete_key(key) key = key.to_s localizations.delete(key) key_without_locale = split_key(key).last localization_keys.delete(key_without_locale) I18n.backend.reload! end |
#delete_locale(key) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/lit/cache.rb', line 65 def delete_locale(key) key = key.to_s locale_key, key_without_locale = split_key(key) locale = find_locale(locale_key) delete_localization(locale, key_without_locale) end |
#export ⇒ Object
this comes directly from copycopter.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/lit/cache.rb', line 120 def export reset localizations_scope = Lit::Localization unless ENV['LOCALES'].blank? locale_keys = ENV['LOCALES'].to_s.split(',') || [] locale_ids = Lit::Locale.where(locale: locale_keys).pluck(:id) localizations_scope = localizations_scope.where(locale_id: locale_ids) unless locale_ids.empty? end db_localizations = {} localizations_scope.find_each do |l| db_localizations[l.full_key] = l.get_value end keys = nested_string_keys_to_hash(db_localizations) keys.to_yaml end |
#find_locale(locale_key) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/lit/cache.rb', line 109 def find_locale(locale_key) locale_key = locale_key.to_s @locale_cache ||= {} unless @locale_cache.has_key?(locale_key) locale = Lit::Locale.where(locale: locale_key).first_or_create! @locale_cache[locale_key] = locale end @locale_cache[locale_key] end |
#get_global_hits_counter(key) ⇒ Object
153 154 155 |
# File 'lib/lit/cache.rb', line 153 def get_global_hits_counter(key) @hits_counter['global_hits_counter.' + key] end |
#get_hits_counter(key) ⇒ Object
157 158 159 |
# File 'lib/lit/cache.rb', line 157 def get_hits_counter(key) @hits_counter['hits_counter.' + key] end |
#has_key?(key) ⇒ Boolean
40 41 42 |
# File 'lib/lit/cache.rb', line 40 def has_key?(key) localizations.has_key?(key) end |
#init_key_with_value(key, value) ⇒ Object
36 37 38 |
# File 'lib/lit/cache.rb', line 36 def init_key_with_value(key, value) update_locale(key, value, true) end |
#keys ⇒ Object
48 49 50 |
# File 'lib/lit/cache.rb', line 48 def keys localizations.keys end |
#load_all_translations ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lit/cache.rb', line 72 def load_all_translations first = Localization.order('id ASC').first last = Localization.order('id DESC').first if !first || !last || (!localizations.has_key?(first.full_key) || !localizations.has_key?(last.full_key)) Localization.includes([:locale, :localization_key]).find_each do |l| localizations[l.full_key] = l.get_value end end end |
#nested_string_keys_to_hash(db_localizations) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/lit/cache.rb', line 136 def nested_string_keys_to_hash(db_localizations) # http://subtech.g.hatena.ne.jp/cho45/20061122 deep_proc = proc do |_k, s, o| if s.is_a?(Hash) && o.is_a?(Hash) next s.merge(o, &deep_proc) end next o end keys = {} db_localizations.sort.each do |k, v| key_parts = k.to_s.split('.') converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } keys.merge!(converted, &deep_proc) end keys end |
#refresh_key(key) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/lit/cache.rb', line 84 def refresh_key(key) key = key.to_s locale_key, key_without_locale = split_key(key) locale = find_locale(locale_key) localization = find_localization(locale, key_without_locale) localizations[key] = localization.get_value if localization end |
#reset ⇒ Object Also known as: clear
100 101 102 103 104 105 |
# File 'lib/lit/cache.rb', line 100 def reset @locale_cache = {} localizations.clear localization_keys.clear load_all_translations end |
#restore_hits_counter ⇒ Object
165 166 167 |
# File 'lib/lit/cache.rb', line 165 def restore_hits_counter @hits_counter_working = true end |
#stop_hits_counter ⇒ Object
161 162 163 |
# File 'lib/lit/cache.rb', line 161 def stop_hits_counter @hits_counter_working = false end |
#sync ⇒ Object
44 45 46 |
# File 'lib/lit/cache.rb', line 44 def sync localizations.clear end |
#update_cache(key, value) ⇒ Object
60 61 62 63 |
# File 'lib/lit/cache.rb', line 60 def update_cache(key, value) key = key.to_s localizations[key] = value end |
#update_locale(key, value, force_array = false) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/lit/cache.rb', line 52 def update_locale(key, value, force_array = false) key = key.to_s locale_key, key_without_locale = split_key(key) locale = find_locale(locale_key) localization = find_localization(locale, key_without_locale, value, force_array, true) localizations[key] = localization.get_value if localization end |