Class: Facter::Core::Aggregate

Inherits:
Object
  • Object
show all
Includes:
LegacyFacter::Core::Resolvable, LegacyFacter::Core::Suitable
Defined in:
lib/facter/custom_facts/core/aggregate.rb

Overview

Since:

  • 2.0.0

Defined Under Namespace

Classes: DependencyError

Instance Attribute Summary collapse

Attributes included from LegacyFacter::Core::Resolvable

#logger, #timeout

Instance Method Summary collapse

Methods included from LegacyFacter::Core::Resolvable

#flush, #limit, #on_flush, #value

Methods included from LegacyFacter::Core::Suitable

#confine, #has_weight, #suitable?, #weight

Constructor Details

#initialize(name, fact) ⇒ Facter::Util::Resolution

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new aggregated resolution mechanism.

Parameters:

  • name (String)

    The name of the resolution.

  • fact (Facter::Fact)

    The fact to which this resolution will be added.

Since:

  • 2.0.0



72
73
74
75
76
77
78
79
80
81
# File 'lib/facter/custom_facts/core/aggregate.rb', line 72

def initialize(name, fact)
  @name = name
  @fact = fact

  @confines = []
  @chunks = {}

  @aggregate = nil
  @deps = LegacyFacter::Core::DirectedGraph.new
end

Instance Attribute Details

#confinesArray<LegacyFacter::Core::Confine> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns An array of confines restricting this to a specific platform.

Returns:

  • (Array<LegacyFacter::Core::Confine>)

    An array of confines restricting this to a specific platform

Since:

  • 2.0.0



47
48
49
# File 'lib/facter/custom_facts/core/aggregate.rb', line 47

def confines
  @confines
end

#depsLegacyFacter::Core::DirectedGraph (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 2.0.0



39
40
41
# File 'lib/facter/custom_facts/core/aggregate.rb', line 39

def deps
  @deps
end

#factFacter::Util::Fact (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 2.0.0



54
55
56
# File 'lib/facter/custom_facts/core/aggregate.rb', line 54

def fact
  @fact
end

#fact_typeSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The fact type of the aggregate resolution.

Returns:

  • (Symbol)

    The fact type of the aggregate resolution

Since:

  • 2.0.0



32
33
34
# File 'lib/facter/custom_facts/core/aggregate.rb', line 32

def fact_type
  @fact_type
end

#last_evaluatedString (readonly)

Returns:

  • (String)

Since:

  • 2.0.0



61
62
63
# File 'lib/facter/custom_facts/core/aggregate.rb', line 61

def last_evaluated
  @last_evaluated
end

#nameSymbol (readonly)

Returns The name of the aggregate resolution.

Returns:

  • (Symbol)

    The name of the aggregate resolution

Since:

  • 2.0.0



25
26
27
# File 'lib/facter/custom_facts/core/aggregate.rb', line 25

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ bool

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compares the weight of two aggregate facts

Returns:

  • (bool)

    Weight comparison result

Since:

  • 2.0.0



88
89
90
# File 'lib/facter/custom_facts/core/aggregate.rb', line 88

def <=>(other)
  weight <=> other.weight
end

#aggregate {|Hash<Symbol, Object>| ... } ⇒ Facter::Core::Aggregate

Define how all chunks should be combined

Examples:

Merge all chunks

aggregate.aggregate do |chunks|
  final_result = {}
  chunks.each_value do |chunk|
    final_result.deep_merge(chunk)
  end
  final_result
end

Sum all chunks

aggregate.aggregate do |chunks|
  total = 0
  chunks.each_value do |chunk|
    total += chunk
  end
  total
end

Yields:

  • (Hash<Symbol, Object>)

    A hash containing chunk names and chunk values

Returns:

Raises:

  • (ArgumentError)

Since:

  • 2.0.0



182
183
184
185
186
187
# File 'lib/facter/custom_facts/core/aggregate.rb', line 182

def aggregate(&block)
  raise ArgumentError, "#{self.class.name}#aggregate requires a block" unless block_given?

  @aggregate = block
  self
end

#chunk(name, opts = {}, &block) ⇒ Facter::Core::Aggregate

Define a new chunk for the given aggregate

Examples:

Defining a chunk with no dependencies

aggregate.chunk(:mountpoints) do
  # generate mountpoint information
end

Defining an chunk to add mount options

aggregate.chunk(:mount_options, :require => [:mountpoints]) do |mountpoints|
  # `mountpoints` is the result of the previous chunk
  # generate mount option information based on the mountpoints
end

Parameters:

  • name (Symbol)

    A name unique to this aggregate describing the chunk

  • opts (Hash) (defaults to: {})

    Hash with options for the aggregate fact

Returns:

Since:

  • 2.0.0



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/facter/custom_facts/core/aggregate.rb', line 142

def chunk(name, opts = {}, &block)
  evaluate_params(name, &block)

  deps = Array(opts.delete(:require))

  unless opts.empty?
    raise ArgumentError, "Unexpected options passed to #{self.class.name}#chunk: #{opts.keys.inspect}"
  end

  @deps[name] = deps
  @chunks[name] = block
  self
end

#evaluate(&block) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates the given block

Returns:

  • (String)

    Result of the block’s evaluation

Since:

  • 2.0.0



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/facter/custom_facts/core/aggregate.rb', line 110

def evaluate(&block)
  if @last_evaluated
    msg = +"Already evaluated #{@name}"
    msg << " at #{@last_evaluated}" if msg.is_a? String
    msg << ', reevaluating anyways'
    log.warn msg
  end
  instance_eval(&block)

  @last_evaluated = block.source_location.join(':')
end

#options(options) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets options for the aggregate fact

Returns:

  • (nil)

Raises:

  • (ArgumentError)

Since:

  • 2.0.0



97
98
99
100
101
102
103
# File 'lib/facter/custom_facts/core/aggregate.rb', line 97

def options(options)
  accepted_options = %i[name timeout weight fact_type]
  accepted_options.each do |option_name|
    instance_variable_set("@#{option_name}", options.delete(option_name)) if options.key?(option_name)
  end
  raise ArgumentError, "Invalid aggregate options #{options.keys.inspect}" unless options.keys.empty?
end

#resolution_typeSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the fact’s resolution type

Returns:

  • (Symbol)

    The fact’s type

Since:

  • 2.0.0



194
195
196
# File 'lib/facter/custom_facts/core/aggregate.rb', line 194

def resolution_type
  :aggregate
end