Class: Doing::Item
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
- #calculate_end_date(opt) ⇒ Object
-
#duration ⇒ Object
If the entry doesn't have a @done date, return the elapsed time.
-
#end_date ⇒ Time
Get the value of the item's @done tag.
-
#equal?(other) ⇒ Boolean
Test for equality between items.
-
#expand_date_tags(additional_tags = nil) ⇒ Object
Updates the title of the Item by expanding natural language dates within configured date tags (tags whose value is expected to be a date).
- #highlight_search(search, distance: nil, negate: false, case_type: nil) ⇒ Object
-
#id ⇒ String
Generate a hash that represents the entry.
-
#ignore_case(search, case_type) ⇒ Boolean
Determine if case should be ignored for searches.
-
#initialize(date, title, section, note = nil) ⇒ Item
constructor
Initialize an item with date, title, section, and optional note.
-
#interval ⇒ Object
Get the difference between the item's start date and the value of its @done tag (if present).
-
#move_to(new_section, label: true, log: true) ⇒ Object
Move item from current section to destination section.
-
#overlapping_time?(item_b) ⇒ Boolean
Test if the interval between start date and @done value overlaps with another item's.
-
#same_time?(item_b) ⇒ Boolean
Test if two items occur at the same time (same start date and equal duration).
-
#search(search, distance: nil, negate: false, case_type: nil) ⇒ Boolean
Test if item matches search string.
-
#should_finish? ⇒ Boolean
Test if item is included in never_finish config and thus should not receive a @done tag.
-
#should_time? ⇒ Boolean
Test if item is included in never_time config and thus should not receive a date on the @done tag.
-
#tag(tags, **options) ⇒ Object
Add (or remove) tags from the title of the item.
-
#tag_array ⇒ Array
convert tags on item to an array with @ symbols removed.
-
#tag_values?(queries, bool = :and, negate: false) ⇒ Boolean
Test if item matches tag values.
-
#tags ⇒ Array
Get a list of tags on the item.
-
#tags?(tags, bool = :and, negate: false) ⇒ Boolean
Test if item contains tag(s).
-
#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?, #uncolor
Constructor Details
#initialize(date, title, section, note = nil) ⇒ Item
Initialize an item with date, title, section, and optional note
25 26 27 28 29 30 |
# File 'lib/doing/item.rb', line 25 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.
8 9 10 |
# File 'lib/doing/item.rb', line 8 def date @date end |
#note ⇒ Object
Returns the value of attribute note.
8 9 10 |
# File 'lib/doing/item.rb', line 8 def note @note end |
#section ⇒ Object
Returns the value of attribute section.
8 9 10 |
# File 'lib/doing/item.rb', line 8 def section @section end |
#title ⇒ Object
Returns the value of attribute title.
8 9 10 |
# File 'lib/doing/item.rb', line 8 def title @title end |
Instance Method Details
#calculate_end_date(opt) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/doing/item.rb', line 62 def calculate_end_date(opt) if opt[:took] if @date + opt[:took] > Time.now @date = Time.now - opt[:took] Time.now else @date + opt[:took] end elsif opt[:back] if opt[:back].is_a? Integer @date + opt[:back] else @date + (opt[:back] - @date) end else Time.now end end |
#duration ⇒ Object
If the entry doesn't have a @done date, return the elapsed time
37 38 39 40 41 |
# File 'lib/doing/item.rb', line 37 def duration return nil if @title =~ /(?<=^| )@done\b/ return Time.now - @date end |
#end_date ⇒ Time
Get the value of the item's @done tag
58 59 60 |
# File 'lib/doing/item.rb', line 58 def end_date @end_date ||= Time.parse(Regexp.last_match(1)) if @title =~ /@done\((\d{4}-\d\d-\d\d \d\d:\d\d.*?)\)/ end |
#equal?(other) ⇒ Boolean
Test for equality between items
95 96 97 98 99 100 101 102 103 |
# File 'lib/doing/item.rb', line 95 def equal?(other) return false if @title.strip != other.title.strip return false if @date != other.date return false unless @note.equal?(other.note) true end |
#expand_date_tags(additional_tags = nil) ⇒ Object
Updates the title of the Item by expanding natural language dates within configured date tags (tags whose value is expected to be a date)
145 146 147 |
# File 'lib/doing/item.rb', line 145 def ( = nil) @title.() end |
#highlight_search(search, distance: nil, negate: false, case_type: nil) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/doing/item.rb', line 274 def highlight_search(search, distance: nil, negate: false, case_type: nil) prefs = Doing.config.settings['search'] || {} matching = prefs.fetch('matching', 'pattern').normalize_matching distance ||= prefs.fetch('distance', 3).to_i case_type ||= prefs.fetch('case', 'smart').normalize_case new_note = Note.new if search.is_rx? || matching == :fuzzy rx = search.to_rx(distance: distance, case_type: case_type) new_title = @title.gsub(rx) { |m| yellow(m) } new_note.add(@note.to_s.gsub(rx) { |m| yellow(m) }) else query = search.strip.to_phrase_query if query[:must].nil? && query[:must_not].nil? query[:must] = query[:should] query[:should] = [] end query[:must].concat(query[:should]).each do |s| rx = Regexp.new(s.wildcard_to_rx, ignore_case(s, case_type)) new_title = @title.gsub(rx) { |m| yellow(m) } new_note.add(@note.to_s.gsub(rx) { |m| yellow(m) }) end end Item.new(@date, new_title, @section, new_note) end |
#id ⇒ String
Generate a hash that represents the entry
84 85 86 |
# File 'lib/doing/item.rb', line 84 def id @id ||= (@date.to_s + @title + @section).hash end |
#ignore_case(search, case_type) ⇒ Boolean
Determine if case should be ignored for searches
270 271 272 |
# File 'lib/doing/item.rb', line 270 def ignore_case(search, case_type) (case_type == :smart && search !~ /[A-Z]/) || case_type == :ignore end |
#interval ⇒ Object
Get the difference between the item's start date and the value of its @done tag (if present)
49 50 51 |
# File 'lib/doing/item.rb', line 49 def interval @interval ||= calc_interval end |
#move_to(new_section, label: true, log: true) ⇒ Object
Move item from current section to destination section
388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/doing/item.rb', line 388 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.truncate(60)} from #{from} to #{@section}") self end |
#overlapping_time?(item_b) ⇒ Boolean
Test if the interval between start date and @done value overlaps with another item's
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/doing/item.rb', line 124 def overlapping_time?(item_b) return true if same_time?(item_b) start_a = date a_interval = interval end_a = a_interval ? start_a + a_interval.to_i : start_a start_b = item_b.date b_interval = item_b.interval end_b = b_interval ? start_b + b_interval.to_i : start_b (start_a >= start_b && start_a <= end_b) || (end_a >= start_b && end_a <= end_b) || (start_a < start_b && end_a > end_b) end |
#same_time?(item_b) ⇒ Boolean
Test if two items occur at the same time (same start date and equal duration)
112 113 114 |
# File 'lib/doing/item.rb', line 112 def same_time?(item_b) date == item_b.date ? interval == item_b.interval : false end |
#search(search, distance: nil, negate: false, case_type: nil) ⇒ Boolean
Test if item matches search string
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/doing/item.rb', line 313 def search(search, distance: nil, negate: false, case_type: nil) prefs = Doing.config.settings['search'] || {} matching = prefs.fetch('matching', 'pattern').normalize_matching distance ||= prefs.fetch('distance', 3).to_i case_type ||= prefs.fetch('case', 'smart').normalize_case if search.is_rx? || matching == :fuzzy matches = @title + @note.to_s =~ search.to_rx(distance: distance, case_type: case_type) else query = search.strip.to_phrase_query if query[:must].nil? && query[:must_not].nil? query[:must] = query[:should] query[:should] = [] end matches = no_searches?(query[:must_not], case_type: case_type) matches &&= all_searches?(query[:must], case_type: case_type) matches &&= any_searches?(query[:should], case_type: case_type) end # if search =~ /(?<=\A| )[+-]\S/ # else # text = @title + @note.to_s # matches = text =~ search.to_rx(distance: distance, case_type: case_type) # end # if search.is_rx? || !fuzzy # matches = text =~ search.to_rx(distance: distance, case_type: case_type) # else # distance = 0.25 if distance > 1 # score = if (case_type == :smart && search !~ /[A-Z]/) || case_type == :ignore # text.downcase.pair_distance_similar(search.downcase) # else # score = text.pair_distance_similar(search) # end # if score >= distance # matches = true # Doing.logger.debug('Fuzzy Match:', %(#{@title}, "#{search}" #{score})) # end # end negate ? !matches : matches end |
#should_finish? ⇒ Boolean
Test if item is included in never_finish config and thus should not receive a @done tag
363 364 365 |
# File 'lib/doing/item.rb', line 363 def should_finish? should?('never_finish') end |
#should_time? ⇒ Boolean
Test if item is included in never_time config and thus should not receive a date on the @done tag
373 374 375 |
# File 'lib/doing/item.rb', line 373 def should_time? should?('never_time') end |
#tag(tags, **options) ⇒ Object
Add (or remove) tags from the title of the item
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/doing/item.rb', line 163 def tag(, **) added = [] removed = [] date = .fetch(:date, false) [:value] ||= date ? Time.now.strftime('%F %R') : nil .delete(:date) single = .fetch(:single, false) .delete(:single) = . if .is_a? ::String remove = .fetch(:remove, false) .each do |tag| bool = remove ? :and : :not if (tag, bool) @title.tag!(tag, **).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_array ⇒ Array
convert tags on item to an array with @ symbols removed
204 205 206 |
# File 'lib/doing/item.rb', line 204 def tag_array . end |
#tag_values?(queries, bool = :and, negate: false) ⇒ Boolean
Test if item matches tag values
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/doing/item.rb', line 248 def tag_values?(queries, bool = :and, negate: false) bool = bool.normalize_bool matches = case bool when :and all_values?(queries) when :not no_values?(queries) else any_values?(queries) end negate ? !matches : matches end |
#tags ⇒ Array
Get a list of tags on the item
195 196 197 |
# File 'lib/doing/item.rb', line 195 def @title.scan(/(?<= |\A)@([^\s(]+)/).map { |tag| tag[0] }.sort.uniq end |
#tags?(tags, bool = :and, negate: false) ⇒ Boolean
Test if item contains tag(s)
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/doing/item.rb', line 217 def (, bool = :and, negate: false) if bool == :pattern = ...join(' ') matches = tag_pattern?() return negate ? !matches : matches end = () bool = bool.normalize_bool matches = case bool when :and () when :not () else () end negate ? !matches : matches end |
#to_pretty(elements: %i[date title section]) ⇒ Object
outputs a colored string with relative date and highlighted tags
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/doing/item.rb', line 410 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
401 402 403 |
# File 'lib/doing/item.rb', line 401 def to_s "\t- #{@date.strftime('%Y-%m-%d %H:%M')} | #{@title}#{@note.empty? ? '' : "\n#{@note}"}" end |