Class: ShadowFacter::Facts

Inherits:
Object
  • Object
show all
Defined in:
lib/shadow_facter/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(namespace, keys) ⇒ Facts

Returns a new instance of Facts.



10
11
12
13
# File 'lib/shadow_facter/base.rb', line 10

def initialize(namespace, keys)
  @namespace = namespace
  @keys = keys
end

Instance Method Details

#[](key) ⇒ Object

Return a fact value by key. Returns nil if non-existent or not constrained.



16
17
18
# File 'lib/shadow_facter/base.rb', line 16

def [](key)
  value(key)
end

#has_fact?(key) ⇒ Boolean

Return a boolean on the availability of a fact.

Returns:

  • (Boolean)


32
33
34
# File 'lib/shadow_facter/base.rb', line 32

def has_fact?(key)
  !value(key).nil?
end

#keysObject

Return an array of all of the constrained fact keys.



27
28
29
# File 'lib/shadow_facter/base.rb', line 27

def keys
  @keys.uniq.select { |k| !value(k).nil? }
end

#reload!Object

Reload facts in namespace.



57
58
59
# File 'lib/shadow_facter/base.rb', line 57

def reload!
  keys.each { |key| Facter[Base.facter_key(@namespace, key)].flush }
end

#to_hashObject

Return a hash of all of the constrained fact keys and values.



37
38
39
40
41
42
# File 'lib/shadow_facter/base.rb', line 37

def to_hash
  keys.inject({}) do |h, k|
    h[k] = value(k)
    h
  end
end

#to_jsonObject

Return json of all of the constrained fact keys and values.



51
52
53
54
# File 'lib/shadow_facter/base.rb', line 51

def to_json
  require 'json'
  JSON.pretty_generate to_hash
end

#to_yamlObject

Return yaml of all of the constrained fact keys and values.



45
46
47
48
# File 'lib/shadow_facter/base.rb', line 45

def to_yaml
  require 'yaml'
  to_hash.to_yaml
end

#value(key) ⇒ Object

Return a fact value by key. Returns nil if non-existent or not constrained.



21
22
23
24
# File 'lib/shadow_facter/base.rb', line 21

def value(key)
  f = Facter[Base.facter_key(@namespace, key)]
  f.value unless f.nil?
end