Class: EcomDev::ChefSpec::Helpers::Platform

Inherits:
Object
  • Object
show all
Defined in:
lib/ecomdev/chefspec/helpers/platform.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, path = nil) ⇒ Platform

Returns a new instance of Platform.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 6

def initialize(file = nil, path = nil)
  @os = {}
  @family = {}

  path = self.class.platform_path if path.nil?
  file = self.class.platform_file if file.nil?

  json_path = File.join(path, file)

  if File.readable?(json_path)
    load_json(File.read(json_path))
  end
end

Class Method Details

.platform_fileObject



175
176
177
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 175

def platform_file
  @platform_file ||= 'platform.json'
end

.platform_file=(value) ⇒ Object



179
180
181
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 179

def platform_file=(value)
  @platform_file = value
end

.platform_pathObject



157
158
159
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 157

def platform_path
  @platform_path ||= RSpec.configuration.default_path
end

.platform_path=(value) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 161

def platform_path=(value)
  unless value.nil?
    value = Pathname.new value

    if value.relative? && value.to_path[0] != '~'
      value = Pathname.new File.join(RSpec.configuration.default_path, value.to_path)
    end

    value = value.to_path
  end

  @platform_path = value
end

Instance Method Details

#filter(*conditions) ⇒ Object

Returns list of available os versions filtered by conditions

Each condition is treated as OR operation If no condition is specified all items are listed If TrueClass is supplied as one of the arguments it filters only last versions



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 53

def filter(*conditions)
  latest = !conditions.select {|item| item === true }.empty?
  filters = translate_conditions(conditions)
  items = list(latest)
  unless filters.empty?
    items = items.select do |item|
      !filters.select {|filter| match_filter(filter, item) }.empty?
    end

  end
  items
end

#list(latest = false) ⇒ Array<Hash{Symbol => String, Symbol}>

Returns list of available os versions

Parameters:

  • latest (TrueClass, FalseClass) (defaults to: false)

    specify if would like to receive only latest

Returns:

  • (Array<Hash{Symbol => String, Symbol}>)

    list of os versions in view of hash



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 144

def list(latest = false)
  result = []
  @os.map do |os, versions|
    unless latest
      versions.each { |version| result << {os: os, version: version}}
    else
      result << {os: os, version: versions.last} if versions.length > 0
    end
  end
  result
end

#load_json(json_str) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ecomdev/chefspec/helpers/platform.rb', line 20

def load_json(json_str)
  json = JSON.load(json_str)
  if json.key?('os') && json['os'].is_a?(Hash)
    json['os'].each_pair do |os, version|
      @os[os.to_sym] ||= Array.new
      unless version.is_a?(Array)
        version = [version]
      end
      version.flatten.map {|v| v.to_s }.each do |v|
        @os[os.to_sym] << v unless @os[os.to_sym].include?(v)
      end
    end
  end

  if json.key?('family') && json['family'].is_a?(Hash)
    json['family'].each_pair do |family, os|
      @family[family.to_sym] ||= Array.new
      unless os.is_a?(Array)
        os = [os]
      end
      os.flatten.map { |v| v.to_sym }.select { |v| @os.key?(v) }.each do |v|
        @family[family.to_sym] << v unless @family[family.to_sym].include?(v)
      end
    end
  end
end