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::COLORED_REGEXP
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
-
#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.
-
#id ⇒ String
Generate a hash that represents the entry.
-
#initialize(date, title, section, note = 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?, #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, #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) ⇒ Item
Initialize an item with date, title, section, and optional note
35 36 37 38 39 40 |
# File 'lib/doing/item/item.rb', line 35 def initialize(date, title, section, note = nil) @date = date.is_a?(Time) ? date : Time.parse(date) @title = title @section = section @note = Note.new(note) 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 |
#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
124 125 126 |
# File 'lib/doing/item/item.rb', line 124 def clone Marshal.load(Marshal.dump(self)) end |
#equal?(other, match_section: false) ⇒ Boolean
Test for equality between items
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/doing/item/item.rb', line 57 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 |
#id ⇒ String
Generate a hash that represents the entry
45 46 47 |
# File 'lib/doing/item/item.rb', line 45 def id @id ||= (@date.to_s + @title + @section).hash end |
#move_to(new_section, label: true, log: true) ⇒ Object
Move item from current section to destination section
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/doing/item/item.rb', line 80 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/doing/item/item.rb', line 102 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
93 94 95 |
# File 'lib/doing/item/item.rb', line 93 def to_s "\t- #{@date.strftime('%Y-%m-%d %H:%M')} | #{@title}#{@note.good? ? "\n#{@note}" : ''}" end |