Class: Doing::Item
- 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
-
#date ⇒ Object
Returns the value of attribute date.
-
#id ⇒ Object
Returns the value of attribute id.
-
#note ⇒ Object
Returns the value of attribute note.
-
#section ⇒ Object
Returns the value of attribute section.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #clone ⇒ Object
-
#equal?(other, match_section: false) ⇒ Boolean
Test for equality between items.
- #gen_id ⇒ Object
-
#initialize(date, title, section, note = nil, id = nil) ⇒ Item
constructor
Initialize an item with date, title, section, and optional note.
-
#move_to(new_section, label: true, log: true) ⇒ Object
Move item from current section to destination section.
-
#to_pretty(elements: %i[date title section])) ⇒ Object
outputs a colored string with relative date and highlighted tags.
-
#to_s ⇒ Object
outputs item in Doing file format, including leading tab.
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
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
#date ⇒ Object
Returns the value of attribute date.
18 19 20 |
# File 'lib/doing/item/item.rb', line 18 def date @date end |
#id ⇒ Object
Returns the value of attribute id.
18 19 20 |
# File 'lib/doing/item/item.rb', line 18 def id @id end |
#note ⇒ Object
Returns the value of attribute note.
18 19 20 |
# File 'lib/doing/item/item.rb', line 18 def note @note end |
#section ⇒ Object
Returns the value of attribute section.
18 19 20 |
# File 'lib/doing/item/item.rb', line 18 def section @section end |
#title ⇒ Object
Returns the value of attribute title.
18 19 20 |
# File 'lib/doing/item/item.rb', line 18 def title @title end |
Instance Method Details
#clone ⇒ Object
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
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_id ⇒ Object
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
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
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.('cyan') end end output.join(' ') end |
#to_s ⇒ Object
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 |