class Puppet::Util::Feature

Attributes

path[R]

Public Instance Methods

add(name, options = {}) { || ... } click to toggle source

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.

# File lib/puppet/util/feature.rb, line 13
def add(name, options = {})
  method = name.to_s + "?"
  @results.delete(name)

  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
    # we return a cached result if:
    #  * if a block is given (and we just evaluated it above)
    #  * if we already have a positive result
    #  * if we've tested this feature before and it failed, but we're
    #    configured to always cache
    if block_given?     ||
        @results[name]  ||
        (@results.has_key?(name) and Puppet[:always_cache_features])
      @results[name]
    else
      @results[name] = test(name, options)
      @results[name]
    end
  end
end
load() click to toggle source
# File lib/puppet/util/feature.rb, line 51
def load
  @loader.loadall
end
method_missing(method, *args) click to toggle source
# File lib/puppet/util/feature.rb, line 55
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
test(name, options) click to toggle source

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.

# File lib/puppet/util/feature.rb, line 67
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

Public Class Methods

new(path) click to toggle source

Create a new feature collection.

# File lib/puppet/util/feature.rb, line 45
def initialize(path)
  @path = path
  @results = {}
  @loader = Puppet::Util::Autoload.new(self, @path)
end