ReinH (owner)

Forks

Revisions

  • ebcd7d ReinH Thu Oct 08 14:13:53 -0700 2009
  • 483bf9 ReinH Thu Oct 08 14:09:29 -0700 2009
  • c1c2a0 ReinH Thu Oct 08 12:23:50 -0700 2009
  • 749895 ReinH Thu Oct 08 12:21:31 -0700 2009
gist: 205283 Download_button fork
public
Public Clone URL: git://gist.github.com/205283.git
Embed All Files: show embed
Ruby #
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
77
78
79
80
81
82
83
84
module Factor
    def self.add(name, &block)
        Factor::Resolvers.add(name, block)
    end
 
    # Facts are provided by returning immediate values and lazily
    # evaluating value strategy objects (things that respond to #call, like
    # simple Procs)
    def self.value(name)
        value = Factor::Resolvers[name]
        value = value.call while value.respond_to?(:call)
    end
 
end
 
module Factor::Resolvers
    # resolver table
    RESOLVERS = {}
    def self.[](key); RESOLVERS[key] end
 
    def self.add(name, block)
        RESOLVERS[name] = instance_eval(block)
    end
 
    private
 
    def self.exec(command, &block)
        block.call(`#{command}`)
    end
 
    # Lazy is used here to wrap an (imaginary) Windows Registry class with the necessary #call
    # interface.
    def self.registry(key)
        lazy { Factor::Platform::Windows::RegistryService.new(key).value }
    end
 
    def self.command(&block)
        block
    end
 
    def self.extract(regexp, options)
        exec(options[:from]){|output| output[regexp]}
    end
 
    def self.setcode(str=nil, &block)
        str ? exec(str) : block
    end
 
    # Provides lazy evaluation of a fact
    def lazy(&block); block end
end
 
 
# Example:
Factor.add(:hostname) do
 
    setcode do
        hostname = nil
        name = Factor::Util::Resolution.exec('hostname') or nil
        if name
            if name =~ /^([\w-]+)\.(.+)$/
                hostname = $1
                # the Domain class uses this
                $domain = $2
            else
                hostname = name
            end
            hostname
        else
            nil
        end
    end
 
    # becomes
 
    exec('hostname') do |name|
        name[/^([\w-]+)\.?/]
    end
 
    # becomes
 
    extract /^([\w-]+)\.?/, :from => 'hostname'
end