Class: SknUtils::Lists::LinkNode

Inherits:
Object
  • Object
show all
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) ⇒ LinkNode

Returns a new instance of LinkNode.



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

def initialize(val, anchor_node=nil, strategy=:after)
  @value = val
  @prev = nil
  @next = nil

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

Instance Attribute Details

#nextObject

Returns the value of attribute next.



9
10
11
# File 'lib/skn_utils/lists/link_node.rb', line 9

def next
  @next
end

#prevObject

Returns the value of attribute prev.



9
10
11
# File 'lib/skn_utils/lists/link_node.rb', line 9

def prev
  @prev
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/skn_utils/lists/link_node.rb', line 9

def value
  @value
end

Instance Method Details

#match_by_value(other_value) ⇒ Object



41
42
43
# File 'lib/skn_utils/lists/link_node.rb', line 41

def match_by_value(other_value)
  self.value === other_value
end

#remove!Object

returns next node



46
47
48
49
50
51
52
# File 'lib/skn_utils/lists/link_node.rb', line 46

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

#to_sObject



54
55
56
# File 'lib/skn_utils/lists/link_node.rb', line 54

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