Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb
Overview
Add some methods from Rails to aid in changeing keys
Instance Method Summary collapse
- #_deep_transform_keys_in_object(object, &block) ⇒ Object
- #deep_diff(value2) ⇒ Object
- #deep_transform_keys(&block) ⇒ Object
- #only_change_diff(value2) ⇒ Object
- #stringify_keys ⇒ Object
- #to_oci ⇒ Object
- #to_puppet ⇒ Object
Instance Method Details
#_deep_transform_keys_in_object(object, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb', line 36 def _deep_transform_keys_in_object(object, &block) case object when Hash object.each_with_object({}) do |(key, value), result| # We don't want to transform the content of the tags. Leave them as they are result[yield(key)] = if [:defined_tags, :freeform_tags, :definedTags, :freeformTags, :metadata, :extended_metadata].include?(key.to_sym) value else _deep_transform_keys_in_object(value, &block) end end when Array object.map { |e| _deep_transform_keys_in_object(e, &block) } else object end end |
#deep_diff(value2) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb', line 58 def deep_diff(value2) value1 = self (value1.keys | value2.keys).each_with_object({}) do |key, diff| next unless value1[key] != value2[key] diff[key] = if value1[key].respond_to?(:deep_diff) && value2[key].respond_to?(:deep_diff) value1[key].deep_diff(value2[key]) else [value1[key], value2[key]] end end end |
#deep_transform_keys(&block) ⇒ Object
54 55 56 |
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb', line 54 def deep_transform_keys(&block) _deep_transform_keys_in_object(self, &block) end |
#only_change_diff(value2) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb', line 71 def only_change_diff(value2) value1 = self value1.keys.each_with_object({}) do |key, diff| next unless value1[key] != value2[key] diff[key] = if value1[key].respond_to?(:only_change_diff) && value2[key].respond_to?(:only_change_diff) value1[key].only_change_diff(value2[key]) else [value1[key], value2[key]] end end end |
#stringify_keys ⇒ Object
18 19 20 |
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb', line 18 def stringify_keys deep_transform_keys(&:to_s) end |
#to_oci ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb', line 22 def to_oci # # The keys sometimes use Gbs and Mbs, we don't want to underscore this. # So we first make these lowercase. # deep_transform_keys do |key| key.to_s. gsub(/_gb/, '_g_b'). gsub('_gbs', '_g_bs'). gsub('_mbs', '_m_bs'). split('_').collect(&:capitalize).join.downcase_first_letter end end |
#to_puppet ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/hash.rb', line 5 def to_puppet # # The keys sometimes use Gbs and Mbs, we don't want to underscore this. # So we first make these lowercase. # deep_transform_keys do |key| key.to_s. gsub('GBs', 'Gbs'). gsub('GBs', 'Gbs'). underscore end end |