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.



76
77
78
# File 'lib/traject/translation_map.rb', line 76

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.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/traject/translation_map.rb', line 92

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

  return found
end

#lookup(path) ⇒ Object

Returns an actual Hash – or nil if none found.



81
82
83
84
85
86
# File 'lib/traject/translation_map.rb', line 81

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

#reset_cache!Object



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

def reset_cache!
  @cached.clear
end