Method: Doing::WWID#stop_start

Defined in:
lib/doing/wwid/modify.rb

#stop_start(target_tag, opt) ⇒ Object

Accepts one tag and the raw text of a new item if the passed tag is on any item, it's replaced with @done. if new_item is not nil, it's tagged with the passed tag and inserted. This is for use where only one instance of a given tag should exist (@meanwhile)

Options Hash (opt):

  • :section (String)

    target section

  • :archive (Boolean)

    archive old item

  • :back (Date)

    backdate new item

  • :new_item (String)

    content to use for new item

  • :note (Array)

    note content for new item



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/doing/wwid/modify.rb', line 341

def stop_start(target_tag, opt)
  opt ||= {}
  tag = target_tag.dup
  opt[:section] ||= Doing.setting('current_section')
  opt[:archive] ||= false
  opt[:back] ||= Time.now
  opt[:new_item] ||= false
  opt[:note] ||= false

  opt[:section] = guess_section(opt[:section])

  tag.sub!(/^@/, '')

  found_items = 0

  @content.each_with_index do |item, i|
    old_item = i.clone
    next unless item.section == opt[:section] || opt[:section] =~ /all/i

    next unless item.title =~ /@#{tag}/

    item.title.add_tags!([tag, 'done'], remove: true)
    item.tag('done', value: opt[:back].strftime('%F %R'))

    found_items += 1

    if opt[:archive] && opt[:section] != 'Archive'
      item.title = item.title.sub(/(?:@from\(.*?\))?(.*)$/, "\\1 @from(#{item.section})")
      item.move_to('Archive', label: false, log: false)
      logger.count(:completed_archived)
      logger.info('Completed/archived:', item.title)
    else
      logger.count(:completed)
      logger.info('Completed:', item.title)
    end
    Hooks.trigger :post_entry_updated, self, item, old_item
  end


  logger.debug('Skipped:', "No active @#{tag} tasks found.") if found_items.zero?

  if opt[:new_item]
    date, title, note = format_input(opt[:new_item])
    opt[:back] = date unless date.nil?
    note.add(opt[:note]) if opt[:note]
    title.tag!(tag)
    add_item(title.cap_first, opt[:section], { note: note, back: opt[:back] })
  end

  write(@doing_file)
end