class Chef::DSL::PlatformIntrospection::PlatformDependentValue

Implementation class for determining platform dependent values

Public Class Methods

new(platform_hash) click to toggle source

Create a platform dependent value object.

Arguments

platform_hash (Hash) a hash of the same structure as Chef::Platform, like this:

{
  :debian => {:default => 'the value for all debian'}
  [:centos, :redhat, :fedora] => {:default => "value for all EL variants"}
  :ubuntu => { :default => "default for ubuntu", '10.04' => "value for 10.04 only"},
  :default => "the default when nothing else matches"
}
  • platforms can be specified as Symbols or Strings

  • multiple platforms can be grouped by using an Array as the key

  • values for platforms need to be Hashes of the form: {platform_version => value_for_that_version}

  • the exception to the above is the default value, which is given as :default => default_value

# File lib/chef/dsl/platform_introspection.rb, line 46
def initialize(platform_hash)
  @values = {}
  platform_hash.each { |platforms, value| set(platforms, value)}
end

Public Instance Methods

value_for_node(node) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 51
def value_for_node(node)
  platform, version = node[:platform].to_s, node[:platform_version].to_s
  if @values.key?(platform) && @values[platform].key?(version)
    @values[platform][version]
  elsif @values.key?(platform) && @values[platform].key?("default")
    @values[platform]["default"]
  elsif @values.key?("default")
    @values["default"]
  else
    nil
  end
end

Private Instance Methods

assert_valid_platform_values!(platforms, value) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 86
def assert_valid_platform_values!(platforms, value)
  unless value.kind_of?(Hash)
    msg = "platform dependent values must be specified in the format :platform => {:version => value} "
    msg << "you gave a value #{value.inspect} for platform(s) #{platforms}"
    raise ArgumentError, msg
  end
end
normalize_keys(hash) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 76
def normalize_keys(hash)
  hash.inject({}) do |h, key_value|
    keys, value = *key_value
    Array(keys).each do |key|
      h[key.to_s] = value
    end
    h
  end
end
set(platforms, value) click to toggle source
# File lib/chef/dsl/platform_introspection.rb, line 66
def set(platforms, value)
  if platforms.to_s == 'default'
    @values["default"] = value
  else
    assert_valid_platform_values!(platforms, value)
    Array(platforms).each { |platform| @values[platform.to_s] = normalize_keys(value)}
    value
  end
end