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)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/order_tree/order_tree_node.rb', line 64

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



60
61
62
# File 'lib/order_tree/order_tree_node.rb', line 60

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

#before(other) ⇒ Object



56
57
58
# File 'lib/order_tree/order_tree_node.rb', line 56

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

#deproxyObject



47
48
49
50
51
52
53
# File 'lib/order_tree/order_tree_node.rb', line 47

def deproxy
  orig = self.orig.respond_to?(:dup) ? self.orig.dup : self.orig
  if orig.respond_to? :each 
    orig.each.map! { |i| proxy?(i) ? i.deproxy : i }
  end
  orig
end

#path!Object



83
84
85
# File 'lib/order_tree/order_tree_node.rb', line 83

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