moved utility functions to own module (used from members and writer)
This commit is contained in:
parent
0a33f940cf
commit
519283a034
@ -1,6 +1,8 @@
|
|||||||
module Sof
|
module Sof
|
||||||
|
|
||||||
class Members
|
class Members
|
||||||
|
include Util
|
||||||
|
|
||||||
def initialize root
|
def initialize root
|
||||||
@root = root
|
@root = root
|
||||||
@counter = 1
|
@counter = 1
|
||||||
@ -10,7 +12,7 @@ module Sof
|
|||||||
attr_reader :objects , :root
|
attr_reader :objects , :root
|
||||||
|
|
||||||
def add object , level
|
def add object , level
|
||||||
return if Members.is_value?(object)
|
return if is_value?(object)
|
||||||
if( @objects.has_key?(object) )
|
if( @objects.has_key?(object) )
|
||||||
occurence = @objects[object]
|
occurence = @objects[object]
|
||||||
occurence.level = level if occurence.level > level
|
occurence.level = level if occurence.level > level
|
||||||
@ -21,7 +23,7 @@ module Sof
|
|||||||
@counter = @counter + 1
|
@counter = @counter + 1
|
||||||
attributes = attributes_for(object)
|
attributes = attributes_for(object)
|
||||||
attributes.each do |a|
|
attributes.each do |a|
|
||||||
val = object.instance_variable_get "@#{a}".to_sym
|
val = get_value( object , a)
|
||||||
add(val , level + 1)
|
add(val , level + 1)
|
||||||
end
|
end
|
||||||
if( object.is_a? Array )
|
if( object.is_a? Array )
|
||||||
@ -30,23 +32,5 @@ module Sof
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.is_value? o
|
|
||||||
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
|
|
||||||
|
|
||||||
def attributes_for object
|
|
||||||
if( Known.is( object.class ))
|
|
||||||
Known.attributes(object.class)
|
|
||||||
else
|
|
||||||
object.instance_variables.collect{|i| i.to_s[1..-1].to_sym } # chop of @
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
26
lib/sof/util.rb
Normal file
26
lib/sof/util.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
module Sof
|
||||||
|
module Util
|
||||||
|
def is_value? o
|
||||||
|
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
|
||||||
|
|
||||||
|
def get_value(object,name)
|
||||||
|
object.instance_variable_get "@#{name}".to_sym
|
||||||
|
end
|
||||||
|
|
||||||
|
def attributes_for object
|
||||||
|
if( Known.is( object.class ))
|
||||||
|
Known.attributes(object.class)
|
||||||
|
else
|
||||||
|
object.instance_variables.collect{|i| i.to_s[1..-1].to_sym } # chop of @
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user