Sha256: b87b150ad591c736b66c7e5e8dc487954b55552074622b161005be4efdaca200

Contents?: true

Size: 1020 Bytes

Versions: 2

Compression:

Stored size: 1020 Bytes

Contents

module BBLib

  def self.to_hash obj
    return {obj => nil} unless !obj.instance_variables.empty?
    hash = {}
    obj.instance_variables.each do |var|
      value = obj.instance_variable_get(var)
      if value.is_a? Array
        hash[var.to_s.delete("@")] = value.map{ |v| v.respond_to?(:obj_to_hash) && !v.instance_variables.empty? ? v.obj_to_hash : v }
      elsif value.is_a? Hash
        begin
          if !hash[var.to_s.delete("@")].is_a?(Hash) then hash[var.to_s.delete("@")] = Hash.new end
        rescue
          hash[var.to_s.delete("@")] = Hash.new
        end
        value.each do |k, v|
          hash[var.to_s.delete("@")][k.to_s.delete("@")] = v.respond_to?(:obj_to_hash) && !v.instance_variables.empty? ? v.obj_to_hash : v
        end
      elsif value.respond_to?(:obj_to_hash) && !value.instance_variables.empty?
        hash[var.to_s.delete("@")] = value.obj_to_hash
      else
        hash[var.to_s.delete("@")] = value
      end
    end
    return hash
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bblib-0.2.2 lib/object/bbobject.rb
bblib-0.2.0 lib/object/bbobject.rb