Sha256: 9d3ad39150cb0c59bfc7b69c73d5f138e6835266cfcace1c2f6700a00b5fbbea

Contents?: true

Size: 973 Bytes

Versions: 15

Compression:

Stored size: 973 Bytes

Contents

module OasRails
  module Spec
    require 'digest'

    module Hashable
      def hash_key
        Hashable.generate_hash(hash_representation)
      end

      def hash_representation
        public_instance_variables.sort.to_h { |var| [var, instance_variable_get(var)] }
      end

      def self.generate_hash(obj)
        Digest::MD5.hexdigest(hash_representation_recursive(obj).to_s)
      end

      def public_instance_variables
        instance_variables.select do |var|
          method_name = var.to_s.delete('@')
          respond_to?(method_name) || respond_to?("#{method_name}=")
        end
      end

      def self.hash_representation_recursive(obj)
        case obj
        when Hash
          obj.transform_values { |v| hash_representation_recursive(v) }
        when Array
          obj.map { |v| hash_representation_recursive(v) }
        when Hashable
          obj.hash_representation
        else
          obj
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
oas_rails-0.9.0 lib/oas_rails/spec/hashable.rb
oas_rails-0.8.4 lib/oas_rails/spec/hashable.rb
oas_rails-0.8.3 lib/oas_rails/spec/hashable.rb
oas_rails-0.8.2 lib/oas_rails/spec/hashable.rb
oas_rails-0.8.1 lib/oas_rails/spec/hashable.rb
oas_rails-0.8.0 lib/oas_rails/spec/hashable.rb
oas_rails-0.7.0 lib/oas_rails/spec/hashable.rb
oas_rails-0.6.0 lib/oas_rails/spec/hashable.rb
oas_rails-0.5.0 lib/oas_rails/spec/hashable.rb
oas_rails-0.4.5 lib/oas_rails/spec/hashable.rb
oas_rails-0.4.4 lib/oas_rails/spec/hashable.rb
oas_rails-0.4.3 lib/oas_rails/spec/hashable.rb
oas_rails-0.4.2 lib/oas_rails/spec/hashable.rb
oas_rails-0.4.1 lib/oas_rails/spec/hashable.rb
oas_rails-0.4.0 lib/oas_rails/spec/hashable.rb