Module: Chef::Mixin::Language

Included in:
Recipe, Resource
Defined in:
lib/chef/mixin/language.rb

Instance Method Summary collapse

Instance Method Details

#platform?(*args) ⇒ Boolean

Given a list of platforms, returns true if the current recipe is being run on a node with that platform, false otherwise.

Parameters

args

A list of platforms

Returns

true

If the current platform is in the list

false

If the current platform is not in the list

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'lib/chef/mixin/language.rb', line 67

def platform?(*args)
  has_platform = false
  
  args.flatten.each do |platform|
    has_platform = true if platform == @node[:platform]
  end
  
  has_platform
end

#value_for_platform(platform_hash) ⇒ Object

Given a hash similar to the one we use for Platforms, select a value from the hash. Supports per platform defaults, along with a single base default. Arrays may be passed as hash keys and will be expanded.

Parameters

platform_hash

A platform-style hash.

Returns

value

Whatever the most specific value of the hash is.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/mixin/language.rb', line 32

def value_for_platform(platform_hash)
  result = nil
  
  platform_hash.each_pair do |key, value|
    if key.is_a?(Array)
      key.each { |array_key| platform_hash[array_key] = value }
      platform_hash.delete(key)
    end
  end
  if platform_hash.has_key?(@node[:platform])
    if platform_hash[@node[:platform]].has_key?(@node[:platform_version])
      result = platform_hash[@node[:platform]][@node[:platform_version]]
    elsif platform_hash[@node[:platform]].has_key?("default")
      result = platform_hash[@node[:platform]]["default"]
    end
  end
  
  unless result
    if platform_hash.has_key?("default")
      result = platform_hash["default"]
    end
  end  
  
  result
end