Class: Steplib::HashUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/steplib/hash_utils.rb

Class Method Summary collapse

Class Method Details

.check_required_attributes_and_types!(hash_to_check, attribute_type_array) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/steplib/hash_utils.rb', line 16

def check_required_attributes_and_types!(hash_to_check, attribute_type_array)
  attribute_type_array.each do |a_attribute_type_itm|
    attribute_key = a_attribute_type_itm[0]
    attribute_type = a_attribute_type_itm[1]
    #
    attr_value = hash_to_check[attribute_key]
    if attr_value.nil?
      raise "Attribute (#{attribute_key}) not found in hash"
    end
    unless attr_value.is_a? attribute_type
      raise "Attribute (#{attribute_key}) found, but it's type (#{attr_value.class}) doesn't match the required (#{attribute_type})"
    end
  end
  return true
end

.copy_attributes(hsh_copy_to, hsh_copy_from, attr_to_copy_list) ⇒ Object

Copies the listed attributes from the copy_from hash

to the copy_to hash.

Doesn’t do a copy of input hash, will inline-set

the attributes / modify the input hash!


81
82
83
84
85
86
# File 'lib/steplib/hash_utils.rb', line 81

def copy_attributes(hsh_copy_to, hsh_copy_from, attr_to_copy_list)
  attr_to_copy_list.each do |a_attr_to_copy|
    hsh_copy_to[a_attr_to_copy] = hsh_copy_from[a_attr_to_copy]
  end
  return hsh_copy_to
end

.copy_attributes_dcopy(in_hsh_copy_to, hsh_copy_from, attr_to_copy_list) ⇒ Object

Deep-copy version Returns a new hash, the original input hash will be kept unchanged!



89
90
91
92
# File 'lib/steplib/hash_utils.rb', line 89

def copy_attributes_dcopy(in_hsh_copy_to, hsh_copy_from, attr_to_copy_list)
  hsh_copy_to = deep_copy(in_hsh_copy_to)
  return copy_attributes(hsh_copy_to, hsh_copy_from, attr_to_copy_list)
end

.copy_missing_defaults(hsh_copy_to, hsh_copy_from, attr_to_copy_list) ⇒ Object

Copies the listed attributes from the copy_from hash

to the copy_to hash in case the attribute is missing
from copy_to hash.

Doesn’t do a copy of input hash, will inline-set

the attributes / modify the input hash!


61
62
63
64
65
66
67
68
# File 'lib/steplib/hash_utils.rb', line 61

def copy_missing_defaults(hsh_copy_to, hsh_copy_from, attr_to_copy_list)
  attr_to_copy_list.each do |a_attr_to_copy|
    if hsh_copy_to[a_attr_to_copy].nil?
      hsh_copy_to[a_attr_to_copy] = hsh_copy_from[a_attr_to_copy]
    end
  end
  return hsh_copy_to
end

.copy_missing_defaults_dcopy(in_hsh_copy_to, hsh_copy_from, attr_to_copy_list) ⇒ Object

Deep-copy version Returns a new hash, the original input hash will be kept unchanged!



71
72
73
74
# File 'lib/steplib/hash_utils.rb', line 71

def copy_missing_defaults_dcopy(in_hsh_copy_to, hsh_copy_from, attr_to_copy_list)
  hsh_copy_to = deep_copy(in_hsh_copy_to)
  return copy_missing_defaults(hsh_copy_to, hsh_copy_from, attr_to_copy_list)
end

.deep_copy(hsh) ⇒ Object



12
13
14
# File 'lib/steplib/hash_utils.rb', line 12

def deep_copy(hsh)
  return Marshal.load(Marshal.dump(hsh))
end

.set_missing_defaults(hsh, defaults_arr) ⇒ Object

Sets the provided default value for the specified

key if the key is missing.

Defaults_arr is an array of :key=>,:value=> hashes Doesn’t do a copy of input hash, will inline-set

the attributes / modify the input hash!


38
39
40
41
42
43
44
45
46
47
# File 'lib/steplib/hash_utils.rb', line 38

def set_missing_defaults(hsh, defaults_arr)
  defaults_arr.each do |a_def|
    a_def_key = a_def[:key]
    a_def_value = a_def[:value]
    if hsh[a_def_key].nil?
      hsh[a_def_key] = a_def_value
    end
  end
  return hsh
end

.set_missing_defaults_dcopy(in_hsh, defaults_arr) ⇒ Object

Deep-copy version Returns a new hash, the original input hash will be kept unchanged!



50
51
52
53
# File 'lib/steplib/hash_utils.rb', line 50

def set_missing_defaults_dcopy(in_hsh, defaults_arr)
  hsh = deep_copy(in_hsh)
  return set_missing_defaults(hsh, defaults_arr)
end