Sha256: cea28b49e93bf281a01962130c136d18c424ee9eef49c88d555ce0cd4879b124

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

require 'diffy'

module Simnos
  class Utils
    class << self
      def normalize_hash(hash)
        hash.dup.each do |k, v|
          if v.kind_of?(Array)
            if v.first.kind_of?(Hash)
              hash[k] = v.map { |o| normalize_hash(o) }
            elsif v.first.respond_to?(:to_h)
              hash[k] = v.map { |o| normalize_hash(o.to_h) }
            else
              v.sort!
            end
          elsif v.respond_to?(:to_h)
            hash[k] = normalize_hash(v.to_h)
          end
        end
        sort_keys(hash)
      end

      def sort_keys(hash)
        hash = Hash[hash.sort]
        hash.each do |k, v|
          if v.kind_of?(Array)
            if v.first.kind_of?(Hash)
              hash[k] = v.map { |h| sort_keys(h) }
            end
          elsif v.kind_of?(Hash)
            hash[k] = sort_keys(v)
          end
        end
        hash
      end

      def diff(obj1, obj2, options = {})
        diffy = Diffy::Diff.new(
          obj1.pretty_inspect,
          obj2.pretty_inspect,
          :diff => '-u'
        )

        out = diffy.to_s(options[:color] ? :color : :text).gsub(/\s+\z/m, '')
        out.gsub!(/^/, options[:indent]) if options[:indent]
        out
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simnos-0.1.3.beta1 lib/simnos/utils.rb
simnos-0.1.2 lib/simnos/utils.rb
simnos-0.1.1 lib/simnos/utils.rb
simnos-0.1.1.beta2 lib/simnos/utils.rb
simnos-0.1.1.beta1 lib/simnos/utils.rb