summaryrefslogtreecommitdiffstats
path: root/lib/puppet/string/string_collection.rb
blob: 45a192703e69d3fc77dc428f1ef5e505361c80fa (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'puppet/string'

module Puppet::String::StringCollection
  SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/

  @strings = Hash.new { |hash, key| hash[key] = {} }

  def self.strings
    unless @loaded
      @loaded = true
      $LOAD_PATH.each do |dir|
        next unless FileTest.directory?(dir)
        Dir.chdir(dir) do
          Dir.glob("puppet/string/v*/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file|
            iname = file.sub(/\.rb/, '')
            begin
              require iname
            rescue Exception => detail
              puts detail.backtrace if Puppet[:trace]
              raise "Could not load #{iname} from #{dir}/#{file}: #{detail}"
            end
          end
        end
      end
    end
    return @strings.keys
  end

  def self.validate_version(version)
    !!(SEMVER_VERSION =~ version.to_s)
  end

  def self.[](name, version)
    @strings[underscorize(name)][version] if string?(name, version)
  end

  def self.string?(name, version)
    name = underscorize(name)
    cache = @strings[name]
    return true if cache.has_key?(version)

    loaded = cache.keys

    files = ["puppet/string/#{name}"]
    unless version == :current
      files << "#{name}@#{version}/puppet/string/#{name}"
    end

    files.each do |file|
      begin
        require file
        if version == :current || !file.include?('@')
          loaded = (cache.keys - loaded).first
          cache[:current] = cache[loaded] unless loaded.nil?
        end
        return true if cache.has_key?(version)
      rescue LoadError
        # pass
      end
    end

    return false
  end

  def self.register(string)
    @strings[underscorize(string.name)][string.version] = string
  end

  def self.underscorize(name)
    unless name.to_s =~ /^[-_a-z]+$/i then
      raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid string name"
    end

    name.to_s.downcase.split(/[-_]/).join('_').to_sym
  end
end