2014-08-13 19:05:00 +02:00
|
|
|
module Sof
|
|
|
|
|
|
|
|
class Members
|
|
|
|
def initialize root
|
|
|
|
@root = root
|
|
|
|
@counter = 1
|
|
|
|
@objects = {}
|
|
|
|
add(root ,0 )
|
|
|
|
end
|
2014-08-14 18:24:18 +02:00
|
|
|
attr_reader :objects , :root
|
2014-08-14 16:40:56 +02:00
|
|
|
|
2014-08-13 19:05:00 +02:00
|
|
|
def add object , level
|
2014-08-14 18:24:18 +02:00
|
|
|
return if Members.is_value?(object)
|
2014-08-13 19:05:00 +02:00
|
|
|
if( @objects.has_key?(object) )
|
2014-08-14 16:40:56 +02:00
|
|
|
occurence = @objects[object]
|
2014-08-13 19:05:00 +02:00
|
|
|
occurence.level = level if occurence.level > level
|
2014-08-14 18:24:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
o = Occurence.new( object , @counter , level )
|
|
|
|
@objects[object] = o
|
|
|
|
@counter = @counter + 1
|
|
|
|
attributes = attributes_for(object)
|
|
|
|
attributes.each do |a|
|
|
|
|
val = object.instance_variable_get "@#{a}".to_sym
|
|
|
|
add(val , level + 1)
|
|
|
|
end
|
|
|
|
if( object.is_a? Array )
|
|
|
|
object.each do |a|
|
|
|
|
add(a , level + 1)
|
2014-08-13 19:05:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-14 18:24:18 +02:00
|
|
|
def self.is_value? o
|
2014-08-14 16:40:56 +02:00
|
|
|
return true if o == true
|
|
|
|
return true if o == false
|
|
|
|
return true if o == nil
|
|
|
|
return true if o.class == Fixnum
|
|
|
|
return true if o.class == Symbol
|
|
|
|
return true if o.class == String
|
|
|
|
return false
|
|
|
|
end
|
2014-08-13 19:05:00 +02:00
|
|
|
|
2014-08-14 18:24:18 +02:00
|
|
|
def attributes_for object
|
|
|
|
if( Known.is( object.class ))
|
|
|
|
Known.attributes(object.class)
|
2014-08-13 19:05:00 +02:00
|
|
|
else
|
2014-08-14 18:24:18 +02:00
|
|
|
object.instance_variables.collect{|i| i.to_s[1..-1].to_sym } # chop of @
|
2014-08-13 19:05:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|