Class: ForemanChef::CachedRunList

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/foreman_chef/cached_run_list.rb

Defined Under Namespace

Classes: Jail

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.item_to_chef(item) ⇒ Object

converts { :type => ‘role’, :name => ‘default’ } to ‘role



63
64
65
66
# File 'app/models/foreman_chef/cached_run_list.rb', line 63

def self.item_to_chef(item)
  item = item.with_indifferent_access
  "#{item[:type]}[#{item[:name]}]"
end

.item_to_form(item) ⇒ Object

converts ‘role’ to { :type => ‘role’, :name => ‘default’ }



57
58
59
60
# File 'app/models/foreman_chef/cached_run_list.rb', line 57

def self.item_to_form(item)
  type, name = item.split('[')
  { :type => type, :name => name.chop! }
end

.parse(run_list_data, run_list_object = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/foreman_chef/cached_run_list.rb', line 11

def self.parse(run_list_data, run_list_object = nil)
  data = parse_data(run_list_data)

  if run_list_object.nil?
    cached_run_list = new(:list => data)
  else
    cached_run_list = run_list_object.clone
    cached_run_list.list = data
  end

  cached_run_list
end

.parse_array(run_list_array) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/foreman_chef/cached_run_list.rb', line 44

def self.parse_array(run_list_array)
  raise ArgumentError, "run_list is #{run_list_array.class}, expected Array" unless run_list_array.is_a?(Array)

  if run_list_array.first.is_a?(Hash)
    run_list_array.map { |item| item_to_chef(item) }
  elsif run_list_array.first.is_a?(String) or run_list_array.empty?
    run_list_array
  else
    raise ArgumentError, "run_list_array Array contains unknown data format of class #{run_list_array.class}"
  end
end

.parse_data(run_list_data) ⇒ Object

return data in format ready for serialization which means Hash like this { :run_list => [ ‘role’, ‘recipe’ ] }



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/foreman_chef/cached_run_list.rb', line 27

def self.parse_data(run_list_data)
  case run_list_data
    when Array
      parse_array(run_list_data)
    when Hash
      run_list_data = run_list_data.with_indifferent_access
      if run_list_data.has_key?(:run_list)
        parse_array(run_list_data.with_indifferent_access[:run_list])
      else
        # from form we get {'0' => { 'name' => ...}, '1' => {...}}
        parse_array(run_list_data.values)
      end
    else
      raise ArgumentError, 'unsupported run_list_data format'
  end
end

Instance Method Details

#as_chef_jsonObject



68
69
70
# File 'app/models/foreman_chef/cached_run_list.rb', line 68

def as_chef_json
  { :run_list => self.list }
end

#as_form_jsonObject



76
77
78
# File 'app/models/foreman_chef/cached_run_list.rb', line 76

def as_form_json
  list.map { |item| self.class.item_to_form(item) }
end

#to_chef_jsonObject



72
73
74
# File 'app/models/foreman_chef/cached_run_list.rb', line 72

def to_chef_json
  as_chef_json.to_json
end

#to_form_jsonObject



80
81
82
# File 'app/models/foreman_chef/cached_run_list.rb', line 80

def to_form_json
  as_form_json.to_json
end