Class: Syctask::Task

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/syctask/task.rb

Overview

A Task is the basic element of the task list and holds all information about a task.

Constant Summary collapse

FIELDS =

The fields that can be set for a task

%w[title description
follow_up due_date prio
note tags]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, title, id) ⇒ Task

Creates a new task. If the options contain a note than the current date and time is added.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/syctask/task.rb', line 49

def initialize(options = {}, title, id)
  @creation_date = Time.now.strftime('%Y-%m-%d - %H:%M:%S')
  @title = title
  @options = options
  if @options[:note]
    @options[:note] =
      "#{@creation_date}\n#{@options[:note]}\n"
  end
  if @options[:follow_up] or @options[:due_date]
    @duration = 2 * 15 * 60
    @remaining = 2 * 15 * 60
  else
    @duration = 0
    @remaining = 0
  end
  @id = id
end

Instance Attribute Details

#creation_dateObject (readonly)

Creation date



39
40
41
# File 'lib/syctask/task.rb', line 39

def creation_date
  @creation_date
end

#dirObject

Directory where the file of the task is located



45
46
47
# File 'lib/syctask/task.rb', line 45

def dir
  @dir
end

#done_dateObject (readonly)

Done date



43
44
45
# File 'lib/syctask/task.rb', line 43

def done_date
  @done_date
end

#durationObject (readonly)

Duration specifies the planned time for processing the task



33
34
35
# File 'lib/syctask/task.rb', line 33

def duration
  @duration
end

#idObject (readonly)

ID of the task



31
32
33
# File 'lib/syctask/task.rb', line 31

def id
  @id
end

#lead_timeObject (readonly)

Lead time is the time this task has been processed



37
38
39
# File 'lib/syctask/task.rb', line 37

def lead_time
  @lead_time
end

#optionsObject

Holds the options of the task. Options are

  • description - additional information about the task

  • follow_up - follow-up date of the task

  • due_date - due date of the task

  • prio - priority of the task

  • note - information about the progress or state of the task

  • tags - can be used to search for tasks that belong to a certain

    category
    


27
28
29
# File 'lib/syctask/task.rb', line 27

def options
  @options
end

#remainingObject (readonly)

Remaining time is the duration subtracted by the lead time since last plan



35
36
37
# File 'lib/syctask/task.rb', line 35

def remaining
  @remaining
end

#titleObject (readonly)

Title of the class



29
30
31
# File 'lib/syctask/task.rb', line 29

def title
  @title
end

#update_dateObject (readonly)

Update date



41
42
43
# File 'lib/syctask/task.rb', line 41

def update_date
  @update_date
end

Instance Method Details

#<=>(other) ⇒ Object

Compares this Task to the other task and compares them regarding the ID and the dir. If ID is equal then dir is compared



75
76
77
78
79
80
81
82
# File 'lib/syctask/task.rb', line 75

def <=>(other)
  id_compare = @id.to_i <=> other.id.to_i
  if id_compare == 0
    @dir <=> other.dir
  else
    id_compare
  end
end

#==(other) ⇒ Object

Compares this task with another task regarding id and dir. If both are equal true is returned otherwise false



69
70
71
# File 'lib/syctask/task.rb', line 69

def ==(other)
  @id == other.id and @dir == other.dir
end

#done(note = '') ⇒ Object

Marks the task as done. When done than the done date is set. Optionally a note can be provided.



143
144
145
146
147
# File 'lib/syctask/task.rb', line 143

def done(note = '')
  @done_date = Time.now.strftime('%Y-%m-%d - %H:%M:%S')
  options[:note] = "#{@done_date}\n#{note}\n#{@options[:note]}" if note
  Syctask.log_task('done', self)
end

#done?Boolean

Checks if this task is done. Returns true if done otherwise false

Returns:

  • (Boolean)


150
151
152
# File 'lib/syctask/task.rb', line 150

def done?
  !@done_date.nil?
end

#matches?(filter = {}) ⇒ Boolean

Compares the provided elements in the filter with the correspondent elements in the task. When all comparissons match than true is returned. If one comparisson does not match false is returned. If filter is empty than true is returned. The values can be compared regarding <, =, > or whether the task’s value is part of a list of provided values. It is also possible to provide a regex as a filter. Following comparissons are available Value Compare :title regex :description regex :id contains, <|=|> no operator same as = :prio contains, <|=|> no operator same as = :tags contains, regex :follow_up <|=|> :due <|=|>

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/syctask/task.rb', line 186

def matches?(filter = {})
  return true if filter.empty?

  evaluator = Evaluator.new
  filter.each do |key, value|
    matches = false
    case key
    when :title, :t
      matches = evaluator.matches?(@title, value)
    when :description
      matches = evaluator.matches?(@options[:description], value)
    when :id, :i, 'id', 'i'
      matches = (evaluator.includes?(@id, value) or
                 evaluator.compare_numbers(@id, value))
    when :prio, :p
      matches = (evaluator.includes?(@options[:prio], value) or
                 evaluator.compare(@options[:prio], value))
    when :tags
      matches = evaluator.matches?(@options[:tags], value)
    when :follow_up, :f, :d, :due_date
      matches = evaluator.compare_dates(@options[key], value)
    end
    return false unless matches
  end
  true
end

Prints the task as a CSV



220
221
222
# File 'lib/syctask/task.rb', line 220

def print_csv
  STDOUT.puts(csv_string)
end

Prints the task in a formatted way eather all values when long is true or only id, title, prio, follow-up and due date.



215
216
217
# File 'lib/syctask/task.rb', line 215

def print_pretty(long = false)
  pretty_string(long)
end

#set_duration(duration) ⇒ Object

Sets the duration that this task is planned for processing. Assigns to remaining the duration time



121
122
123
124
# File 'lib/syctask/task.rb', line 121

def set_duration(duration)
  @duration = duration
  @remaining = duration
end

#today?Boolean

Checks if task is scheduled for today. Returns true if follow up or due date is today otherwise false.

Returns:

  • (Boolean)


156
157
158
159
160
161
# File 'lib/syctask/task.rb', line 156

def today?
  evaluator = Evaluator.new
  today = Time.now.strftime('%Y-%m-%d')
  evaluator.compare_dates(@options[:follow_up], today) or \
    evaluator.compare_dates(@options[:due_date], today)
end

#tracked?Boolean

Checks whether the task is currently tracked. Returns true if so otherwise false

Returns:

  • (Boolean)


165
166
167
168
169
# File 'lib/syctask/task.rb', line 165

def tracked?
  tracker = Syctask::TaskTracker.new
  task = tracker.tracked_task
  task.nil? ? false : task == self
end

#update(options) ⇒ Object

Updates the task with new values. Except for note and tags which are supplemented with the new values and not overridden.



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
# File 'lib/syctask/task.rb', line 86

def update(options)
  @update_date = Time.now.strftime('%Y-%m-%d - %H:%M:%S')
  if options[:duration]
    set_duration(options.delete(:duration).to_i * 15 * 60)
  elsif options[:follow_up] or options[:due_date]
    set_duration(2 * 15 * 60) if @duration.nil?
  end
  options.keys.each do |key|
    new_value = options[key]

    case key
    when :note
      new_value = "#{@update_date}\n#{new_value}\n#{@options[key]}"
    when :tags
      unless @options[key].nil?
        new_value = if @options[key].include? new_value
                      @options[key]
                    else
                      "#{@options[key]},#{new_value}"
                    end
      end
    end

    @options[key] = new_value
  end
end

#update?Boolean

Checks whether this task has been updated. Returns true if updated otherwise false

Returns:

  • (Boolean)


115
116
117
# File 'lib/syctask/task.rb', line 115

def update?
  !@updated_date.nil?
end

#update_lead_time(lead_time) ⇒ Object

Updates the lead time. Adds the lead time to @lead_time and calculates



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/syctask/task.rb', line 128

def update_lead_time(lead_time)
  if @lead_time
    @lead_time += lead_time
  else
    @lead_time = lead_time
  end
  if @remaining
    @remaining -= lead_time
  else
    @remaining = @duration.to_i - lead_time
  end
end