Class: Doing::Item
- Inherits:
-
Object
- Object
- Doing::Item
- Defined in:
- lib/doing/item.rb
Overview
This class describes a single WWID item
Instance Attribute Summary collapse
-
#date ⇒ Object
include Amatch.
-
#note ⇒ Object
include Amatch.
-
#section ⇒ Object
include Amatch.
-
#title ⇒ Object
include Amatch.
Instance Method Summary collapse
-
#end_date ⇒ Time
Get the value of the item's @done tag.
-
#equal?(other) ⇒ Boolean
Test for equality between items.
- #hash_id ⇒ Object
-
#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).
-
#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: 3, negate: false, case_type: :smart, fuzzy: false) ⇒ Boolean
Test if item matches search string.
- #should_finish? ⇒ Boolean
- #should_time? ⇒ Boolean
-
#tag(tag, value: nil, remove: false, rename_to: nil, regex: false) ⇒ Object
Add (or remove) tags from the title of the item.
-
#tags ⇒ Array
Get a list of tags on the item.
-
#tags?(tags, bool = :and, negate: false) ⇒ Boolean
Test if item contains tag(s).
Constructor Details
#initialize(date, title, section, note = nil) ⇒ Item
Initialize an item with date, title, section, and optional note
23 24 25 26 27 28 |
# File 'lib/doing/item.rb', line 23 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
include Amatch
10 11 12 |
# File 'lib/doing/item.rb', line 10 def date @date end |
#note ⇒ Object
include Amatch
10 11 12 |
# File 'lib/doing/item.rb', line 10 def note @note end |
#section ⇒ Object
include Amatch
10 11 12 |
# File 'lib/doing/item.rb', line 10 def section @section end |
#title ⇒ Object
include Amatch
10 11 12 |
# File 'lib/doing/item.rb', line 10 def title @title end |
Instance Method Details
#end_date ⇒ Time
Get the value of the item's @done tag
49 50 51 |
# File 'lib/doing/item.rb', line 49 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
64 65 66 67 68 69 70 71 72 |
# File 'lib/doing/item.rb', line 64 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 |
#hash_id ⇒ Object
53 54 55 |
# File 'lib/doing/item.rb', line 53 def hash_id (@date.to_s + @title + @section + @note.join(' ')).hash end |
#interval ⇒ Object
Get the difference between the item's start date and the value of its @done tag (if present)
40 41 42 |
# File 'lib/doing/item.rb', line 40 def interval @interval ||= calc_interval end |
#overlapping_time?(item_b) ⇒ Boolean
Test if the interval between start date and @done value overlaps with another item's
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/doing/item.rb', line 93 def overlapping_time?(item_b) return true if same_time?(item_b) start_a = date interval = interval end_a = interval ? start_a + interval.to_i : start_a start_b = item_b.date interval = item_b.interval end_b = interval ? start_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)
81 82 83 |
# File 'lib/doing/item.rb', line 81 def same_time?(item_b) date == item_b.date ? interval == item_b.interval : false end |
#search(search, distance: 3, negate: false, case_type: :smart, fuzzy: false) ⇒ Boolean
Test if item matches search string
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/doing/item.rb', line 162 def search(search, distance: 3, negate: false, case_type: :smart, fuzzy: false) text = @title + @note.to_s matches = text =~ search.to_rx(distance: distance, case_type: case_type) # 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
185 186 187 |
# File 'lib/doing/item.rb', line 185 def should_finish? should?('never_finish') end |
#should_time? ⇒ Boolean
189 190 191 |
# File 'lib/doing/item.rb', line 189 def should_time? should?('never_time') end |
#tag(tag, value: nil, remove: false, rename_to: nil, regex: false) ⇒ Object
Add (or remove) tags from the title of the item
114 115 116 |
# File 'lib/doing/item.rb', line 114 def tag(tag, value: nil, remove: false, rename_to: nil, regex: false) @title.tag!(tag, value: value, remove: remove, rename_to: rename_to, regex: regex).strip! end |
#tags ⇒ Array
Get a list of tags on the item
123 124 125 |
# File 'lib/doing/item.rb', line 123 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)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/doing/item.rb', line 136 def (, bool = :and, negate: false) = () bool = bool.normalize_bool matches = case bool when :and () when :not () else () end negate ? !matches : matches end |