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, #time_string, #to_tags, #to_tags!

Constructor Details

#initializeItems

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

#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.

Parameters:

  • section (Section)

    The section to add. A String value will be converted to Section automatically.

  • log (Boolean) (defaults to: false)

    Add a log message notifying the user about the creation of the section.

Returns:

  • nothing



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



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

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

Parameters:

  • item

    The item



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

Parameters:

  • items (Items)

    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

Parameters:

  • section (String)

    section title

Returns:

  • (Items)

    Array of items



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

Parameters:

  • section (String)

    section title

Returns:

  • (Boolean)

    true if section 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

Returns:

  • (Array)

    section titles



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



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

Parameters:

  • old_item

    The old item

  • new_item

    The new item

Raises:

  • (ItemNotFound)


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