Module: Hashify

Included in:
Exam::Results
Defined in:
lib/ruby_proctor/hashify.rb

Instance Method Summary collapse

Instance Method Details

#ivars_excluded_from_hashObject

Classes that include this module can exclude certain instance variable from its hash representation by overriding this method



5
6
7
# File 'lib/ruby_proctor/hashify.rb', line 5

def ivars_excluded_from_hash
  []
end

#to_hashObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_proctor/hashify.rb', line 9

def to_hash
  hash = {}
  excluded_ivars = ivars_excluded_from_hash

  # Iterate over all the instance variables and store their
  # names and values in a hash
  instance_variables.each do |var|
    next if excluded_ivars.include? var.to_s

    value = instance_variable_get(var)
    value = value.map(&:to_hash) if value.is_a? Array

    hash[var.to_s.delete("@")] = value
  end

  return hash
end