Module: OasRails::Spec::Hashable

Included in:
Parameter, RequestBody, Response
Defined in:
lib/oas_rails/spec/hashable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_hash(obj) ⇒ Object



14
15
16
# File 'lib/oas_rails/spec/hashable.rb', line 14

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

.hash_representation_recursive(obj) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/oas_rails/spec/hashable.rb', line 25

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

Instance Method Details

#hash_keyObject



6
7
8
# File 'lib/oas_rails/spec/hashable.rb', line 6

def hash_key
  Hashable.generate_hash(hash_representation)
end

#hash_representationObject



10
11
12
# File 'lib/oas_rails/spec/hashable.rb', line 10

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

#public_instance_variablesObject



18
19
20
21
22
23
# File 'lib/oas_rails/spec/hashable.rb', line 18

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