Class: Fr::SZipper::HeadCursor

Inherits:
AbstractCursor show all
Defined in:
lib/fr/szipper/head_cursor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractCursor

#empty?, #next, #tail, #truncate

Methods included from Enumerable

#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each, #entries, #filter, #filter_, #first, #flat_map, #foldl, #grep, #group_by, #include?, #index, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #partition, #reduce, #reject, #sort, #take, #take_while, #zip

Constructor Details

#initialize(node) ⇒ HeadCursor

Returns a new instance of HeadCursor.



8
9
10
# File 'lib/fr/szipper/head_cursor.rb', line 8

def initialize(node)
  @node = node
end

Instance Attribute Details

#node#next, #tail? (readonly)

Returns:



6
7
8
# File 'lib/fr/szipper/head_cursor.rb', line 6

def node
  @node
end

Instance Method Details

#append(node) ⇒ Object



37
38
39
# File 'lib/fr/szipper/head_cursor.rb', line 37

def append(node)
  EditedCursor.new(node.copy(next: @node.next), self)
end

#deleteObject



57
58
59
60
61
62
63
# File 'lib/fr/szipper/head_cursor.rb', line 57

def delete
  if tail?
    HeadPrevCursor.new(nil)
  else
    HeadCursor.new(@node.next)
  end
end

#headObject



24
25
26
# File 'lib/fr/szipper/head_cursor.rb', line 24

def head
  self
end

#head?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/fr/szipper/head_cursor.rb', line 16

def head?
  true
end

#inspectObject



65
66
67
# File 'lib/fr/szipper/head_cursor.rb', line 65

def inspect
  "Cursor([#{@node.inspect}, ...])"
end

#positionObject



12
13
14
# File 'lib/fr/szipper/head_cursor.rb', line 12

def position
  0
end

#prepend(node) ⇒ Object



41
42
43
# File 'lib/fr/szipper/head_cursor.rb', line 41

def prepend(node)
  HeadCursor.new(node.copy(next: @node))
end

#prev(phantom = false) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/fr/szipper/head_cursor.rb', line 28

def prev(phantom = false)
  if phantom
    HeadPrevCursor.new(@node)
  else
    raise Errors::ZipperError,
      "head node has no prev"
  end
end

#replace(node) ⇒ Object



53
54
55
# File 'lib/fr/szipper/head_cursor.rb', line 53

def replace(node)
  HeadCursor.new(node)
end

#tail?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/fr/szipper/head_cursor.rb', line 20

def tail?
  @node.tail?
end

#update(node = nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/fr/szipper/head_cursor.rb', line 45

def update(node = nil)
  if node.nil?
    HeadCursor.new(yield(@node).copy(next: @node.next))
  else
    HeadCursor.new(node.copy(next: @node.next))
  end
end