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.2.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.2.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.1.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.1.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.5 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.4 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.3 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.2 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.4.4 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.4.3 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.4.2 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.4.1 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.0.pre.beta.4 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-6.0.0.pre.beta.3 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.4.0 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.3.3 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.3.2 app/helpers/forest_liana/is_same_data_structure_helper.rb
forest_liana-5.3.1 app/helpers/forest_liana/is_same_data_structure_helper.rb