lib/atd/builtin_class_modifications.rb in atd-0.4.0 vs lib/atd/builtin_class_modifications.rb in atd-0.5.0

- old
+ new

@@ -1,38 +1,45 @@ -# @!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 +module ATD::Refinements + # @!visibility private + refine Hash do + # 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 - 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) + def include_in_key?(search) + each do |key, val| + return val if key.is_a?(Array) && key.include?(search) + end 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 + # This method only exists for the test suite, specifically {ATDTest#test_route_creation}. + # @!visibility private + refine Object do + # Returns the instance variables of a class + def class_instance_variables + instance_variables.map { |var| [var, instance_variable_get(var)] }.to_h + end end - # Returns the instance variables of a class - def class_instance_variables - instance_variables.map { |var| [var, instance_variable_get(var)] }.to_h + refine Array do + def where(criteria) + select do |element| + criteria.all? do |criterion, expected_value| + Array(element.public_send(criterion)).include?(expected_value) + end + end + end end end