moved utility functions to own module (used from members and writer)

This commit is contained in:
Torsten Ruger 2014-08-14 19:48:06 +03:00
parent 0a33f940cf
commit 519283a034
2 changed files with 30 additions and 20 deletions

View File

@ -1,6 +1,8 @@
module Sof
class Members
include Util
def initialize root
@root = root
@counter = 1
@ -10,7 +12,7 @@ module Sof
attr_reader :objects , :root
def add object , level
return if Members.is_value?(object)
return if is_value?(object)
if( @objects.has_key?(object) )
occurence = @objects[object]
occurence.level = level if occurence.level > level
@ -21,7 +23,7 @@ module Sof
@counter = @counter + 1
attributes = attributes_for(object)
attributes.each do |a|
val = object.instance_variable_get "@#{a}".to_sym
val = get_value( object , a)
add(val , level + 1)
end
if( object.is_a? Array )
@ -30,23 +32,5 @@ module Sof
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

26
lib/sof/util.rb Normal file
View 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