Class: RFacter::Util::Collection Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Concurrent::Async, Enumerable, DSL
Defined in:
lib/rfacter/util/collection.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.

Manage which facts exist on a Node and how we access them.

Largely just a wrapper around a hash of facts that have been retrieved from a particular node.

Since:

  • 0.1.0

Constant Summary

Constants included from DSL

DSL::COLLECTION, DSL::NODE

Instance Method Summary collapse

Constructor Details

#initialize(node, config: RFacter::Config.config, **opts) ⇒ Collection

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.

Initialize a new Collection object

Parameters:

  • node (RFacter::Node)

    The node from which this collection should retrieve facts.

Since:

  • 0.1.0



31
32
33
34
35
36
37
# File 'lib/rfacter/util/collection.rb', line 31

def initialize(node, config: RFacter::Config.config, **opts)
  @node = node
  @config = config

  @facts = Hash.new
  @internal_loader = RFacter::Util::Loader.new
end

Instance Method Details

#[](name) ⇒ 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.

Return a fact object by name.

Since:

  • 0.1.0



40
41
42
# File 'lib/rfacter/util/collection.rb', line 40

def [](name)
  value(name)
end

#add(name, options = {}, &block) ⇒ RFacter::Util::Fact

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.

Add a resolution mechanism for a named fact. This does not distinguish between adding a new fact and adding a new way to resolve a fact.

Parameters:

  • name (Symbol)

    The name of the fact to define

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

    A hash of options to set on the fact and resolution

Returns:

Since:

  • 0.1.0



69
70
71
72
73
74
75
# File 'lib/rfacter/util/collection.rb', line 69

def add(name, options = {}, &block)
  fact = create_or_return_fact(name, options)

  fact.add(options, &block)

  return fact
end

#define_fact(name, options = {}, &block) ⇒ RFacter::Util::Fact

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.

Define a new fact or extend an existing fact.

Parameters:

  • name (Symbol)

    The name of the fact to define

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

    A hash of options to set on the fact

Returns:

Since:

  • 0.1.0



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rfacter/util/collection.rb', line 50

def define_fact(name, options = {}, &block)
  fact = create_or_return_fact(name, options)

  if block_given?
    fact.instance_eval(&block)
  end

  fact
rescue => e
  logger.log_exception(e, "Unable to add fact #{name}: #{e}")
end

#eachObject

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.

Iterate across all of the facts.

Since:

  • 0.1.0



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rfacter/util/collection.rb', line 80

def each
  load_all

  COLLECTION.bind(self) do
    NODE.bind(@node) do
      @facts.each do |name, fact|
        value = fact.value
        unless value.nil?
          yield name.to_s, value
        end
      end
    end
  end
end

#fact(name) ⇒ 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.

Return a fact by name.

Since:

  • 0.1.0



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rfacter/util/collection.rb', line 96

def fact(name)
  name = canonicalize(name)

  # Try to load the fact if necessary
  load(name) unless @facts[name]

  # Try HARDER
  load_all unless @facts[name]

  if @facts.empty?
    logger.warnonce("No facts loaded from #{@internal_loader.search_path.join(File::PATH_SEPARATOR)}")
  end

  @facts[name]
end

#flushObject

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.

Flush all cached values.

Since:

  • 0.1.0



113
114
115
# File 'lib/rfacter/util/collection.rb', line 113

def flush
  @facts.each { |name, fact| fact.flush }
end

#listObject

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.

Return a list of all of the facts.

Since:

  • 0.1.0



118
119
120
121
# File 'lib/rfacter/util/collection.rb', line 118

def list
  load_all
  return @facts.keys
end

#load(name) ⇒ 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



123
124
125
# File 'lib/rfacter/util/collection.rb', line 123

def load(name)
  @internal_loader.load(name, self)
end

#load_allObject

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.

Load all known facts.

Since:

  • 0.1.0



128
129
130
# File 'lib/rfacter/util/collection.rb', line 128

def load_all
  @internal_loader.load_all(self)
end

#to_hashObject

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.

Return a hash of all of our facts.

Since:

  • 0.1.0



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rfacter/util/collection.rb', line 133

def to_hash
  COLLECTION.bind(self) do
    NODE.bind(@node) do
      @facts.each_with_object({}) do |(name, fact), hash|
        resolved_value = fact.value

        # For backwards compatibility, convert the fact name to a string.
        hash[name.to_s] = resolved_value unless resolved_value.nil?
      end
    end
  end
end

#value(name) ⇒ 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



146
147
148
149
150
151
152
153
154
# File 'lib/rfacter/util/collection.rb', line 146

def value(name)
  COLLECTION.bind(self) do
    NODE.bind(@node) do
      if fact = fact(name)
        fact.value
      end
    end
  end
end