Class: Puppet::Util::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/util/feature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Feature

Create a new feature collection.



34
35
36
37
38
# File 'lib/vendor/puppet/util/feature.rb', line 34

def initialize(path)
  @path = path
  @results = {}
  @loader = Puppet::Util::Autoload.new(self, @path)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/vendor/puppet/util/feature.rb', line 44

def method_missing(method, *args)
  return super unless method.to_s =~ /\?$/

  feature = method.to_s.sub(/\?$/, '')
  @loader.load(feature)

  respond_to?(method) && self.send(method)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



2
3
4
# File 'lib/vendor/puppet/util/feature.rb', line 2

def path
  @path
end

Instance Method Details

#add(name, options = {}) ⇒ Object

Create a new feature test. You have to pass the feature name, and it must be unique. You can either provide a block that will get executed immediately to determine if the feature is present, or you can pass an option to determine it. Currently, the only supported option is ‘libs’ (must be passed as a symbol), which will make sure that each lib loads successfully.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vendor/puppet/util/feature.rb', line 11

def add(name, options = {})
  method = name.to_s + "?"
  raise ArgumentError, "Feature #{name} is already defined" if self.class.respond_to?(method)

  if block_given?
    begin
      result = yield
    rescue Exception => detail
      warn "Failed to load feature test for #{name}: #{detail}"
      result = false
    end
    @results[name] = result
  end

  meta_def(method) do
    # Positive cache only, except blocks which are executed just once above
    final = @results[name] || block_given?
    @results[name] = test(name, options) unless final
    @results[name]
  end
end

#loadObject



40
41
42
# File 'lib/vendor/puppet/util/feature.rb', line 40

def load
  @loader.loadall
end

#test(name, options) ⇒ Object

Actually test whether the feature is present. We only want to test when someone asks for the feature, so we don’t unnecessarily load files.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vendor/puppet/util/feature.rb', line 56

def test(name, options)
  return true unless ary = options[:libs]
  ary = [ary] unless ary.is_a?(Array)

  ary.each do |lib|
    return false unless load_library(lib, name)
  end

  # We loaded all of the required libraries
  true
end