Method: Doing::WWID#edit_last

Defined in:
lib/doing/wwid/editor.rb

#edit_last(section: 'All', options: {}) ⇒ Object

Edit the last entry

Parameters:

  • section (String) (defaults to: 'All')

    The section, default "All"

Raises:

  • (UserCancelled)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/doing/wwid/editor.rb', line 180

def edit_last(section: 'All', options: {})
  options[:section] = guess_section(section)

  item = last_entry(options)

  if item.nil?
    logger.debug('Skipped:', 'No entries found')
    return
  end

  old_item = item.clone
  content = ["#{item.date.strftime('%F %R')} | #{item.title.dup}"]
  content << item.note.strip_lines.join("\n") unless item.note.empty?
  new_item = fork_editor(content.join("\n"))
  raise UserCancelled, 'No change' if new_item.strip == content.join("\n").strip

  date, title, note = format_input(new_item)
  date ||= item.date

  if title.nil? || title.empty?
    logger.debug('Skipped:', 'No content provided')
  elsif title == item.title && note.equal?(item.note) && date.equal?(item.date)
    logger.debug('Skipped:', 'No change in content')
  else
    item.date = date unless date.nil?
    item.title = title
    item.note.add(note, replace: true)
    logger.info('Edited:', item.title)
    Hooks.trigger :post_entry_updated, self, item, old_item

    write(@doing_file)
  end
end