module Artemis::Services::ObjectToHash

Public Class Methods

convert(object, opts = { include_nils: true }) click to toggle source
# File lib/artemis/services/object_to_hash.rb, line 4
def self.convert(object, opts = { include_nils: true })
  hash = {}
  object.instance_variables.each do |var|
    var_value = object.instance_variable_get(var)

    next if !opts['include_nils'] && var_value.nil?

    hash[var.to_s.delete('@')] = if descendant?(var_value, Artemis::Bot)
                                   convert(var_value)
                                 else
                                   hash[var.to_s.delete('@')] = var_value
                                 end
  end

  hash
end
descendant?(value, parent) click to toggle source
# File lib/artemis/services/object_to_hash.rb, line 21
def self.descendant?(value, parent)
  parent = parent.class unless parent.is_a?(Class) || parent.is_a?(Module)
  value.class.to_s.index("#{parent}::").present?
end