Class: Tree::TreeNode
Instance Method Summary collapse
- #__compare(other) ⇒ Object
- #ancestor_in?(other) ⇒ Boolean
- #ancestor_of?(other) ⇒ Boolean
- #descendant_in?(other) ⇒ Boolean
- #descendant_of?(other) ⇒ Boolean
-
#get(name) ⇒ Object
get a Node name.
-
#name=(name) ⇒ Object
set a Node name.
- #original_parentage ⇒ Object
-
#parentage ⇒ Object
-
default return nil for root * now return [] for root.
-
-
#same?(other) ⇒ Boolean
is same node ?.
-
#treename ⇒ Object
return “a b c”.
-
#treetage ⇒ Object
parentage in reverse.
Instance Method Details
#__compare(other) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tagen/tree.rb', line 54 def __compare other raise ArgumentError, "comparison '{self}` with '{other}` failed" .format(self.class, other) unless self.class === other a = self.treetage b = other.treetage if a.length==b.length return 0 if a==b elsif a.length < b.length return 1 if a==b[0...a.length] elsif a.length>b.length return -1 if a[0...b.length]==b end end |
#ancestor_in?(other) ⇒ Boolean
74 |
# File 'lib/tagen/tree.rb', line 74 def ancestor_in?(other) [0, 1].include?(__compare(other)) ? true : false end |
#ancestor_of?(other) ⇒ Boolean
72 |
# File 'lib/tagen/tree.rb', line 72 def ancestor_of?(other) __compare(other)==1 ? true : false end |
#descendant_in?(other) ⇒ Boolean
75 |
# File 'lib/tagen/tree.rb', line 75 def descendant_in?(other) [0,-1].include?(__compare(other)) ? true : false end |
#descendant_of?(other) ⇒ Boolean
73 |
# File 'lib/tagen/tree.rb', line 73 def descendant_of?(other) __compare(other)==-1 ? true : false end |
#get(name) ⇒ Object
get a Node name
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tagen/tree.rb', line 14 def get name return name if self.class===name treetage = Array===name ? name : name.split(/ +/) treetage.gach self.root do |n,i,m| if m[n] m[n] else raise ENoNode, "'#{n}` node doesn't exists for root `#{root.name}'" end end end |
#name=(name) ⇒ Object
set a Node name
28 29 30 31 32 33 34 |
# File 'lib/tagen/tree.rb', line 28 def name= name old = @name @name = name children_hash = parent.instance_variable.get :@children_hash children_hash.delete old children_hash[@name] = self end |
#original_parentage ⇒ Object
36 |
# File 'lib/tagen/tree.rb', line 36 alias original_parentage parentage |
#parentage ⇒ Object
-
default return nil for root
-
now return [] for root
39 40 41 |
# File 'lib/tagen/tree.rb', line 39 def parentage is_root? ? [] : original_parentage end |
#same?(other) ⇒ Boolean
is same node ?
71 |
# File 'lib/tagen/tree.rb', line 71 def same?(other) __compare(other)==0 ? true : false end |
#treename ⇒ Object
return “a b c”
50 51 52 |
# File 'lib/tagen/tree.rb', line 50 def treename self.treetage.join(" ") end |
#treetage ⇒ Object
parentage in reverse
45 46 47 |
# File 'lib/tagen/tree.rb', line 45 def treetage ([self.name] + self.parentage.map{|v| v.name}).reverse[1..-1] end |