Sha256: ab4a5ee78a59d1f493faeb570c863a01ce01d8af5cd9276548d512834ee2bcc9

Contents?: true

Size: 783 Bytes

Versions: 1

Compression:

Stored size: 783 Bytes

Contents

class PeakFlowUtils::ParseJson
  def initialize(object)
    @object = object
  end

  def parse
    parse_to_json(@object)
  end

  def active_record?(object)
    object.class.ancestors.any? do |ancestor|
      ancestor.name == "ActiveRecord::Base"
    end
  end

  def parse_to_json(object)
    if object.is_a?(Hash)
      result = {}

      object.each do |key, value|
        result[key] = parse_to_json(value)
      end

      return result
    elsif object.is_a?(Array)
      return object.map do |value|
        parse_to_json(value)
      end
    elsif active_record?(object)
      result = "#<#{object.class.name} id: #{object.id}"
      result << " name: \"#{object.name}\"" if object.respond_to?(:name)
      result << ">"

      return result
    end

    object
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
peak_flow_utils-0.1.19 app/services/peak_flow_utils/parse_json.rb