2014-08-13 19:05:00 +02:00
|
|
|
module Sof
|
|
|
|
|
|
|
|
class Members
|
2014-08-14 18:48:06 +02:00
|
|
|
include Util
|
|
|
|
|
2014-08-13 19:05:00 +02:00
|
|
|
def initialize root
|
|
|
|
@root = root
|
|
|
|
@counter = 1
|
|
|
|
@objects = {}
|
2014-08-18 12:53:05 +02:00
|
|
|
@referenced = false
|
2014-08-14 19:24:26 +02:00
|
|
|
add(root , 0)
|
2014-08-13 19:05:00 +02:00
|
|
|
end
|
2014-08-18 12:53:05 +02:00
|
|
|
attr_reader :objects , :root , :referenced
|
2014-08-14 16:40:56 +02:00
|
|
|
|
2014-08-13 19:05:00 +02:00
|
|
|
def add object , level
|
2014-08-14 18:48:06 +02:00
|
|
|
return if is_value?(object)
|
2014-08-21 14:21:20 +02:00
|
|
|
if( occurence = @objects[object.object_id] )
|
2014-08-18 12:33:40 +02:00
|
|
|
#puts "reset level #{level} at #{occurence.level}"
|
2014-08-21 14:21:20 +02:00
|
|
|
if occurence.level > level
|
|
|
|
occurence.level = level
|
|
|
|
end
|
|
|
|
unless occurence.referenced
|
|
|
|
#puts "referencing #{@counter} , at level #{level}/#{occurence.level} "
|
|
|
|
occurence.referenced = @counter
|
|
|
|
@counter = @counter + 1
|
|
|
|
end
|
2014-08-14 18:24:18 +02:00
|
|
|
return
|
|
|
|
end
|
2014-08-18 16:25:49 +02:00
|
|
|
o = Occurence.new( object , level )
|
2014-08-21 14:21:20 +02:00
|
|
|
@objects[object.object_id] = o
|
2014-08-14 18:24:18 +02:00
|
|
|
attributes = attributes_for(object)
|
|
|
|
attributes.each do |a|
|
2014-08-14 18:48:06 +02:00
|
|
|
val = get_value( object , a)
|
2014-08-14 18:24:18 +02:00
|
|
|
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
|
2014-08-15 16:18:15 +02:00
|
|
|
if( object.is_a? Hash )
|
|
|
|
object.each do |a,b|
|
|
|
|
add(a , level + 1)
|
|
|
|
add(b , level + 1)
|
|
|
|
end
|
|
|
|
end
|
2014-08-13 19:05:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|