Tizen 2.1 base
[external/device-mapper.git] / report-generators / lib / string-store.rb
1 # Copyright (C) 2010 Red Hat, Inc. All rights reserved.
2 #
3 # This copyrighted material is made available to anyone wishing to use,
4 # modify, copy, or redistribute it subject to the terms and conditions
5 # of the GNU General Public License v.2.
6 #
7 # You should have received a copy of the GNU General Public License
8 # along with this program; if not, write to the Free Software Foundation,
9 # Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
11 # Provides a simple way of accessing the contents of files by a symbol
12 # name.  Useful for erb templates.
13
14 require 'pathname'
15
16 class StringStore
17   attr_accessor :path
18
19   def initialize(p)
20     @paths = p.nil? ? Array.new : p # FIXME: do we need to copy p ?
21   end
22
23   def lookup(sym)
24     files = expansions(sym)
25
26     @paths.each do |p|
27       files.each do |f|
28         pn = Pathname.new("#{p}/#{f}")
29         if pn.file?
30           return pn.read
31         end
32       end
33     end
34
35     raise RuntimeError, "unknown string entry: #{sym}"
36   end
37
38   private
39   def expansions(sym)
40     ["#{sym}", "#{sym}.txt"]
41   end
42 end