Class: Doing::Items

Inherits:
Array
  • Object
show all
Defined in:
lib/doing/items.rb

Overview

Items Array

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#highlight_tags, #log_tags, #nested_hash, #tags_to_array, #to_tags, #to_tags!

Constructor Details

#initializeItems



8
9
10
11
# File 'lib/doing/items.rb', line 8

def initialize
  super
  @sections = []
end

Instance Attribute Details

#sectionsObject

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_tagsObject



108
109
110
111
112
# File 'lib/doing/items.rb', line 108

def all_tags
  each_with_object([]) do |entry, tags|
    tags.concat(entry.tags).sort!.uniq!
  end
end

#delete_item(item, single: false) ⇒ Object

Delete an item from the index



83
84
85
86
87
# 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
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.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_titlesArray

List sections, title only



17
18
19
# File 'lib/doing/items.rb', line 17

def section_titles
  @sections.map(&:title)
end

#to_sObject

Output sections and items in Doing file format



115
116
117
118
119
120
121
122
123
# File 'lib/doing/items.rb', line 115

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

Raises:

  • (ItemNotFound)


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/doing/items.rb', line 95

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