Sha256: 6130080558c7933c4a19651dd71ed42543c9a0d35e24c1a7f01c56527a6024e8

Contents?: true

Size: 1.55 KB

Versions: 33

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

class ReeObject::AsJson
  include Ree::FnDSL

  fn :as_json do
    def_error { RecursiveObjectErr }
  end
  
  BASIC_TYPES = [
    Date, Time, Numeric, String, FalseClass, TrueClass, NilClass, Symbol,
    Module, Class
  ].freeze

  contract(
    Any => Or[Hash, ArrayOf[Any], *BASIC_TYPES]
  ).throws(RecursiveObjectErr)
  def call(obj)
    recursively_convert(obj, {}, {})
  end

  private

  def recursively_convert(obj, acc, cache)
    ancestors = obj.class.ancestors

    if ancestors.intersection(BASIC_TYPES).size > 0
      obj
    elsif obj.is_a?(Array)
      convert_array(obj, acc, cache)
    elsif obj.is_a?(Hash)
      convert_hash(obj, acc, cache)
    elsif obj.respond_to?(:to_h)
      convert_hash(obj.to_h, acc, cache)
    else
      convert_object(obj, acc, cache)
    end
  end

  def convert_array(obj, acc, cache)
    obj.map { |el| recursively_convert(el, {}, cache) }
  end

  def convert_hash(obj, acc, cache)
    obj.each do |k, v|
      key_sym = k.to_sym
      acc[key_sym] = recursively_convert(v, {}, cache)
    end

    acc
  end

  def convert_object(obj, acc, cache)
    return obj if obj.is_a?(Class) || obj.is_a?(Module)

    if cache.key?(obj.object_id)
      raise RecursiveObjectErr, "Recursive object found: #{obj}"
    end

    cache[obj.object_id] = acc
    
    obj.instance_variables.each do |var|
      key_name = var.to_s.delete("@")
      key_sym = key_name.to_sym

      key = key_sym
      value = obj.instance_variable_get(var)

      acc[key] = recursively_convert(value, {}, cache)
    end

    acc
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
ree_lib-1.0.33 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.32 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.31 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.30 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.29 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.28 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.27 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.26 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.24 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.23 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.22 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.21 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.20 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.19 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.18 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.17 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.16 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.15 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.14 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
ree_lib-1.0.13 lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb