Class: Dooby::List

Inherits:
Object show all
Includes:
DatesHelper
Defined in:
lib/dooby/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DatesHelper

#today_tag, #todays_date, #todays_date_tag, #tomorrow, #tomorrows_date

Constructor Details

#initialize(location) ⇒ List

Returns a new instance of List.



9
10
11
12
13
# File 'lib/dooby/list.rb', line 9

def initialize(location)
  @location = location
  @tasks = {}
  load!
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



5
6
7
# File 'lib/dooby/list.rb', line 5

def location
  @location
end

Instance Method Details

#add(task = Task.new) {|task| ... } ⇒ Object

Yields:

  • (task)


20
21
22
23
24
25
26
# File 'lib/dooby/list.rb', line 20

def add(task = Task.new)
  yield task if block_given?
  task = handle_tomorrow_tag task     
  @tasks[task.id] = task
  save!
  task
end

#all_tagsObject



118
119
120
121
122
123
124
# File 'lib/dooby/list.rb', line 118

def all_tags
  tags = []
  @tasks.each do |id, task|
    tags << task.todo.only_tags(*SPECIAL_CHARS)
  end
  tags.flatten
end

#bulk_delete!(terms) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/dooby/list.rb', line 36

def bulk_delete!(terms)
  only_tags = terms.only_tags *SPECIAL_CHARS
  matches = []
  @tasks.each do |id, task|
    delete! id if only_tags.all? { |tag| task.todo.include? tag }
  end unless only_tags.empty?
end

#current_itemObject



126
127
128
129
130
131
# File 'lib/dooby/list.rb', line 126

def current_item
  current = tasks.select do |id, task|
    task.todo.include? CURRENT_ITEM_TAG
  end
  current.flatten.last.todo.gsub(CURRENT_ITEM_TAG,'').strip unless current.blank?
end

#delete!(task_id) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/dooby/list.rb', line 28

def delete!(task_id)
  if @tasks.delete task_id
    save!
  else
    false
  end
end

#edit!(task_id) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/dooby/list.rb', line 44

def edit!(task_id)
  old_task = @tasks[task_id]
  if old_task
    t = Task.new
    yield t
    delete! task_id
    add t
  end
end

#find(what_to_show = []) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dooby/list.rb', line 62

def find (what_to_show=[])
  list = []

  if @tasks.empty?
    list = nil
  else
    case what_to_show
    when [] then
      to_delete = []
      to_add = []
      @tasks.each do |id, task|
        
        #if item has {today's date} replace it with #today
        if task.todo.include? todays_date_tag
          task_with_today_tag = task.dup
          task_with_today_tag.todo.gsub!(todays_date_tag, TODAY_TAG)
          to_add << task_with_today_tag
          to_delete << id
        else
          list << TASK_ROW_TEMPLATE.call(task)
        end
      end
      
      to_delete.each { |task_id| delete!(task_id) }
      to_add.each do |task|
        add task
        list << TASK_ROW_TEMPLATE.call(task)
      end
      
    when *SPECIAL_CHARS then
      @tasks.each do |id, task|
        task.todo.gsub(/(#{what_to_show}\w+)/).each do |tag|
          color = SPECIAL_CHAR_COLORS[tag.first_char]
          colorized = tag.send color
          list << colorized unless list.include? colorized
        end
      end
    else #user has enter search terms
      if what_to_show.any? { |s| s.include? tomorrow }
        what_to_show << tomorrows_date
        what_to_show.delete(tomorrow)
        what_to_show.delete(TOMORROW_TAG)
      end
      @tasks.each do |id, task|
        if what_to_show.all? { |term| task.todo.include? term }
          list << TASK_ROW_TEMPLATE.call(task)
        end
      end
      
      list = nil if list.empty?
    end
  end
        
  list
end

#flush!Object



15
16
17
18
# File 'lib/dooby/list.rb', line 15

def flush!
  @tasks = {}
  save!
end

#tasksObject



58
59
60
# File 'lib/dooby/list.rb', line 58

def tasks
  @tasks.dup
end

#tasks?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/dooby/list.rb', line 54

def tasks?
  !@tasks.empty?
end