summaryrefslogtreecommitdiffstats
path: root/lib/puppet/fact.rb
blob: 16c505af431d70b740df3ec2a25efc34e4def0a8 (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
#!/usr/local/bin/ruby -w

# $Id$

# an interface for registering and retrieving facts
# this is an abstract interface, and should just be used to interact
# with another library

# currently a very thin veneer on 'facter'

require 'facter'
require 'puppet'
require 'puppet/type'

module Puppet
	class Fact
        def Fact.[](name)
            fact = Facter[name]
            if fact.value.nil?
                raise "Could not retrieve fact %s" % name
            end
            debug("fact: got %s from %s for %s" % [fact.value,fact,name])
            return fact.value
        end

        # just pass the block to 'add'
        # the block has to do things like set the interpreter,
        # the code (which can be a ruby block), and maybe the
        # os and osrelease
        def Fact.add(name,&block)
            Facter[name].add(&block)
        end

        def Fact.name
            return :fact
        end

        def Fact.namevar
            return :name
        end

        #Puppet::Type.newtype(self)

        # we're adding a new resolution mechanism here; this is just how
        # types work
        # we don't have any real interest in the returned object
        def initialize(hash)
            name = hash[:name]
            hash.delete(:name)
            Fact.add(name) { |fact|
                method = nil
                hash.each { |key,value|
                    if key.is_a?(String)
                        method = key + "="
                    elsif key.is_a?(Symbol)
                        method = key.id2name + "="
                    else
                        raise "Key must be either string or symbol"
                    end
                    fact.send(method,value)
                }
            }
        end
    end
end