Class: Chef::Resource::PlatformMap

Inherits:
Object
  • Object
show all
Includes:
Mixin::ConvertToClassName, Mixin::ParamsValidate
Defined in:
lib/chef/resource_platform_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Constructor Details

#initialize(map = {:default => {}}) ⇒ PlatformMap

Returns a new instance of PlatformMap.



31
32
33
# File 'lib/chef/resource_platform_map.rb', line 31

def initialize(map={:default => {}})
  @map = map
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



29
30
31
# File 'lib/chef/resource_platform_map.rb', line 29

def map
  @map
end

Instance Method Details

#filter(platform, version) ⇒ Object



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

def filter(platform, version)
  resource_map = map[:default].clone
  platform_sym = platform
  if platform.kind_of?(String)
    platform.downcase!
    platform.gsub!(/\s/, "_")
    platform_sym = platform.to_sym
  end

  if map.has_key?(platform_sym)
    if map[platform_sym].has_key?(version)
      if map[platform_sym].has_key?(:default)
        resource_map.merge!(map[platform_sym][:default])
      end
      resource_map.merge!(map[platform_sym][version])
    elsif map[platform_sym].has_key?(:default)
      resource_map.merge!(map[platform_sym][:default])
    end
  end
  resource_map
end

#get(short_name, platform = nil, version = nil) ⇒ Object

Raises:

  • (NameError)


122
123
124
125
126
127
128
129
# File 'lib/chef/resource_platform_map.rb', line 122

def get(short_name, platform=nil, version=nil)
  resource_klass = platform_resource(short_name, platform, version) ||
                   resource_matching_short_name(short_name)

  raise NameError, "Cannot find a resource for #{short_name} on #{platform} version #{version}" if resource_klass.nil?

  resource_klass
end

#set(args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef/resource_platform_map.rb', line 57

def set(args)
  validate(
    args,
    {
      :platform => {
        :kind_of => Symbol,
        :required => false
      },
      :version => {
        :kind_of => String,
        :required => false
      },
      :short_name => {
        :kind_of => Symbol,
        :required => true
      },
      :resource => {
        :kind_of => [ String, Symbol, Class ],
        :required => true
      }
    }
  )
  if args.has_key?(:platform)
    if args.has_key?(:version)
      if map.has_key?(args[:platform])
        if map[args[:platform]].has_key?(args[:version])
          map[args[:platform]][args[:version]][args[:short_name].to_sym] = args[:resource]
        else
          map[args[:platform]][args[:version]] = {
            args[:short_name].to_sym => args[:resource]
          }
        end
      else
        map[args[:platform]] = {
          args[:version] => {
            args[:short_name].to_sym => args[:resource]
          }
        }
      end
    else
      if map.has_key?(args[:platform])
        if map[args[:platform]].has_key?(:default)
          map[args[:platform]][:default][args[:short_name].to_sym] = args[:resource]
        else
          map[args[:platform]] = { :default => { args[:short_name].to_sym => args[:resource] } }
        end
      else
        map[args[:platform]] = {
          :default => {
            args[:short_name].to_sym => args[:resource]
          }
        }
      end
    end
  else
    if map.has_key?(:default)
      map[:default][args[:short_name].to_sym] = args[:resource]
    else
      map[:default] = {
        args[:short_name].to_sym => args[:resource]
      }
    end
  end
end