Module: InstVarsToHash

Overview

This module will allow you to properly to_s, to_h, and to_json on classes in which it is included.

It is not intended to be general purpose, but rather should be used only with standards and standards-related data structures, which themselves are mirrors of the JSON definitions used by Academic Benchmarks

Instance Method Summary collapse

Instance Method Details

#to_h(omit_parent: true, omit_empty_children: true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/academic_benchmarks/lib/inst_vars_to_hash.rb', line 19

def to_h(omit_parent: true, omit_empty_children: true)
  retval = {}
  instance_variables.each do |iv|
    # Don't hashify these attributes, otherwise it can lead to infinite recursion
    next if %w[@authority @document @publication @section].include? iv.to_s
    if !(skip_parent?(omit_parent, iv) || skip_children?(omit_empty_children, iv))
      retval[iv.to_s.delete('@').to_sym] = elem_to_h(instance_variable_get(iv))
    end
  end
  retval
end

#to_json(omit_parent: true, omit_empty_children: true) ⇒ Object



31
32
33
34
35
36
# File 'lib/academic_benchmarks/lib/inst_vars_to_hash.rb', line 31

def to_json(omit_parent: true, omit_empty_children: true)
  to_h(
    omit_parent: omit_parent,
    omit_empty_children: omit_empty_children
  ).to_json
end

#to_s(omit_parent: true, omit_empty_children: true) ⇒ Object



12
13
14
15
16
17
# File 'lib/academic_benchmarks/lib/inst_vars_to_hash.rb', line 12

def to_s(omit_parent: true, omit_empty_children: true)
  to_h(
    omit_parent: omit_parent,
    omit_empty_children: omit_empty_children
  ).to_s
end