Class: Lit::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/lit/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



22
23
24
25
26
# File 'lib/lit/cache.rb', line 22

def initialize
  @localizations = Lit.get_key_value_engine
  @hits_counter = Lit.get_key_value_engine
  @hits_counter_working = true
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
31
32
# File 'lib/lit/cache.rb', line 28

def [](key)
  update_hits_count(key)
  ret = @localizations[key]
  ret
end

#[]=(key, value) ⇒ Object



34
35
36
# File 'lib/lit/cache.rb', line 34

def []=(key, value)
  update_locale(key, value)
end

#delete_key(key) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/lit/cache.rb', line 94

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



67
68
69
70
71
72
# File 'lib/lit/cache.rb', line 67

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

#exportObject

this comes directly from copycopter.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lit/cache.rb', line 125

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



113
114
115
116
117
118
119
120
121
# File 'lib/lit/cache.rb', line 113

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



158
159
160
# File 'lib/lit/cache.rb', line 158

def get_global_hits_counter(key)
  @hits_counter['global_hits_counter.'+key]
end

#get_hits_counter(key) ⇒ Object



162
163
164
# File 'lib/lit/cache.rb', line 162

def get_hits_counter(key)
  @hits_counter['hits_counter.'+key]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/lit/cache.rb', line 42

def has_key?(key)
  @localizations.has_key?(key)
end

#init_key_with_value(key, value) ⇒ Object



38
39
40
# File 'lib/lit/cache.rb', line 38

def init_key_with_value(key, value)
  update_locale(key, value, true)
end

#keysObject



50
51
52
# File 'lib/lit/cache.rb', line 50

def keys
  @localizations.keys
end

#load_all_translationsObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lit/cache.rb', line 74

def load_all_translations
  first = Localization.order('id ASC').first
  last = Localization.order('id DESC').first
  if not first or not last or (not @localizations.has_key?(first.full_key) or
    not @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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lit/cache.rb', line 141

def nested_string_keys_to_hash(db_localizations)
  # http://subtech.g.hatena.ne.jp/cho45/20061122
  deep_proc = Proc.new { |k, s, o|
    if s.kind_of?(Hash) && o.kind_of?(Hash)
      next s.merge(o, &deep_proc)
    end
    next o
  }
  keys = {}
  db_localizations.sort.each do |k,v|
    key_parts = k.to_s.split('.')
    converted = key_parts.reverse.inject(v) { |a, n| { n => a } }
    keys.merge!(converted, &deep_proc)
  end
  keys
end

#refresh_key(key) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/lit/cache.rb', line 86

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

#resetObject Also known as: clear



102
103
104
105
106
107
108
109
# File 'lib/lit/cache.rb', line 102

def reset
  @locale_cache = {}
  @localizations = Lit.get_key_value_engine
  @localizations.clear
  @localization_keys = Lit.get_key_value_engine
  @localization_keys.clear
  load_all_translations
end

#restore_hits_counterObject



170
171
172
# File 'lib/lit/cache.rb', line 170

def restore_hits_counter
  @hits_counter_working = true
end

#stop_hits_counterObject



166
167
168
# File 'lib/lit/cache.rb', line 166

def stop_hits_counter
  @hits_counter_working = false
end

#syncObject



46
47
48
# File 'lib/lit/cache.rb', line 46

def sync
  @localizations.clear
end

#update_cache(key, value) ⇒ Object



62
63
64
65
# File 'lib/lit/cache.rb', line 62

def update_cache(key, value)
  key = key.to_s
  @localizations[key] = value
end

#update_locale(key, value, force_array = false) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/lit/cache.rb', line 54

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