Class: Steplib::HashUtils
- Inherits:
-
Object
- Object
- Steplib::HashUtils
- Defined in:
- lib/steplib/hash_utils.rb
Class Method Summary collapse
- .check_required_attributes_and_types!(hash_to_check, attribute_type_array) ⇒ Object
-
.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.
-
.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!.
-
.copy_missing_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 in case the attribute is missing from copy_to hash.
-
.copy_missing_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!.
- .deep_copy(hsh) ⇒ Object
-
.set_missing_defaults(hsh, defaults_arr) ⇒ Object
Sets the provided default value for the specified key if the key is missing.
-
.set_missing_defaults_dcopy(in_hsh, defaults_arr) ⇒ Object
Deep-copy version Returns a new hash, the original input hash will be kept unchanged!.
- .whitelist(hsh, whitelist) ⇒ Object
Class Method Details
.check_required_attributes_and_types!(hash_to_check, attribute_type_array) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/steplib/hash_utils.rb', line 25 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!
90 91 92 93 94 95 |
# File 'lib/steplib/hash_utils.rb', line 90 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!
98 99 100 101 |
# File 'lib/steplib/hash_utils.rb', line 98 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_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 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!
70 71 72 73 74 75 76 77 |
# File 'lib/steplib/hash_utils.rb', line 70 def copy_missing_attributes(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_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!
80 81 82 83 |
# File 'lib/steplib/hash_utils.rb', line 80 def copy_missing_attributes_dcopy(in_hsh_copy_to, hsh_copy_from, attr_to_copy_list) hsh_copy_to = deep_copy(in_hsh_copy_to) return copy_missing_attributes(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!
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/steplib/hash_utils.rb', line 47 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!
59 60 61 62 |
# File 'lib/steplib/hash_utils.rb', line 59 def set_missing_defaults_dcopy(in_hsh, defaults_arr) hsh = deep_copy(in_hsh) return set_missing_defaults(hsh, defaults_arr) end |
.whitelist(hsh, whitelist) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/steplib/hash_utils.rb', line 16 def whitelist(hsh, whitelist) return nil if hsh.nil? res_hash = {} whitelist.each do |whiteitm| res_hash[whiteitm] = hsh[whiteitm] end return res_hash end |