class Locale::HashCompare

Compares generic values and returns rich diff

Public Class Methods

new(hash, other_hash) click to toggle source
# File lib/locales_toys/template/locale.rb, line 22
def initialize(hash, other_hash)
        @hash = hash
        @other_hash = other_hash
end

Public Instance Methods

different_keys() click to toggle source
# File lib/locales_toys/template/locale.rb, line 27
def different_keys
        @hash.each_with_object({}) do |(key, value), result|
                difference = generic_difference(value, @other_hash[key])
                next if difference.nil? || difference.empty?

                result[key] = difference
        end
end

Private Instance Methods

differences_in_array(array, other_array) click to toggle source
# File lib/locales_toys/template/locale.rb, line 51
def differences_in_array(array, other_array)
        return array if array.size != other_array.size

        array.zip(other_array).map do |object, other_object|
                next if !object.is_a?(Hash) || !other_object.is_a?(Hash)

                difference = self.class.new(object, other_object).different_keys
                difference unless difference.empty?
        end.compact
end
generic_difference(value, other_value) click to toggle source
# File lib/locales_toys/template/locale.rb, line 38
def generic_difference(value, other_value)
        return value if value.class != other_value.class

        case value
        when Hash
                self.class.new(value, other_value).different_keys
        when Array
                differences_in_array(value, other_value)
        when nil
                value
        end
end