Class: Doing::Item

Inherits:
Object show all
Includes:
Color, ItemDates, ItemQuery, ItemState, ItemTags
Defined in:
lib/doing/item/item.rb

Overview

This class describes a single WWID item

Constant Summary

Constants included from Color

Color::ATTRIBUTES, Color::ATTRIBUTE_NAMES, Color::ESCAPE_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Color

#attributes, coloring?, #rgb, #support?, template, #uncolor

Methods included from ItemTags

#tag, #tag_array, #tags, #tags_with_values

Methods included from ItemState

#finished?, #should_finish?, #should_time?, #unfinished?

Methods included from ItemQuery

#highlight_search, #ignore_case, #keep_item?, #search, #tag_values?, #tags?

Methods included from ItemDates

#calculate_end_date, #duration, #end_date, #expand_date_tags, #interval, #overlapping_time?, #same_time?

Constructor Details

#initialize(date, title, section, note = nil, id = nil) ⇒ Item

Initialize an item with date, title, section, and optional note

Parameters:

  • date (Time)

    The item's start date

  • title (String)

    The title

  • section (String)

    The section to which the item belongs

  • note (Array or String) (defaults to: nil)

    The note (optional)

  • id (defaults to: nil)

    MD5 identifier



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

def initialize(date, title, section, note = nil, id = nil)
  @date = date.is_a?(Time) ? date : Time.parse(date)
  @title = title
  @section = section
  @note = Note.new(note)
  @id = id&.valid_id? ? id.strip : gen_id
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



18
19
20
# File 'lib/doing/item/item.rb', line 18

def date
  @date
end

#idObject

Returns the value of attribute id.



18
19
20
# File 'lib/doing/item/item.rb', line 18

def id
  @id
end

#noteObject

Returns the value of attribute note.



18
19
20
# File 'lib/doing/item/item.rb', line 18

def note
  @note
end

#sectionObject

Returns the value of attribute section.



18
19
20
# File 'lib/doing/item/item.rb', line 18

def section
  @section
end

#titleObject

Returns the value of attribute title.



18
19
20
# File 'lib/doing/item/item.rb', line 18

def title
  @title
end

Instance Method Details

#cloneObject



123
124
125
# File 'lib/doing/item/item.rb', line 123

def clone
  Marshal.load(Marshal.dump(self))
end

#equal?(other, match_section: false) ⇒ Boolean

Test for equality between items

Parameters:

  • other (Item)

    The other item

  • match_section (Boolean) (defaults to: false)

    If true, require item sections to match

Returns:

  • (Boolean)

    is equal?



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/doing/item/item.rb', line 56

def equal?(other, match_section: false)
  return false if @title.strip != other.title.strip

  return false if @date != other.date

  return false unless @note.equal?(other.note)

  return false if match_section && !@section.equal?(other.section)

  true
end

#gen_idObject



44
45
46
# File 'lib/doing/item/item.rb', line 44

def gen_id
  Digest::MD5.hexdigest(to_s)
end

#move_to(new_section, label: true, log: true) ⇒ Object

Move item from current section to destination section

Parameters:

  • new_section (String)

    The destination section

  • label (Boolean) (defaults to: true)

    add @from(original section) tag

  • log (Boolean) (defaults to: true)

    log this action

Returns:

  • nothing



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/doing/item/item.rb', line 79

def move_to(new_section, label: true, log: true)
  from = @section

  tag('from', rename_to: 'from', value: from, force: true) if label
  @section = new_section

  Doing.logger.count(@section == 'Archive' ? :archived : :moved) if log
  Doing.logger.debug("#{@section == 'Archive' ? 'Archived' : 'Moved'}:",
                     "#{@title.trunc(60)} from #{from} to #{@section}")
  self
end

#to_pretty(elements: %i[date title section])) ⇒ Object

outputs a colored string with relative date and highlighted tags

Returns:

  • Pretty representation of the object.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/doing/item/item.rb', line 101

def to_pretty(elements: %i[date title section])
  output = []
  elements.each do |e|
    case e
    when :date
      output << format('%13s |', @date.relative_date).cyan
    when :section
      output << "#{magenta}(#{white(@section)}#{magenta})"
    when :title
      output << @title.white.highlight_tags('cyan')
    end
  end

  output.join(' ')
end

#to_sObject

outputs item in Doing file format, including leading tab



92
93
94
# File 'lib/doing/item/item.rb', line 92

def to_s
  "\t- #{@date.strftime('%Y-%m-%d %H:%M')} | #{@title} <#{@id}>#{@note.good? ? "\n#{@note}" : ''}"
end