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.


108
109
110
# File 'lib/traject/translation_map.rb', line 108

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.


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

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.exist? rb_file
      found = eval( File.open(rb_file).read , binding, rb_file )
      break
    elsif File.exist? yaml_file
      found = YAML.load_file(yaml_file)
      break
    elsif File.exist? 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.


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

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

#reset_cache!Object


151
152
153
# File 'lib/traject/translation_map.rb', line 151

def reset_cache!
  @cached.clear
end