Sha256: d5e618cd22abd39f158b09639b2f6c85b3ed7126f45d07a7e295a4f4c879512e

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require_relative 'attr'
require_relative 'hooks'
require_relative 'simple_serialize'

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

  def self.named_args *args
    args.last.is_a?(Hash) && args.last.keys.all?{|k|k.is_a?(Symbol)} ? args.last : Hash.new
  end

  def self.named_args! *args
    if args.last.is_a?(Hash) && args.last.keys.all?{|k|k.is_a?(Symbol)}
      args.delete_at(-1)
    else
      Hash.new
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bblib-0.3.0 lib/object/bbobject.rb