Class: Rubrowser::Tree
- Inherits:
-
Object
- Object
- Rubrowser::Tree
- Defined in:
- lib/tree.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#occurences ⇒ Object
readonly
Returns the value of attribute occurences.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #add_child(child_name_path = []) ⇒ Object
- #add_occurence(user, constants) ⇒ Object
- #eq?(other) ⇒ Boolean
- #find_node(path) ⇒ Object
- #get_or_create_child(child_name) ⇒ Object
- #id ⇒ Object
-
#initialize(name = 'Kernel', parent = nil) ⇒ Tree
constructor
A new instance of Tree.
- #root? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(name = 'Kernel', parent = nil) ⇒ Tree
Returns a new instance of Tree.
16 17 18 19 20 21 |
# File 'lib/tree.rb', line 16 def initialize(name = 'Kernel', parent = nil) @name = name @parent = parent @children = Set.new @occurences = Set.new end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/tree.rb', line 3 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/tree.rb', line 3 def name @name end |
#occurences ⇒ Object
Returns the value of attribute occurences.
3 4 5 |
# File 'lib/tree.rb', line 3 def occurences @occurences end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/tree.rb', line 3 def parent @parent end |
Class Method Details
.from_parsers(parsers) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/tree.rb', line 5 def self.from_parsers(parsers) return Tree.new if parsers.empty? definitions = parsers.map(&:definitions).reduce(:+).uniq occurences = parsers.map(&:occurences).reduce(:+).uniq Tree.new.tap do |tree| definitions.each { |definition| tree.add_child(definition) } occurences.each { |occurence| tree.add_occurence(*occurence.first) } end end |
Instance Method Details
#add_child(child_name_path = []) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/tree.rb', line 32 def add_child(child_name_path = []) return if child_name_path.empty? child = get_or_create_child(child_name_path[0]) children.add(child) child.add_child(child_name_path[1..-1]) end |
#add_occurence(user, constants) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/tree.rb', line 39 def add_occurence(user, constants) user_node = find_node(user) occured_node = constants.map { |constant| find_node(constant) }.compact.first return unless user_node && occured_node user_node.occurences << occured_node end |
#eq?(other) ⇒ Boolean
57 58 59 |
# File 'lib/tree.rb', line 57 def eq?(other) other.name == name end |
#find_node(path) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/tree.rb', line 46 def find_node(path) return self if path.empty? child = children.find { |c| c.name == path.first } return unless child child.find_node(path[1..-1]) end |
#get_or_create_child(child_name) ⇒ Object
53 54 55 |
# File 'lib/tree.rb', line 53 def get_or_create_child(child_name) children.find { |child| child_name == child.name } || Tree.new(child_name, self) end |
#id ⇒ Object
27 28 29 30 |
# File 'lib/tree.rb', line 27 def id return name if root? || parent.root? "#{parent.id}::#{name}".to_sym end |
#root? ⇒ Boolean
23 24 25 |
# File 'lib/tree.rb', line 23 def root? @parent.nil? end |
#to_h ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/tree.rb', line 61 def to_h { id: id, name: name }.tap do |hash| hash[:children] = children.map(&:to_h) unless children.empty? hash[:occurences] = occurences.map(&:id) unless occurences.empty? end end |