Class: BasicTree

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/basic_tree.rb,
lib/basic_tree/version.rb

Defined Under Namespace

Classes: Kids

Constant Summary collapse

VERSION =
"1.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, parent = nil, &block) ⇒ BasicTree

TODO: test



17
18
19
20
21
22
23
# File 'lib/basic_tree.rb', line 17

def initialize(object, parent = nil, &block)
  self.object = object
  parent.try(:insert!, self)
  if block_given?
    block.arity == 1 ? yield(self) : instance_eval(&block)
  end
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



96
97
98
# File 'lib/basic_tree.rb', line 96

def object
  @object
end

#parentObject

Returns the value of attribute parent.



95
96
97
# File 'lib/basic_tree.rb', line 95

def parent
  @parent
end

Instance Method Details

#<=>(other) ⇒ Object



130
131
132
# File 'lib/basic_tree.rb', line 130

def <=>(other)
  subtree.map(&:object) <=> other.subtree.map(&:object)
end

#add(object, &block) ⇒ Object

TODO: test



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

def add(object, &block)
  if object.is_a?(self.class)
    insert!(object)
  else
    self.class.new(object, self, &block)
  end
end

#ancestorsObject



71
72
73
# File 'lib/basic_tree.rb', line 71

def ancestors
  root? ? [] : (parent.ancestors << parent)
end

#childrenObject



63
64
65
# File 'lib/basic_tree.rb', line 63

def children
  kids.dup
end

#descendantsObject



75
76
77
78
79
# File 'lib/basic_tree.rb', line 75

def descendants
  d = []
  kids.each { |k| d += k.descendants.unshift(k) }
  d
end

#first?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/basic_tree.rb', line 120

def first?
  root? || siblings_and_self[0] == self
end

#insert!(basic_tree) ⇒ Object

TODO: test

Raises:

  • (ArgumentError)


35
36
37
38
39
# File 'lib/basic_tree.rb', line 35

def insert!(basic_tree)
  raise ArgumentError, "Must be a #{self.class}" unless basic_tree.is_a?(self.class)
  basic_tree.send(:parent=, self)
  kids << basic_tree
end

#last?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/basic_tree.rb', line 124

def last?
  root? || siblings_and_self.last == self
end

#leaf?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/basic_tree.rb', line 116

def leaf?
  kids.empty?
end

#levelObject



102
103
104
# File 'lib/basic_tree.rb', line 102

def level
  path.size
end

#move_down!Object

TODO: test



56
57
58
59
# File 'lib/basic_tree.rb', line 56

def move_down!
  raise "Already last" if last?
  parent.send(:kids).swap!(position, position + 1)
end

#move_up!Object

TODO: test



50
51
52
53
# File 'lib/basic_tree.rb', line 50

def move_up!
  raise "Already first" if first?
  parent.send(:kids).swap!(position, position - 1)
end

#pathObject



67
68
69
# File 'lib/basic_tree.rb', line 67

def path
  ancestors << self
end

#positionObject



106
107
108
# File 'lib/basic_tree.rb', line 106

def position
  siblings_and_self.index(self)
end

#remove!(basic_tree) ⇒ Object

TODO: test

Raises:

  • (ArgumentError)


42
43
44
45
46
47
# File 'lib/basic_tree.rb', line 42

def remove!(basic_tree)
  raise ArgumentError, "Must be a #{self.class}" unless basic_tree.is_a?(self.class)
  raise StandardError, "Can't remove root" if root?
  parent.send(:kids).delete(self)
  basic_tree.send(:parent=, nil)
end

#rootObject



98
99
100
# File 'lib/basic_tree.rb', line 98

def root
  path.first
end

#root?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/basic_tree.rb', line 112

def root?
  !parent
end

#siblingsObject



89
90
91
# File 'lib/basic_tree.rb', line 89

def siblings
  root? ? [] : siblings_and_self.delete_if { |s| s.equal?(self) }
end

#siblings_and_selfObject



85
86
87
# File 'lib/basic_tree.rb', line 85

def siblings_and_self
  root? ? [self] : parent.children
end

#subtreeObject



81
82
83
# File 'lib/basic_tree.rb', line 81

def subtree
  descendants.unshift(self)
end