Class: Chef::DSL::PlatformIntrospection::PlatformDependentValue

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/dsl/platform_introspection.rb

Overview

Implementation class for determining platform dependent values

Instance Method Summary collapse

Constructor Details

#initialize(platform_hash) ⇒ PlatformDependentValue

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: => value_for_that_version

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



48
49
50
51
# File 'lib/chef/dsl/platform_introspection.rb', line 48

def initialize(platform_hash)
  @values = {}
  platform_hash.each { |platforms, value| set(platforms, value) }
end

Instance Method Details

#value_for_node(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/dsl/platform_introspection.rb', line 53

def value_for_node(node)
  platform, version = node[:platform].to_s, node[:platform_version].to_s
  # Check if we match a version constraint via Chef::VersionConstraint::Platform and Chef::Version::Platform
  matched_value = match_versions(node)
  if @values.key?(platform) && @values[platform].key?(version)
    @values[platform][version]
  elsif matched_value
    matched_value
  elsif @values.key?(platform) && @values[platform].key?("default")
    @values[platform]["default"]
  elsif @values.key?("default")
    @values["default"]
  else
    nil
  end
end