module OasRails::Spec::Hashable

def self.generate_hash(obj)

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

def self.hash_representation_recursive(obj)

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

def hash_key

def hash_key
  Hashable.generate_hash(hash_representation)
end

def hash_representation

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

def public_instance_variables

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