summaryrefslogtreecommitdiffstats
path: root/report-generators/lib/string-store.rb
blob: 36d819a9594e5a27d99723355605c9d2440fa8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Provides a simple way of accessing the contents of files by a symbol
# name.  Useful for erb templates.

require 'pathname'

class StringStore
  attr_accessor :path

  def initialize(p)
    @paths = p.nil? ? Array.new : p # FIXME: do we need to copy p ?
  end

  def lookup(sym)
    files = expansions(sym)

    @paths.each do |p|
      files.each do |f|
        pn = Pathname.new("#{p}/#{f}")
        if pn.file?
          return pn.read
        end
      end
    end

    raise RuntimeError, "unknown string entry: #{sym}"
  end

  private
  def expansions(sym)
    ["#{sym}", "#{sym}.txt"]
  end
end