Sha256: 898a5fcd7d157469c9516f54abba52248617a6dca6c57e680edaf6dc948fb41c

Contents?: true

Size: 964 Bytes

Versions: 41

Compression:

Stored size: 964 Bytes

Contents

require 'set'

module ForestLiana
  module IsSameDataStructureHelper
    class Analyser
      def initialize(object, other, deep = 0)
        @object = object
        @other = other
        @deep = deep
      end

      def are_objects(object, other)
        object && other && object.is_a?(Hash) && other.is_a?(Hash)
      end

      def check_keys(object, other, step = 0)
        unless are_objects(object, other)
          return false
        end

        object_keys = object.keys
        other_keys = other.keys

        if object_keys.length != other_keys.length
          return false
        end

        object_keys_set = object_keys.to_set
        other_keys.each { |key|
          if !object_keys_set.member?(key) || (step + 1 <= @deep && !check_keys(object[key], other[key], step + 1))
            return false
          end
        }

        return true
      end

      def perform
        check_keys(@object, @other)
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
forest_liana-6.6.3 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.6.2 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.6.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.6.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.5.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.5.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.4.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-7.0.0.beta.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.4.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.8 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.7 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.6 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.5 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.4 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.3 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.2 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.3.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.2.3 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.2.2 app/helpers/forest_liana/is_same_data_structure_helper.rb