class Doing::Items

Items Array

Attributes

sections[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/doing/items.rb, line 8
def initialize
  super
  @sections = []
end

Public Instance Methods

add_section(section, log: false) click to toggle source

Add a new section to the sections array. Accepts either a Section object, or a title string that will be converted into a Section.

@param section [Section] The section to add. A String value will be converted to Section automatically. @param log [Boolean] Add a log message notifying the user about the creation of the section.

@return nothing

# 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() click to toggle source
# File lib/doing/items.rb, line 127
def all_tags
  each_with_object([]) do |entry, tags|
    tags.concat(entry.tags).sort!.uniq!
  end
end
dedup(match_section: true) click to toggle source

Remove duplicated entries. Duplicate entries must have matching start date, title, note, and section

@return [Items] Items array with duplicate entries removed

# File lib/doing/items.rb, line 152
def dedup(match_section: true)
  unique = Items.new
  each do |item|
    unique.push(item) unless unique.include?(item, match_section: match_section)
  end

  unique
end
dedup!(match_section: true) click to toggle source
# File lib/doing/items.rb, line 161
def dedup!(match_section: true)
  replace dedup(match_section: match_section)
end
delete_item(item, single: false) click to toggle source

Delete an item from the index

@param item The item

# File lib/doing/items.rb, line 101
def delete_item(item, single: false)
  deleted = delete(item)
  Doing.logger.count(:deleted)
  Doing.logger.info('Entry deleted:', deleted.title) if single
  deleted
end
delete_section(section, log: false) click to toggle source
# File lib/doing/items.rb, line 61
def delete_section(section, log: false)
  return unless section?(section)

  raise DoingRuntimeError, 'Section not empty' if in_section(section).count > 0

  deleted = false

  @sections.each do |sect|
    if sect.title == section && in_section(sect).count.zero?
      @sections.delete(sect)
      Doing.logger.info('Removed section:', %("#{section}" removed)) if log
      return
    end
  end

  Doing.logger.error('Not found:', %("#{section}" not found))
end
diff(items) click to toggle source

Return Items containing items that don't exist in receiver

@param items [Items] Receiver

# File lib/doing/items.rb, line 138
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) click to toggle source

Get a new Items object containing only items in a specified section

@param section [String] section title

@return [Items] Array of items

# File lib/doing/items.rb, line 86
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
include?(item, match_section: true) click to toggle source
# File lib/doing/items.rb, line 165
def include?(item, match_section: true)
  includes = false
  each do |other_item|
    if other_item.equal?(item, match_section: match_section)
      includes = true
      break
    end
  end

  includes
end
inspect() click to toggle source

@private

# File lib/doing/items.rb, line 191
def inspect
  sections = @sections.map { |s| "<Section:#{s.title} #{in_section(s.title).count} items>" }.join(', ')
  "#<Doing::Items #{count} items, #{@sections.count} sections: #{sections}>"
end
section?(section) click to toggle source

Test if section already exists

@param section [String] section title

@return [Boolean] true if section exists

# 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() click to toggle source

List sections, title only

@return [Array] section titles

# File lib/doing/items.rb, line 17
def section_titles
  @sections.map(&:title)
end
to_s() click to toggle source

Output sections and items in Doing file format

# File lib/doing/items.rb, line 178
def to_s
  out = []
  @sections.each do |section|
    out.push(section.original)
    items = in_section(section.title).sort_by { |i| [i.date, i.title] }
    items.reverse! if Doing.setting('doing_file_sort').normalize_order == :desc
    items.each { |item| out.push(item.to_s) }
  end

  out.join("\n")
end
update_item(old_item, new_item) click to toggle source

Update an item in the index with a modified item

@param old_item The old item @param new_item The new item

# File lib/doing/items.rb, line 114
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.trunc(60))
  new_item
end