Class: Traject::TranslationMap::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/traject/translation_map.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



110
111
112
# File 'lib/traject/translation_map.rb', line 110

def initialize
  @cached = Hash.new
end

Instance Method Details

#_lookup!(path) ⇒ Object

force lookup, without using cache. used by cache. Returns the actual hash. Returns nil if none found. May raise on syntax error in file being loaded.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/traject/translation_map.rb', line 126

def _lookup!(path)
  found = nil

  $LOAD_PATH.each do |base|
    rb_file = File.join( base,  "translation_maps",  "#{path}.rb"  )
    yaml_file = File.join( base, "translation_maps", "#{path}.yaml"  )
    prop_file = File.join(base, "translation_maps", "#{path}.properties" )

    if File.exists? rb_file
      found = eval( File.open(rb_file).read , binding, rb_file )
      break
    elsif File.exists? yaml_file
      found = YAML.load_file(yaml_file)
      break
    elsif File.exists? prop_file
      found = Traject::TranslationMap.read_properties(prop_file)
      break
    end
  end

  # Cached hash can't be mutated without weird consequences, let's
  # freeze it!
  found.freeze if found

  return found
end

#lookup(path) ⇒ Object

Returns an actual Hash -- or nil if none found.



115
116
117
118
119
120
# File 'lib/traject/translation_map.rb', line 115

def lookup(path)
  unless @cached.has_key?(path)
    @cached[path] = _lookup!(path)
  end
  return @cached[path]
end

#reset_cache!Object



153
154
155
# File 'lib/traject/translation_map.rb', line 153

def reset_cache!
  @cached.clear
end