Class: SknUtils::Lists::LinkNode

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/skn_utils/lists/link_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val, anchor_node = nil, strategy = :after, mgr = nil, &cmp_key) ⇒ LinkNode

Returns a new instance of LinkNode.



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
# File 'lib/skn_utils/lists/link_node.rb', line 13

def initialize(val, anchor_node=nil, strategy=:after, mgr=nil, &cmp_key)
  @value = val
  @prev = nil
  @next = nil
  @provider = mgr
  @cmp_proc = block_given? ? cmp_key : lambda {|a| a }

  case strategy
    when :single # after logic
      if anchor_node
        @next = anchor_node.next
        anchor_node.next = self
      end
    when :before
      @prev = anchor_node.prev if anchor_node
      anchor_node.prev = self if anchor_node
      @next = anchor_node
    when :after
      @next = anchor_node.next if anchor_node
      anchor_node.next = self if anchor_node
      @prev = anchor_node
    when :circle_before
      @prev = anchor_node ? anchor_node.prev : self
      anchor_node.prev = self if anchor_node
      @next = anchor_node ? anchor_node : self
    when :circle_after
      @next = anchor_node ? anchor_node.next : self
      anchor_node.next = self if anchor_node
      @prev = anchor_node ? anchor_node : self
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/skn_utils/lists/link_node.rb', line 102

def method_missing(method, *args, &block)
  if @provider and @provider.protected_methods(true).include?(method)
    block_given? ? @provider.send(method, *args, block) :
        (args.size == 0 ?  @provider.send(method) : @provider.send(method, *args))
  else
    super
  end
end

Instance Attribute Details

#nextObject

Returns the value of attribute next.



11
12
13
# File 'lib/skn_utils/lists/link_node.rb', line 11

def next
  @next
end

#prevObject

Returns the value of attribute prev.



11
12
13
# File 'lib/skn_utils/lists/link_node.rb', line 11

def prev
  @prev
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/skn_utils/lists/link_node.rb', line 11

def value
  @value
end

Instance Method Details

#<=>(other_node) ⇒ Object

Returns

 0 if first operand equals second,
 1 if first operand is greater than the second and
-1 if first operand is less than the second.


53
54
55
56
57
58
59
60
61
# File 'lib/skn_utils/lists/link_node.rb', line 53

def <=>(other_node)
  if @cmp_proc.call(self.value) == @cmp_proc.call(other_node.value)
    0
  elsif @cmp_proc.call(self.value) > @cmp_proc.call(other_node.value)
    1
  else
    -1
  end
end

#current_nodeObject



86
87
88
# File 'lib/skn_utils/lists/link_node.rb', line 86

def current_node
  node_request(:current)
end

#first_nodeObject



80
81
82
# File 'lib/skn_utils/lists/link_node.rb', line 80

def first_node
  node_request(:first)
end

#last_nodeObject



92
93
94
# File 'lib/skn_utils/lists/link_node.rb', line 92

def last_node
  node_request(:last)
end

#match_by_value(other_value) ⇒ Object



45
46
47
# File 'lib/skn_utils/lists/link_node.rb', line 45

def match_by_value(other_value)
  @cmp_proc.call(self.value) === @cmp_proc.call(other_value)
end

#next_nodeObject



83
84
85
# File 'lib/skn_utils/lists/link_node.rb', line 83

def next_node
  node_request(:next)
end

#node_valueObject

Reverse API to Parent Linked List Class



77
78
79
# File 'lib/skn_utils/lists/link_node.rb', line 77

def node_value
  node_request.value
end

#prev_nodeObject



89
90
91
# File 'lib/skn_utils/lists/link_node.rb', line 89

def prev_node
  node_request(:prev)
end

#remove!Object

returns next node



64
65
66
67
68
69
70
# File 'lib/skn_utils/lists/link_node.rb', line 64

def remove!
  next_node = @next
  @value = nil
  @prev = nil
  @next = nil
  next_node
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/skn_utils/lists/link_node.rb', line 98

def respond_to_missing?(method, include_private=false)
  @provider && @provider.protected_methods(true).include?(method) || super
end

#to_sObject



72
73
74
# File 'lib/skn_utils/lists/link_node.rb', line 72

def to_s
  "Node with value: #{@value}"
end