module Idnow::Jsonable

Public Instance Methods

to_h() click to toggle source
# File lib/idnow/modules/jsonable.rb, line 5
def to_h
  instance_variables.each_with_object({}) do |var, result_hash|
    key = var.to_s.delete('@')
    value = if instance_variable_get(var).respond_to?(:to_h)
              instance_variable_get(var).to_h
            else
              instance_variable_get(var).to_s
            end
    result_hash[key] = value
  end
end
to_json(*_args) click to toggle source
# File lib/idnow/modules/jsonable.rb, line 17
def to_json(*_args)
  keys_without_underscores(to_h).to_json
end

Private Instance Methods

keys_without_underscores(obj) click to toggle source
# File lib/idnow/modules/jsonable.rb, line 23
def keys_without_underscores(obj)
  return obj unless obj.is_a?(Hash)

  obj.each_with_object({}) do |(k, v), result|
    result[k.to_s.delete('_')] = keys_without_underscores(v)
  end
end