Class: RFacter::Core::Aggregate Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Resolvable, Suitable
Defined in:
lib/rfacter/core/aggregate.rb

Overview

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

Aggregates provide a mechanism for facts to be resolved in multiple steps.

Aggregates are evaluated in two parts: generating individual chunks and then aggregating all chunks together. Each chunk is a block of code that generates a value, and may depend on other chunks when it runs. After all chunks have been evaluated they are passed to the aggregate block as ‘Hash<name, result>`. The aggregate block converts the individual chunks into a single value that is returned as the final value of the aggregate.

Since:

  • 0.1.0

Defined Under Namespace

Classes: DependencyError

Instance Attribute Summary collapse

Attributes included from Resolvable

#timeout

Attributes included from Suitable

#weight

Instance Method Summary collapse

Methods included from Resolvable

#flush, #limit, #on_flush, #value

Methods included from Suitable

#confine, #has_weight, #suitable?

Constructor Details

#initialize(name, fact, config: RFacter::Config.config, **options) ⇒ Aggregate

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 a new instance of Aggregate.

Since:

  • 0.1.0



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rfacter/core/aggregate.rb', line 48

def initialize(name, fact, config: RFacter::Config.config, **options)
  @name = name
  @fact = fact
  @config = config

  @confines = []
  @chunks = {}

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

Instance Attribute Details

#confinesObject (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.

Since:

  • 0.1.0



42
43
44
# File 'lib/rfacter/core/aggregate.rb', line 42

def confines
  @confines
end

#depsObject (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.

Since:

  • 0.1.0



36
37
38
# File 'lib/rfacter/core/aggregate.rb', line 36

def deps
  @deps
end

#factRFacter::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:

  • 0.1.0



46
47
48
# File 'lib/rfacter/core/aggregate.rb', line 46

def fact
  @fact
end

#nameObject (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.

Since:

  • 0.1.0



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

def name
  @name
end

Instance Method Details

#aggregate {|Hash<Symbol, Object>| ... } ⇒ void

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.

This method returns an undefined value.

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

Since:

  • 0.1.0



141
142
143
144
145
146
147
# File 'lib/rfacter/core/aggregate.rb', line 141

def aggregate(&block)
  if block_given?
    @aggregate = block
  else
    raise ArgumentError, "#{self.class.name}#aggregate requires a block"
  end
end

#chunk(name, opts = {}) {|*Object| ... } ⇒ void

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.

This method returns an undefined value.

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: {})

Options Hash (opts):

  • require (Array<Symbol>, Symbol)

    One or more chunks to evaluate and pass to this block.

Yields:

  • (*Object)

    Zero or more chunk results

Since:

  • 0.1.0



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rfacter/core/aggregate.rb', line 102

def chunk(name, opts = {}, &block)
  if not block_given?
    raise ArgumentError, "#{self.class.name}#chunk requires a block"
  end

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

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

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

#evaluate(&block) ⇒ Object

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.

Since:

  • 0.1.0



78
79
80
# File 'lib/rfacter/core/aggregate.rb', line 78

def evaluate(&block)
  instance_eval(&block)
end

#resolution_typeObject

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.

Since:

  • 0.1.0



149
150
151
# File 'lib/rfacter/core/aggregate.rb', line 149

def resolution_type
  :aggregate
end

#set_options(options) ⇒ Object

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.

Since:

  • 0.1.0



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rfacter/core/aggregate.rb', line 60

def set_options(options)
  if options[:name]
    @name = options.delete(:name)
  end

  if options.has_key?(:timeout)
    @timeout = options.delete(:timeout)
  end

  if options.has_key?(:weight)
    @weight = options.delete(:weight)
  end

  if not options.keys.empty?
    raise ArgumentError, "Invalid aggregate options #{options.keys.inspect}"
  end
end