Parent

Class/Module Index [+]

Quicksearch

Facter::Util::Fact

This class represents a fact. Each fact has a name and multiple {Facter::Util::Resolution resolutions}.

Create facts using {Facter.add}

@api public

Attributes

ldapname[RW]

@return [String] @deprecated

name[RW]

The name of the fact @return [String]

Public Class Methods

new(name, options = {}) click to toggle source

Creates a new fact, with no resolution mechanisms. See {Facter.add} for the public API for creating facts. @param name [String] the fact name @param options [Hash] optional parameters @option options [String] :ldapname set the ldapname property on the fact

@api private

# File lib/facter/util/fact.rb, line 27
def initialize(name, options = {})
  @name = name.to_s.downcase.intern

  extract_ldapname_option!(options)

  @ldapname ||= @name.to_s

  @resolves = []
  @searching = false

  @value = nil
end

Public Instance Methods

add(options = {}, &block) click to toggle source

Adds a new {Facter::Util::Resolution resolution}. This requires a block, which will then be evaluated in the context of the new resolution.

@param options [Hash] A hash of options to set on the resolution

@return [Facter::Util::Resolution]

@api private

# File lib/facter/util/fact.rb, line 49
def add(options = {}, &block)
  define_resolution(nil, options, &block)
end
define_resolution(resolution_name, options = {}, &block) click to toggle source

Define a new named resolution or return an existing resolution with the given name.

@param resolution_name [String] The name of the resolve to define or look up @param options [Hash] A hash of options to set on the resolution @return [Facter::Util::Resolution]

@api public

# File lib/facter/util/fact.rb, line 61
def define_resolution(resolution_name, options = {}, &block)

  resolution_type = options.delete(:type) || :simple

  resolve = create_or_return_resolution(resolution_name, resolution_type)

  resolve.set_options(options) unless options.empty?
  resolve.evaluate(&block) if block

  resolve
rescue => e
  Facter.log_exception(e, "Unable to add resolve #{resolution_name.inspect} for fact #{@name}: #{e.message}")
end
extract_ldapname_option!(options) click to toggle source

@api private @deprecated

# File lib/facter/util/fact.rb, line 124
def extract_ldapname_option!(options)
  if options[:ldapname]
    Facter.warnonce("ldapname is deprecated and will be removed in a future version")
    self.ldapname = options.delete(:ldapname)
  end
end
flush() click to toggle source

Flushes any cached values.

@return [void]

@api private

# File lib/facter/util/fact.rb, line 92
def flush
  @resolves.each { |r| r.flush }
  @value = nil
end
resolution(name) click to toggle source

Retrieve an existing resolution by name

@param name [String]

@return [Facter::Util::Resolution, nil] The resolution if exists, nil if

it doesn't exist or name is nil
# File lib/facter/util/fact.rb, line 81
def resolution(name)
  return nil if name.nil?

  @resolves.find { |resolve| resolve.name == name }
end
value() click to toggle source

Returns the value for this fact. This searches all resolutions by suitability and weight (see {Facter::Util::Resolution}). If no suitable resolution is found, it returns nil.

@api public

# File lib/facter/util/fact.rb, line 102
def value
  return @value if @value

  if @resolves.empty?
    Facter.debug "No resolves for %s" % @name
    return nil
  end

  searching do

    suitable_resolutions = sort_by_weight(find_suitable_resolutions(@resolves))
    @value = find_first_real_value(suitable_resolutions)

    announce_when_no_suitable_resolution(suitable_resolutions)
    announce_when_no_value_found(@value)

    @value
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.