Module: OasCore::Spec::Hashable

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_hash(obj) ⇒ Object



16
17
18
# File 'lib/oas_core/spec/hashable.rb', line 16

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

.hash_representation_recursive(obj) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/oas_core/spec/hashable.rb', line 27

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
    hash_representation_recursive(obj.hash_representation)
  else
    # Objects that serialize to a spec (e.g. MediaType) are hashed by
    # their serialized form. Falling through to the default branch would
    # embed their `inspect` output — memory address included — making
    # the digest different on every run.
    obj.respond_to?(:to_spec) ? hash_representation_recursive(obj.to_spec) : obj
  end
end

Instance Method Details

#hash_keyObject



8
9
10
# File 'lib/oas_core/spec/hashable.rb', line 8

def hash_key
  Hashable.generate_hash(hash_representation)
end

#hash_representationObject



12
13
14
# File 'lib/oas_core/spec/hashable.rb', line 12

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

#public_instance_variablesObject



20
21
22
23
24
25
# File 'lib/oas_core/spec/hashable.rb', line 20

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