INSTANCE METHODS ######################################################
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 141 def after_initialize # initialize isn't always called @parent = nil @children = HSArray.new @children.parent = self @root = nil @is_root=false @in_tree = false @prev_left = nil @prev_right = nil end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 295 def after_save if @prev_left && @prev_right && @children.size > 0 # if we're moving from somewhere else @children.clear scale = ((right_col_val-left_col_val)/(@prev_right - @prev_left)).abs old_mid_point = (@prev_right + @prev_left)/2 new_mid_point = (right_col_val+left_col_val)/2 sql = "update #{self.class.table_name} t1 set t1.#{left_col_name} = ((t1.#{left_col_name} - #{old_mid_point})*#{scale})+#{new_mid_point}, t1.#{right_col_name} = ((t1.#{right_col_name} - #{old_mid_point})*#{scale})+#{new_mid_point} where t1.#{left_col_name} >#{@prev_left} and t1.#{right_col_name} < #{@prev_right} " connection.update_sql(sql) build_full_tree # get the children back else @children.each{|child| child.save if !(child.left_col_val && child.right_col_val) } # save only the ones that need lft and rgt end @prev_left = @prev_right = nil end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 278 def before_save if !@parent self.class.roots # this loads virtual_root with roots self.class.virtual_root.children << self # if we haven't got a parent then make the virtual root the parent end far_left = furthrest_left far_right = furthrest_right #puts "furthrest_right = #{far_right} furthrest_left = #{far_left}" gap = far_right - far_left if @children.size>0 self.left_col_val = far_left + gap/4 self.right_col_val = far_right - gap/4 else self.left_col_val = self.right_col_val = far_left+(gap/self.class.bias) end end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 211 def build_full_tree self.in_tree = true begin # get all the children with levels counted and ordered in one SQL statement - this is what nested sets are all about # then do a depth first traversal of all the entire list to build the tree in memory child_list = self.class.find_by_sql("SELECT t1.*,(select count(*) from #{self.class.table_name} t2 where t2.#{left_col_name}<t1.#{left_col_name} and t2.#{right_col_name} > t1.#{right_col_name}) as depth from #{self.class.table_name} t1 where t1.#{left_col_name} >#{left_col_val} and t1.#{right_col_name} <#{right_col_val} order by #{left_col_name} desc") child_list.each{|r| r.in_tree = true} #for some strange reason depth comes back as a string in Postgresql hence the .to_i build_tree_by_recursion(self,child_list.last.depth.to_i,child_list) rescue self.in_tree = false raise end return self end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 269 def children unless ( @children.size>0 || ( left_col_val == right_col_val ) || in_tree) # don't get children unless lft is not equal to rgt # puts "load_tree = #{in_tree}" @children << self.class.find( :all,:conditions => " #{self.class.table_name}.#{left_col_name} >#{left_col_val} and #{self.class.table_name}.#{right_col_name} < #{right_col_val} and #{left_col_val} = (select max(t1.#{left_col_name}) from #{self.class.table_name} t1 where #{self.class.table_name}.#{left_col_name} between t1.#{left_col_name} and t1.#{right_col_name})" ) else @children end end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 257 def is_root? @is_root end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 316 def leaf_nodes self.class.find :all,:conditions => " #{left_col_name} >#{left_col_val} and #{right_col_name} < #{right_col_val} and #{left_col_name} = #{right_col_name}" end
Short hand method to save typing self.class.left_col_name
# File lib/acts_as_hausdorff_space.rb, line 153 def left_col_name self.class.left_col_name end
get the left col value
# File lib/acts_as_hausdorff_space.rb, line 163 def left_col_val read_attribute(left_col_name) end
set the left col value
# File lib/acts_as_hausdorff_space.rb, line 168 def left_col_val=(value) write_attribute(left_col_name,value) end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 227 def lft_gap # puts " in lft gap #{parent.class.name}" gap = Gap.new gap.right_col_val = self.left_col_val if parent.children.first == self gap.left_col_val = parent.left_col_val else # puts "get prev index" prev_index = parent.children.index(self)-1 # puts "prev index = #{prev_index} count = #{parent.children.size}" gap.left_col_val = parent.children[prev_index].right_col_val end return gap end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 320 def mid_point (furthrest_left+furthrest_right)/2 end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 261 def my_root unless @root @root = (self.class.find :first, :conditions => " #{left_col_val} >= #{left_col_name} and #{right_col_val}<= #{right_col_name} ",:order =>"#{left_col_name}") #between includes the end points else @root end end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 324 def needs_moving if left_col_val && right_col_val && !in_tree @prev_left = left_col_val self.left_col_val = nil @prev_right = right_col_val self.right_col_val = nil end end
Get the parent. If the parent hasn’t already been set, and if we’re not building the full tree (which sets parents) and if we’ve got a valid lft and right then construct the SQL to get the parent from the DB. If none is found then it’s a root so set the parent to be the class.virtual_root If the parent is already set then don’t go to the database to look for it, just return it
# File lib/acts_as_hausdorff_space.rb, line 187 def parent if !in_tree && left_col_val && right_col_val && !@parent # only get the parent by SQL if we don't know what it is @parent = self.class.find :first, :conditions => " #{left_col_val} > #{self.class.left_col_name} and #{left_col_val} < #{self.class.right_col_name} ",:order =>'#{self.class.left_col_name} DESC' if !@parent # @is_root = (lft == rgt) || (id == root.id) return self.class.virtual_root end else @parent end end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 200 def parent=(new_parent) if @parent != new_parent @parent = new_parent needs_moving @is_root = @parent.is_a?(VirtualRoot) end # [TODO] clean this up with parent.save_children parent.save unless in_tree || @parent.is_a?(VirtualRoot) return @parent end
(Not documented)
# File lib/acts_as_hausdorff_space.rb, line 242 def rgt_gap # puts " in rgt gap #{parent.class.name}" gap = Gap.new gap.left_col_val = self.right_col_val if parent.children.last == self gap.right_col_val = parent.right_col_val else # puts "get next index" next_index = parent.children.index(self)+1 # puts "next index = #{next_index} count = #{parent.children.size}" gap.right_col_val = parent.children[next_index].left_col_val end return gap end
Short hand method to save typing self.class.right_col_name
# File lib/acts_as_hausdorff_space.rb, line 158 def right_col_name self.class.right_col_name end
get the right col value
# File lib/acts_as_hausdorff_space.rb, line 173 def right_col_val read_attribute(right_col_name) end
this assumes a nicely ordered tree such that the next depth is always one more tha the existing depth otherwise throw a wobbly the strange .to_i has to be there because depth can come back from the db as a string
# File lib/acts_as_hausdorff_space.rb, line 355 def build_tree_by_recursion(parent,depth,ar_results) while ar_results.size > 0 && ar_results.last.depth.to_i >= depth if ar_results.last.depth.to_i == depth current_record = ar_results.pop # puts "curr record in tree =#{current_record.in_tree} parent in tree = #{parent.in_tree}" parent.children << current_record current_record.in_tree = false # puts "+++" elsif ar_results.last.depth.to_i == (depth + 1) current_record.in_tree = true build_tree_by_recursion(current_record,ar_results.last.depth.to_i,ar_results) current_record.in_tree = false elsif ar_results.last.depth.to_i > (depth + 1) raise "badly formed nested set result set" end end # while end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.