class Octofacts::Facts

Attributes

facts[W]

Public Class Methods

new(args = {}) click to toggle source

Constructor.

backend - An Octofacts::Backends object (preferred) options - Additional options (e.g., downcase keys, symbolize keys, etc.)

# File lib/octofacts/facts.rb, line 11
def initialize(args = {})
  @backend = args.fetch(:backend)
  @facts_manipulated = false

  options = args.fetch(:options, {})
  @downcase_keys = args.fetch(:downcase_keys, options.fetch(:downcase_keys, true))
end

Public Instance Methods

facts() click to toggle source

To fact hash. (This method is intended to be called by developers.)

This loads the fact file and downcases, symbolizes, and otherwise manipulates the keys. This is very similar to 'to_hash' except that it returns symbolized keys. The output is suitable for consumption by rspec-puppet (note that rspec-puppet will de-symbolize all the keys in the hash object though).

# File lib/octofacts/facts.rb, line 37
def facts
  @facts ||= begin
    f = @backend.facts
    downcase_keys!(f) if @downcase_keys
    symbolize_keys!(f)
    f
  end
end
method_missing(name, *args, &block) click to toggle source

Missing method - this is used to dispatch to manipulators or to call a Hash method in the facts.

Try calling a Manipulator method, delegate to the facts hash or else error out.

Returns this object (so that calls to manipulators can be chained).

# File lib/octofacts/facts.rb, line 78
def method_missing(name, *args, &block)
  if Octofacts::Manipulators.run(self, name, *args, &block)
    @facts_manipulated = true
    return self
  end

  if facts.respond_to?(name, false)
    if args[0].is_a?(String) || args[0].is_a?(Symbol)
      args[0] = string_or_symbolized_key(args[0])
    end
    return facts.send(name, *args)
  end

  raise NameError, "Unknown method '#{name}' in #{self.class}"
end
prefer(*args) click to toggle source
# File lib/octofacts/facts.rb, line 65
def prefer(*args)
  if @facts_manipulated
    raise Octofacts::Errors::OperationNotPermitted, "Cannot call prefer() after backend facts have been manipulated"
  end
  @backend.prefer(*args)
  self
end
reject(*args) click to toggle source
# File lib/octofacts/facts.rb, line 57
def reject(*args)
  if @facts_manipulated
    raise Octofacts::Errors::OperationNotPermitted, "Cannot call reject() after backend facts have been manipulated"
  end
  @backend.reject(*args)
  self
end
respond_to?(method, include_all = false) click to toggle source
Calls superclass method
# File lib/octofacts/facts.rb, line 94
def respond_to?(method, include_all = false)
  camelized_name = (method.to_s).split("_").collect(&:capitalize).join
  super || Kernel.const_get("Octofacts::Manipulators::#{camelized_name}")
rescue NameError
  return facts.respond_to?(method, include_all)
end
select(*args) click to toggle source

Calls to backend methods.

These calls are passed through directly to backend methods.

# File lib/octofacts/facts.rb, line 49
def select(*args)
  if @facts_manipulated
    raise Octofacts::Errors::OperationNotPermitted, "Cannot call select() after backend facts have been manipulated"
  end
  @backend.select(*args)
  self
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

To hash. (This method is intended to be called by rspec-puppet.)

This loads the fact file and downcases, desymbolizes, and otherwise manipulates the keys. The output is suitable for consumption by rspec-puppet.

# File lib/octofacts/facts.rb, line 23
def to_hash
  f = facts
  downcase_keys!(f) if @downcase_keys
  desymbolize_keys!(f)
  f
end
Also aliased as: to_h

Private Instance Methods

desymbolize_keys!(input) click to toggle source
# File lib/octofacts/facts.rb, line 111
def desymbolize_keys!(input)
  Octofacts::Util::Keys.desymbolize_keys!(input)
end
downcase_keys!(input) click to toggle source
# File lib/octofacts/facts.rb, line 103
def downcase_keys!(input)
  Octofacts::Util::Keys.downcase_keys!(input)
end
string_or_symbolized_key(input) click to toggle source
# File lib/octofacts/facts.rb, line 115
def string_or_symbolized_key(input)
  return input.to_s if facts.key?(input.to_s)
  return input.to_sym if facts.key?(input.to_sym)
  input
end
symbolize_keys!(input) click to toggle source
# File lib/octofacts/facts.rb, line 107
def symbolize_keys!(input)
  Octofacts::Util::Keys.symbolize_keys!(input)
end