Module: Doing::ItemTags

Included in:
Item
Defined in:
lib/doing/item/tags.rb

Overview

A Doing entry

Instance Method Summary collapse

Instance Method Details

#tag(tags, **options) ⇒ Object

Add (or remove) tags from the title of the item

Parameters:

  • tags (Array)

    The tags to apply

  • options

    Additional options

Options Hash (**options):

  • :date (Boolean)

    Include timestamp?

  • :single (Boolean)

    Log as a single change?

  • :value (String)

    A value to include as @tag(value)

  • :remove (Boolean)

    if true remove instead of adding

  • :rename_to (String)

    if not nil, rename target tag to this tag name

  • :regex (Boolean)

    treat target tag string as regex pattern

  • :force (Boolean)

    with rename_to, add tag if it doesn't exist



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/doing/item/tags.rb', line 20

def tag(tags, **options)
  added = []
  removed = []

  date = options.fetch(:date, false)
  options[:value] ||= date ? Time.now.strftime('%F %R') : nil
  options.delete(:date)

  single = options.fetch(:single, false)
  options.delete(:single)

  tags = tags.to_tags if tags.is_a? ::String

  remove = options.fetch(:remove, false)
  tags.each do |tag|
    if tag =~ /^(\S+)\((.*?)\)$/
      m = Regexp.last_match
      tag = m[1]
      options[:value] ||= m[2]
    end

    bool = remove ? :and : :not
    if tags?(tag, bool) || options[:value]
      @title = @title.tag(tag, **options).strip
      remove ? removed.push(tag) : added.push(tag)
    end
  end

  Doing.logger.log_change(tags_added: added, tags_removed: removed, count: 1, item: self, single: single)

  self
end

#tag_arrayArray

convert tags on item to an array with @ symbols removed

Returns:

  • (Array)

    array of tags



77
78
79
# File 'lib/doing/item/tags.rb', line 77

def tag_array
  tags.tags_to_array
end

#tagsArray

Get a list of tags on the item

Returns:

  • (Array)

    array of tags (no values)



58
59
60
# File 'lib/doing/item/tags.rb', line 58

def tags
  @title.scan(/(?<= |\A)@([^\s(]+)/).map { |tag| tag[0] }.sort.uniq
end

#tags_with_valuesArray<Array>

Return all tags including parenthetical values

Returns:

  • (Array<Array>)

    Array of array pairs, [[tag1, value], [tag2, value]]



68
69
70
# File 'lib/doing/item/tags.rb', line 68

def tags_with_values
  @title.scan(/(?<= |\A)@([^\s(]+)(?:\((.*?)\))?/).map { |tag| [tag[0], tag[1]] }.sort.uniq
end