class Hash

Public Instance Methods

_find_depth(maxx_depth , current_depth) click to toggle source
# File lib/nested_hash_helper.rb, line 37
def _find_depth(maxx_depth , current_depth)
        current_class = self.class
        self.each do |current_keys , current_value |
                if current_value.is_a?(current_class)
                        maxx_depth  = current_value._find_depth(maxx_depth , current_depth + 1)
                end
        end
        maxx_depth = (maxx_depth > current_depth)? maxx_depth : current_depth
end
deep_delete(key) click to toggle source
# File lib/nested_hash_helper.rb, line 111
def deep_delete(key)
  current_class = self.class
  self.each do |current_keys , current_value|
    if current_keys == key
      self.delete(current_keys)
    elsif current_value.is_a?(current_class)
      current_value.deep_delete(key)
    end
  end
end
deep_delete_empty() click to toggle source
# File lib/nested_hash_helper.rb, line 18
def deep_delete_empty
        current_class = self.class
        self.each do |current_keys , current_value|
       if current_value.nil? || current_value.empty?
          self.delete(current_keys)
          next
       end      
       if current_value.is_a?(current_class)
            current_value.deep_delete_empty
       end      
        end
end
deep_except(*excluded_keys) click to toggle source

Your code goes here…

# File lib/nested_hash_helper.rb, line 5
def deep_except(*excluded_keys)
  current_class = self.class
  self.each do |current_keys , current_value|
        if excluded_keys.include?(current_keys)
                 self.delete(current_keys)
                 next
                end
                if current_value.is_a?(current_class)
                        current_value.deep_except(*excluded_keys)
            end
        end
        end
find_all_values(key) click to toggle source
# File lib/nested_hash_helper.rb, line 98
def find_all_values(key)
   current_class = self.class
   values = []
   self.each do |current_keys , current_value|
      if current_value.is_a?(current_class)
         values += current_value.find_all_values(key)
        elsif current_keys == key
         values.push(current_value)
      end
   end
 values
end
find_deep_intersection(compare_hash) click to toggle source
# File lib/nested_hash_helper.rb, line 47
def find_deep_intersection(compare_hash)
        current_class = self.class
        final_hash = current_class.new
    self.each do |current_keys , current_value |
        if compare_hash.has_key?(current_keys)
                if current_value.is_a?(current_class) && compare_hash.fetch(current_keys).is_a?(current_class)
               final_hash[current_keys] = current_value.find_deep_intersection(compare_hash.fetch(current_keys))
        elsif !current_value.is_a?(current_class) && !compare_hash.fetch(current_keys).is_a?(current_class) && (current_value == compare_hash.fetch(current_keys) )
              final_hash[current_keys] = current_value
        end
        end
    end
    final_hash
end
find_deep_keys(value) click to toggle source
# File lib/nested_hash_helper.rb, line 62
def find_deep_keys(value)
  current_class = self.class
  deep_keys = []
    self.each do |current_keys , current_value|
       if !current_value.is_a?(current_class) && current_value == value 
             deep_keys = deep_keys.push(current_keys)
        elsif current_value.is_a?(current_class)
          future_deep_keys = current_value.find_deep_keys(value)
          if future_deep_keys.size >= 1
             deep_keys.push(current_keys)
             deep_keys += future_deep_keys
             return deep_keys
          end
       end
    end
    deep_keys
end
find_depth() click to toggle source
# File lib/nested_hash_helper.rb, line 31
def find_depth
          maxx_depth = 1
          current_depth = 1
          self._find_depth(maxx_depth , current_depth)
end
hash_to_array() click to toggle source
# File lib/nested_hash_helper.rb, line 80
def hash_to_array
  current_class = self.class
  final_array = []
    self.each do | current_keys , current_value | 
      temp_array = []
      if current_value.is_a?(current_class)
        temp_array.push(current_keys)
        temp_array += current_value.hash_to_array
        final_array.push(temp_array)
      else
        temp_array.push(current_keys)
        temp_array.push(current_value)
        final_array.push(temp_array)
      end
    end
    final_array
end