Class: WordTreeNode

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tree/word_tree_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word = nil, level = 0, parent = nil) ⇒ WordTreeNode

Returns a new instance of WordTreeNode.



6
7
8
9
10
11
12
13
# File 'lib/tree/word_tree_node.rb', line 6

def initialize(word = nil, level = 0, parent = nil)
  @word = word
  @cleanword = CommandWord.clean(word) if word
  @children = {}
  @line_count = 0
  @level = level
  @parent = parent
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



4
5
6
# File 'lib/tree/word_tree_node.rb', line 4

def children
  @children
end

#cleanwordObject

Returns the value of attribute cleanword.



4
5
6
# File 'lib/tree/word_tree_node.rb', line 4

def cleanword
  @cleanword
end

#levelObject

Returns the value of attribute level.



4
5
6
# File 'lib/tree/word_tree_node.rb', line 4

def level
  @level
end

#line_countObject

Returns the value of attribute line_count.



4
5
6
# File 'lib/tree/word_tree_node.rb', line 4

def line_count
  @line_count
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/tree/word_tree_node.rb', line 4

def parent
  @parent
end

#wordObject

Returns the value of attribute word.



4
5
6
# File 'lib/tree/word_tree_node.rb', line 4

def word
  @word
end

Instance Method Details

#<=>(b) ⇒ Object



34
35
36
# File 'lib/tree/word_tree_node.rb', line 34

def <=> b
  b.line_count <=> line_count
end

#add(line, level = 0) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/tree/word_tree_node.rb', line 15

def add(line, level = 0)
  word = line.words[level]
  clean_word = line.clean_words[level]
  @line_count += 1
  return unless word

  @children[word] ||= WordTreeNode.new(word, level + 1, self)
  @children[word].add(line, level + 1)
end

#each(&block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/tree/word_tree_node.rb', line 25

def each &block
  children.values.sort.each do |v|
    block.call(v)
    v.each do |q|
      block.call(q)
    end
  end
end

#minimum(min) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/tree/word_tree_node.rb', line 72

def minimum(min)
  # 1.9.2 compatibility
  # children.select{|k, v| v.line_count >= min }
  ret = {}
  children.each{|k, v| ret[k] = v if v.line_count >= min }
  ret
end

#root?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/tree/word_tree_node.rb', line 42

def root?
  word.nil?
end

#to_treeObject



67
68
69
70
# File 'lib/tree/word_tree_node.rb', line 67

def to_tree
  return "" if root?
  "#{("  " * (level - 1))}" + "#{word}".green + " #{line_count}:\n"
end

#truncate!(minimum) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/tree/word_tree_node.rb', line 46

def truncate!(minimum)
  # Ruby 1.9.2
  # children.select!{|k, v| v.line_count >= minimum }

  ret = {}
  children.each{|k, v| ret[k] = v if v.line_count >= minimum }
  self.children = ret

  children.each{|k, v| v.truncate!(minimum) }
end

#word_listObject



57
58
59
60
61
62
63
64
65
# File 'lib/tree/word_tree_node.rb', line 57

def word_list
  list = [self.word]
  node = self
  while node = node.parent
    next if node.word.nil?
    list << node.word
  end
  list.reverse
end

#wordsObject



38
39
40
# File 'lib/tree/word_tree_node.rb', line 38

def words
  children.keys.uniq
end