Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/opennebula/flow/validator.rb
Overview
Overwriting hash class with new methods
Class Method Summary collapse
-
.to_raw(content_hash) ⇒ Hash, OpenNebula::Error
Converts a hash to a raw String in the form KEY = VAL.
Instance Method Summary collapse
-
#deep_merge(other_hash, merge_array = true) ⇒ Hash
Returns a new hash containing the contents of other_hash and the contents of self.
Class Method Details
.to_raw(content_hash) ⇒ Hash, OpenNebula::Error
Converts a hash to a raw String in the form KEY = VAL
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/opennebula/flow/validator.rb', line 65 def to_raw(content_hash) return '' if content_hash.nil? || content_hash.empty? content = '' content_hash.each do |key, value| case value when Hash sub_content = to_raw(value) content += "#{key} = [\n" content_lines = sub_content.split("\n") content_lines.each_with_index do |line, index| content += line.to_s content += ",\n" unless index == content_lines.size - 1 end content += "\n]\n" when Array value.each do |element| content += to_raw({ key.to_s => element }) end else content += "#{key} = \"#{value.to_s.gsub('"', '\"')}\"\n" end end content rescue StandardError => e return OpenNebula::Error.new("Error wrapping the hash: #{e.}") end |
Instance Method Details
#deep_merge(other_hash, merge_array = true) ⇒ Hash
Returns a new hash containing the contents of other_hash and the contents of self. If the value for entries with duplicate keys is a Hash, it will be merged recursively, otherwise it will be that of other_hash.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/opennebula/flow/validator.rb', line 37 def deep_merge(other_hash, merge_array = true) target = clone other_hash.each do |key, value| current = target[key] target[key] = if value.is_a?(Hash) && current.is_a?(Hash) current.deep_merge(value, merge_array) elsif value.is_a?(Array) && current.is_a?(Array) && merge_array merged = current + value merged.all? {|el| el.is_a?(Hash) } ? merged.uniq : merged else value end end target end |