Sha256: 2ac72d48824bfceb34e20f44282450c8277b4a974cf7527a0fb4733da39f3298

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

# @!visibility private
class Hash
  # Not only merges two hashes, but also merges the hashes that may be nested in.
  #
  # For example:
  #   {a: {b: "c"}}
  # Is a nested hash
  def deep_merge(second)
    merger = proc do |_, v1, v2|
      if v1.is_a?(Hash) && v2.is_a?(Hash) then v1.merge(v2, &merger)
      elsif v1.is_a?(Array) && v2.is_a?(Array) then v1 | v2
      elsif [:undefined, nil, :nil].include?(v2) then v1
      else v2
      end
    end
    merge(second.to_h, &merger)
  end

  def include_in_key?(search)
    each do |key, val|
      return val if key.is_a?(Array) && key.include?(search)
    end
  end
end

# This method only exists for the test suite, specifically {ATDTest#test_route_creation}.
# @!visibility private
class Object
  # Checks if two objects are instances of the same class and that they have the same instance variables
  def same_properties_as?(other_class)
    other_class.class == self.class && class_instance_variables == other_class.class_instance_variables
  end

  # Returns the instance variables of a class
  def class_instance_variables
    instance_variables.map { |var| [var, instance_variable_get(var)] }.to_h
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
atd-0.4.0 lib/atd/builtin_class_modifications.rb
atd-0.3.2 lib/atd/builtin_class_modifications.rb
atd-0.3.1 lib/atd/builtin_class_modifications.rb
atd-0.2.0.TEST.GITLAB1 lib/atd/builtin_class_modifications.rb
atd-0.1.0.TEST.GITLAB1 lib/atd/builtin_class_modifications.rb