Class: Tree::TreeNode

Inherits:
Object show all
Defined in:
lib/tagen/tree.rb

Instance Method Summary collapse

Instance Method Details

#__compare(other) ⇒ Object

Raises:

  • (ArgumentError)


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

Returns:

  • (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

Returns:

  • (Boolean)


72
# File 'lib/tagen/tree.rb', line 72

def ancestor_of?(other) __compare(other)==1 ? true : false end

#descendant_in?(other) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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_parentageObject



36
# File 'lib/tagen/tree.rb', line 36

alias original_parentage parentage

#parentageObject

  • 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 ?

Returns:

  • (Boolean)


71
# File 'lib/tagen/tree.rb', line 71

def same?(other) __compare(other)==0 ? true : false end

#treenameObject

return “a b c”



50
51
52
# File 'lib/tagen/tree.rb', line 50

def treename
	self.treetage.join(" ")
end

#treetageObject

parentage in reverse

See Also:



45
46
47
# File 'lib/tagen/tree.rb', line 45

def treetage
	([self.name] + self.parentage.map{|v| v.name}).reverse[1..-1]
end