Class: ShadowFacter::Base

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

Class Method Summary collapse

Class Method Details

.current_key(key) ⇒ Object



113
114
115
# File 'lib/shadow_facter/base.rb', line 113

def current_key(key)
  facter_key(current_namespace, key)
end

.current_namespaceObject



109
110
111
# File 'lib/shadow_facter/base.rb', line 109

def current_namespace
  @current_namespace
end

.fact(key, value = nil, confine_args = {}, &block) ⇒ Object

Define a fact in Facter using a value or block. Can be confined with a hash of Facter keys (namespace_key) and value.

Examples:

fact :tea, "oolong"
fact :tea, "puerh", {:drinks_season => "winter"}
fact(:rand) { rand }


88
89
90
91
92
93
94
95
96
97
# File 'lib/shadow_facter/base.rb', line 88

def fact(key, value=nil, confine_args={}, &block)
  raise "Namespace required!" unless current_namespace
  @namespaces[current_namespace] ||= []
  @namespaces[current_namespace] << key
  block = lambda { value.to_s } unless block_given?
  Facter.add(current_key(key)) do
    confine confine_args unless confine_args.empty?
    setcode block
  end
end

.facter_key(namespace, key) ⇒ Object

Construct a namespaced key for using with Facter.



105
106
107
# File 'lib/shadow_facter/base.rb', line 105

def facter_key(namespace, key)
  (namespace.to_s + "_" + key.to_s).to_sym
end

.facts(namespace) ⇒ Object

Return an instance of the Facts class for the specified namespace.



100
101
102
# File 'lib/shadow_facter/base.rb', line 100

def facts(namespace)
  ShadowFacter::Facts.new(namespace, @namespaces[namespace])
end

.namespace(name) ⇒ Object

Define a namespace.



68
69
70
71
72
73
74
# File 'lib/shadow_facter/base.rb', line 68

def namespace(name)
  @namespaces ||= Hash.new
  raise "Nested namespaces not supported yet!" unless @current_namespace.nil?
  @current_namespace = name
  yield
  @current_namespace = nil
end

.namespacesObject

Return an array of defined namespaces names.



77
78
79
# File 'lib/shadow_facter/base.rb', line 77

def namespaces()
  @namespaces.keys
end