Class: Doing::Items
Overview
Items Array
Instance Attribute Summary collapse
-
#sections ⇒ Object
Returns the value of attribute sections.
Instance Method Summary collapse
-
#add_section(section, log: false) ⇒ Object
Add a new section to the sections array.
- #all_tags ⇒ Object
-
#delete_item(item, single: false) ⇒ Object
Delete an item from the index.
-
#diff(items) ⇒ Object
Return Items containing items that don't exist in receiver.
-
#in_section(section) ⇒ Items
Get a new Items object containing only items in a specified section.
-
#initialize ⇒ Items
constructor
A new instance of Items.
-
#section?(section) ⇒ Boolean
Test if section already exists.
-
#section_titles ⇒ Array
List sections, title only.
-
#to_s ⇒ Object
Output sections and items in Doing file format.
-
#update_item(old_item, new_item) ⇒ Object
Update an item in the index with a modified item.
Methods inherited from Array
#highlight_tags, #log_tags, #nested_hash, #tags_to_array, #time_string, #to_tags, #to_tags!
Constructor Details
#initialize ⇒ Items
Returns a new instance of Items.
8 9 10 11 |
# File 'lib/doing/items.rb', line 8 def initialize super @sections = [] end |
Instance Attribute Details
#sections ⇒ Object
Returns the value of attribute sections.
6 7 8 |
# File 'lib/doing/items.rb', line 6 def sections @sections end |
Instance Method Details
#add_section(section, log: false) ⇒ Object
Add a new section to the sections array. Accepts either a Section object, or a title string that will be converted into a Section.
52 53 54 55 56 57 58 59 |
# File 'lib/doing/items.rb', line 52 def add_section(section, log: false) section = section.is_a?(Section) ? section : Section.new(section.cap_first) return if section?(section) @sections.push(section) Doing.logger.info('New section:', %("#{section}" added)) if log end |
#all_tags ⇒ Object
109 110 111 112 113 |
# File 'lib/doing/items.rb', line 109 def each_with_object([]) do |entry, | .concat(entry.).sort!.uniq! end end |
#delete_item(item, single: false) ⇒ Object
Delete an item from the index
83 84 85 86 87 88 |
# File 'lib/doing/items.rb', line 83 def delete_item(item, single: false) deleted = delete(item) Doing.logger.count(:deleted) Doing.logger.info('Entry deleted:', deleted.title) if single deleted end |
#diff(items) ⇒ Object
Return Items containing items that don't exist in receiver
120 121 122 123 124 125 126 127 |
# File 'lib/doing/items.rb', line 120 def diff(items) diff = Items.new each do |item| res = items.select { |i| i.equal?(item) } diff.push(item) unless res.count.positive? end diff end |
#in_section(section) ⇒ Items
Get a new Items object containing only items in a specified section
68 69 70 71 72 73 74 75 76 |
# File 'lib/doing/items.rb', line 68 def in_section(section) if section =~ /^all$/i dup else items = Items.new.concat(select { |item| !item.nil? && item.section == section }) items.add_section(section, log: false) items end end |
#section?(section) ⇒ Boolean
Test if section already exists
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/doing/items.rb', line 27 def section?(section) has_section = false section = section.is_a?(Section) ? section.title.downcase : section.downcase @sections.each do |s| if s.title.downcase == section has_section = true break end end has_section end |
#section_titles ⇒ Array
List sections, title only
17 18 19 |
# File 'lib/doing/items.rb', line 17 def section_titles @sections.map(&:title) end |
#to_s ⇒ Object
Output sections and items in Doing file format
130 131 132 133 134 135 136 137 138 |
# File 'lib/doing/items.rb', line 130 def to_s out = [] @sections.each do |section| out.push(section.original) in_section(section.title).each { |item| out.push(item.to_s)} end out.join("\n") end |
#update_item(old_item, new_item) ⇒ Object
Update an item in the index with a modified item
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/doing/items.rb', line 96 def update_item(old_item, new_item) s_idx = index { |item| item.equal?(old_item) } raise ItemNotFound, 'Unable to find item in index, did it mutate?' unless s_idx return if fetch(s_idx).equal?(new_item) self[s_idx] = new_item Doing.logger.count(:updated) Doing.logger.info('Entry updated:', self[s_idx].title.truncate(60)) new_item end |