Class: OrderTree::OrderTreeNode

Inherits:
UniqueProxy show all
Defined in:
lib/order_tree/order_tree_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from UniqueProxy

#!, #!=, #==, #equal?, #inspect, #method_missing, #orig, #to_s, #unique_id

Constructor Details

#initialize(obj, tree) ⇒ OrderTreeNode

Returns a new instance of OrderTreeNode.



7
8
9
10
# File 'lib/order_tree/order_tree_node.rb', line 7

def initialize obj, tree
  super(obj)
  @tree = tree
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OrderTree::UniqueProxy

Instance Attribute Details

#nextObject

Returns the value of attribute next.



3
4
5
# File 'lib/order_tree/order_tree_node.rb', line 3

def next
  @next
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/order_tree/order_tree_node.rb', line 5

def path
  @path
end

#prevObject

Returns the value of attribute prev.



3
4
5
# File 'lib/order_tree/order_tree_node.rb', line 3

def prev
  @prev
end

#treeObject

Returns the value of attribute tree.



4
5
6
# File 'lib/order_tree/order_tree_node.rb', line 4

def tree
  @tree
end

Instance Method Details

#<=>(other) ⇒ Object

Raises:

  • (::ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/order_tree/order_tree_node.rb', line 55

def <=> other
  if self.equal? other
    return 0
  else
    p, n = self.prev, self.next
    while p or n
      return 1 if p.equal? other
      return -1 if n.equal? other
      p = p.prev if p
      n = n.next if n
    end
  end
  raise ::ArgumentError, "Cannot compare #{self} and #{other} because they are not in the same tree" 
end

#after(other) ⇒ Object



51
52
53
# File 'lib/order_tree/order_tree_node.rb', line 51

def after other
  (self <=> other) == 1 ? true : false
end

#before(other) ⇒ Object



47
48
49
# File 'lib/order_tree/order_tree_node.rb', line 47

def before other
  (self <=> other) == -1 ? true : false
end

#path!Object



74
75
76
# File 'lib/order_tree/order_tree_node.rb', line 74

def path!
  @path = self.tree.root.strict_path! self
end

#removeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/order_tree/order_tree_node.rb', line 12

def remove
  prev_node = self.prev
  next_node = self.next
  self.next.prev = prev_node if self.next 
  self.prev.next = next_node if self.prev 

  if self.tree.root.first.equal? self
    if next_node
      self.tree.root.instance_eval do
        self.first = next_node
      end
    end
  end

  if self.tree.root.last.equal? self
    if prev_node
      self.tree.root.instance_eval do
        self.last = prev_node
      end
    end
  end

  # try this so that the node can remove
  # itself fromt he tree
  my_path = self.path
  self.tree.instance_eval do
    _delegate_hash.delete my_path.last
  end
  @path = nil
  @tree = nil
  @next = nil
  @prev = nil
  self
end