Module: Rtasklib::Controller

Extended by:
Controller
Included in:
Controller, TaskWarrior
Defined in:
lib/rtasklib/controller.rb

Overview

Accessed through the main TW, which includes this module, e.g. tw.all

Ideally should only be the well documented public, user-facing methods. We’re getting there.

By convention bang methods modify the task database, and non-bang read from the database, e.g. ‘Controller#all` vs `Controller#modify!`

XXX: depends on @override_a currently, which isn’t great.

Instance Method Summary collapse

Instance Method Details

#add!(description, tags: nil, dom: nil) ⇒ Object

Add a single task to the database w/required description and optional tags and dom queries (e.g. project:Work)

Parameters:

  • the required desc of the task

  • (defaults to: nil)
  • (defaults to: nil)

API:

  • public



130
131
132
133
134
135
136
# File 'lib/rtasklib/controller.rb', line 130

def add! description, tags: nil, dom: nil
  f = Helpers.filter(tags: tags, dom: dom)
  d = Helpers.wrap_string(description)
  Execute.task_popen3(*override_a, "add", d, f) do |i, o, e, t|
    return t.value
  end
end

#all(active: true) ⇒ Array<Models::TaskModel>

Retrieves the current task list from the TW database

Parameters:

  • (defaults to: true)

    return only pending & waiting tasks

Returns:

API:

  • public



23
24
25
26
27
28
29
30
31
32
# File 'lib/rtasklib/controller.rb', line 23

def all active: true
  all = []
  f = Helpers.pending_or_waiting(active)
  Execute.task_popen3(*override_a, f, "export") do |i, o, e, t|
    all = MultiJson.load(o.read).map do |x|
      Rtasklib::Models::TaskModel.new(x)
    end
  end
  return all
end

#count(ids: nil, tags: nil, dom: nil) ⇒ Object Also known as: size, length

Add a single task to the database

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

API:

  • public



63
64
65
66
67
68
# File 'lib/rtasklib/controller.rb', line 63

def count ids: nil, tags: nil, dom: nil
  f = Helpers.filter(ids: ids, tags: tags, dom: dom)
  Execute.task_popen3(*@override_a, f, "count") do |i, o, e, t|
    return Integer(o.read)
  end
end

#create_uda!(name, type: "string", label: nil, values: nil, default: nil, urgency: nil) ⇒ Boolean

Add a UDA to the users config/database

Parameters:

  • (defaults to: "string")
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

Returns:

  • success

API:

  • public



278
279
280
281
282
283
284
285
286
287
# File 'lib/rtasklib/controller.rb', line 278

def create_uda! name, type: "string", label: nil, values: nil,
               default: nil, urgency: nil
  label = name if label.nil?

  update_config("uda.#{name}.type",  type)
  update_config("uda.#{name}.label", label)
  update_config("uda.#{name}.values",  values)  unless values.nil?
  update_config("uda.#{name}.default", default) unless default.nil?
  update_config("uda.#{name}.urgency", urgency) unless urgency.nil?
end

#delete!(ids: nil, tags: nil, dom: nil) ⇒ Object

Returns false if filter is blank.

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

API:

  • public



180
181
182
183
184
185
186
187
# File 'lib/rtasklib/controller.rb', line 180

def delete! ids: nil, tags: nil, dom: nil
  f = Helpers.filter(ids: ids, tags: tags, dom: dom)
  return false if f.blank?

  Execute.task_popen3(*override_a, f, "delete") do |i, o, e, t|
    return t.value
  end
end

#done!(ids: nil, tags: nil, dom: nil) ⇒ Object

Finishes the filtered tasks Returns false if filter (ids:, tags:, dom:) is blank.

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

API:

  • public



165
166
167
168
169
170
171
172
# File 'lib/rtasklib/controller.rb', line 165

def done! ids: nil, tags: nil, dom: nil
  f = Helpers.filter(ids: ids, tags: tags, dom: dom)
  return false if f.blank?

  Execute.task_popen3(*override_a, f, "done") do |i, o, e, t|
    return t.value
  end
end

#get_rcTaskrc

Calls ‘task _show` with initial overrides returns a Taskrc object of the result

Returns:

API:

  • public



77
78
79
80
81
82
83
# File 'lib/rtasklib/controller.rb', line 77

def get_rc
  res = []
  Execute.task_popen3(*@override_a, "_show") do |i, o, e, t|
    res = o.read.each_line.map { |l| l.chomp }
  end
  Taskrc.new(res, :array)
end

#get_uda_namesArray<String>

Retrieve an array of the uda names

Returns:

API:

  • public



249
250
251
252
253
# File 'lib/rtasklib/controller.rb', line 249

def get_uda_names
  Execute.task_popen3(*@override_a, "_udas") do |i, o, e, t|
    return o.read.each_line.map { |l| l.chomp }
  end
end

#get_udasHash{Symbol=>Hash}

Retrieves a hash of hashes with info about the UDAs currently available

Returns:

API:

  • public



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rtasklib/controller.rb', line 203

def get_udas
  udas = {}
  taskrc.config.attributes
    .select { |attr, val| Helpers.uda_attr? attr }
    .sort
    .chunk  { |attr, val| Helpers.arbitrary_attr attr }
    .each do |attr, arr|
      uda = arr.map do |pair|
        [Helpers.deep_attr(pair[0]), pair[1]]
      end
      udas[attr.to_sym] = Hash[uda]
    end
    return udas
end

#get_versionString

Calls ‘task _version` and returns the result

Returns:

API:

  • public



89
90
91
92
93
94
95
# File 'lib/rtasklib/controller.rb', line 89

def get_version
  version = nil
  Execute.task_popen3("_version") do |i, o, e, t|
    version = Helpers.to_gem_version(o.read.chomp)
  end
  version
end

#modify!(attr, val, ids: nil, tags: nil, dom: nil) ⇒ Object

Modify a set of task the match the input filter with a single attr/value pair. Returns false if filter (ids:, tags:, dom:) is blank.

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

API:

  • public



148
149
150
151
152
153
154
155
156
# File 'lib/rtasklib/controller.rb', line 148

def modify! attr, val, ids: nil, tags: nil, dom: nil
  f = Helpers.filter(ids: ids, tags: tags, dom: dom)
  return false if f.blank?

  query = "#{f} modify #{attr} #{val}"
  Execute.task_popen3(*override_a, query) do |i, o, e, t|
    return t.value
  end
end

#some(ids: nil, tags: nil, dom: nil) ⇒ Array<Models::TaskModel>

Retrieves the current task list filtered by id, tag, or a dom query

Examples:

filter by an array of ids

tw.some(ids: [1..2, 5])

filter by tags

tw.some(tags: ["+school", "or", "-work"]

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

Returns:

API:

  • public



46
47
48
49
50
51
52
53
54
55
# File 'lib/rtasklib/controller.rb', line 46

def some ids: nil, tags: nil, dom: nil
  some = []
  f = Helpers.filter(ids: ids, tags: tags, dom: dom)
  Execute.task_popen3(*@override_a, f, "export") do |i, o, e, t|
    some = MultiJson.load(o.read).map do |x|
      Rtasklib::Models::TaskModel.new(x)
    end
  end
  return some
end

#start!(ids: nil, tags: nil, dom: nil) ⇒ Object

Mark the filter of tasks as started Returns false if filter (ids:, tags:, dom:) is blank.

API:

  • public



101
102
103
104
105
106
107
108
# File 'lib/rtasklib/controller.rb', line 101

def start! ids: nil, tags: nil, dom: nil
  f = Helpers.filter(ids: ids, tags: tags, dom: dom)
  return false if f.blank?

  Execute.task_popen3(*@override_a, f, "start") do |i, o, e, t|
    return t.value
  end
end

#stop!(ids: nil, tags: nil, dom: nil) ⇒ Object

Mark the filter of tasks as stopped Returns false if filter (ids:, tags:, dom:) is blank.

API:

  • public



114
115
116
117
118
119
120
121
# File 'lib/rtasklib/controller.rb', line 114

def stop! ids: nil, tags: nil, dom: nil
  f = Helpers.filter(ids: ids, tags: tags, dom: dom)
  return false if f.blank?

  Execute.task_popen3(*@override_a, f, "stop") do |i, o, e, t|
    return t.value
  end
end

#uda_exists?(uda_name) ⇒ Boolean

Checks if a given uda exists in the current task database

Parameters:

  • the uda name to check for

Returns:

  • whether it matches or not

API:

  • public



260
261
262
263
264
265
266
# File 'lib/rtasklib/controller.rb', line 260

def uda_exists? uda_name
  if get_udas.any? { |uda| uda == uda_name }
    true
  else
    false
  end
end

#undo!Object

Directly call ‘task undo`, which only applies to edits to the task db not configuration changes

API:

  • public



193
194
195
196
197
# File 'lib/rtasklib/controller.rb', line 193

def undo!
  Execute.task_popen3(*override_a, "undo") do |i, o, e, t|
    return t.value
  end
end

#update_config!(attr, val) ⇒ Object

Update a configuration variable in the .taskrc

Parameters:

API:

  • public



223
224
225
226
227
# File 'lib/rtasklib/controller.rb', line 223

def update_config! attr, val
  Execute.task_popen3(*override_a, "config #{attr} #{val}") do |i, o, e, t|
    return t.value
  end
end