Module: ForemanFogProxmox::HashCollection

Defined in:
lib/foreman_fog_proxmox/hash_collection.rb

Class Method Summary collapse

Class Method Details

.add_and_format_element(dest, dest_key, origin, origin_key, format = :to_s) ⇒ Object



24
25
26
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 24

def self.add_and_format_element(dest, dest_key, origin, origin_key, format = :to_s)
  dest[dest_key] = origin[origin_key].send(format) if origin && origin[origin_key]
end

.equals?(h1, h2) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 48

def self.equals?(h1, h2)
  new_h1 = new_hash_transform_values(h1, :to_s)
  new_sorted_h1 = new_h1.sort_by { |key, _value| key }.to_h
  new_h2 = new_hash_transform_values(h2, :to_s)
  new_sorted_h2 = new_h2.sort_by { |key, _value| key }.to_h
  new_sorted_h1.keys == new_sorted_h2.keys && new_sorted_h1.values == new_sorted_h2.values
end

.new_hash_reject_empty_values(h) ⇒ Object



40
41
42
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 40

def self.new_hash_reject_empty_values(h)
  h.reject { |_key, value| ForemanFogProxmox::Value.empty?(value) }
end

.new_hash_reject_keys(h, excluded_keys) ⇒ Object



36
37
38
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 36

def self.new_hash_reject_keys(h, excluded_keys)
  h.reject { |key, _value| excluded_keys.include?(key) }
end

.new_hash_transform_values(h, transformation) ⇒ Object



44
45
46
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 44

def self.new_hash_transform_values(h, transformation)
  h.transform_values { |value| value.send(transformation) }
end

.remove_empty_values(h) ⇒ Object



28
29
30
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 28

def self.remove_empty_values(h)
  h.delete_if { |_key, value| ForemanFogProxmox::Value.empty?(value) }
end

.remove_keys(h, excluded_keys) ⇒ Object



32
33
34
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 32

def self.remove_keys(h, excluded_keys)
  h.delete_if { |key, _value| excluded_keys.include?(key) }
end

.stringify_keys(old_h) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/foreman_fog_proxmox/hash_collection.rb', line 56

def self.stringify_keys(old_h)
  h = old_h.map do |k, v|
    v_str = if v.instance_of? Hash
              v.stringify_keys
            else
              v
            end

    [k.to_s, v_str]
  end
  Hash[h]
end