Class: Taskinator::Tasks

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/taskinator/tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first = nil) ⇒ Tasks

Returns a new instance of Tasks.



13
14
15
16
# File 'lib/taskinator/tasks.rb', line 13

def initialize(first=nil)
  @count = 0
  add(first) if first
end

Instance Attribute Details

#countObject (readonly) Also known as: length

Returns the value of attribute count.



10
11
12
# File 'lib/taskinator/tasks.rb', line 10

def count
  @count
end

#headObject (readonly) Also known as: first

implements a linked list, where each task references the next task



7
8
9
# File 'lib/taskinator/tasks.rb', line 7

def head
  @head
end

Instance Method Details

#add(task) ⇒ Object Also known as: <<, push



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/taskinator/tasks.rb', line 24

def add(task)
  if @head.nil?
    @head = task
    @count = 1
  else
    current = @head
    while current.next
      current = current.next
    end
    current.next = task
    @count += 1
  end
  task
end

#attach(task, count) ⇒ Object



18
19
20
21
22
# File 'lib/taskinator/tasks.rb', line 18

def attach(task, count)
  @head = task
  @count = count
  task
end

#each(&block) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/taskinator/tasks.rb', line 46

def each(&block)
  return to_enum(__method__) unless block_given?

  current = @head
  while current
    yield current
    current = current.next
  end
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/taskinator/tasks.rb', line 42

def empty?
  @head.nil?
end

#inspectObject



56
57
58
# File 'lib/taskinator/tasks.rb', line 56

def inspect
  %([#{collect(&:inspect).join(', ')}])
end